hello all, it’s been a long time since i posted a question here but im really stuck on this one.
im trying to create a life sim which requires random motion. i thought id create a creature that moves around and turns in random directions. to make the turning smooth, i decided to ease the turning. this is where the problem starts. btw, here’s what i have in AS:
scrnW = scrnH=300;
mvSpd = 1;
randAng = function () {
cir.ang += Math.random()*200-100;
};
rotate = function (spd) {
cir._rotation -= (cir._rotation-cir.ang)/spd;
};
cir.onEnterFrame = function() {
this._x += Math.cos(this._rotation*Math.PI/180)*mvSpd;
this._y += Math.sin(this._rotation*Math.PI/180)*mvSpd;
this._x = this._x>scrnW ? 0 : this._x<0 ? scrnW : this._x;
this._y = this._y>scrnH ? 0 : this._y<0 ? scrnH : this._y;
};
setInterval(randAng, 5000);
setInterval(rotate, 20, 50);
when cir.ang becomes greater than 180 (lets say 200) then the thing tries to rotate to 200 but once it passes 180, it jumps back to -180 (_rotation has a range form -180 to 180) and it starts going in circles because it can never reach 200 degrees… how can i fix this i tried this code:
scrnW = scrnH=300;
mvSpd = 1;
randAng = function () {
cir.ang += Math.random()*200-100;
};
rotate = function (spd) {
rot = cir._rotation -= (cir._rotation-cir.ang)/spd;
//prevents cir.ang from going over 180 and under -180
if (cir.ang>180) {
cir.ang = 360-cir.ang;
}
if (cir.ang<-180) {
cir.ang = 360+cir.ang;
}
};
cir.onEnterFrame = function() {
this._x += Math.cos(this._rotation*Math.PI/180)*mvSpd;
this._y += Math.sin(this._rotation*Math.PI/180)*mvSpd;
this._x = this._x>scrnW ? 0 : this._x<0 ? scrnW : this._x;
this._y = this._y>scrnH ? 0 : this._y<0 ? scrnH : this._y;
};
setInterval(randAng, 2000);
setInterval(rotate, 20, 50);
but this just prevents it from completely turning all the way around, which is what im looking for…