Forum Topic: Argh, _rotation help ><!

(243 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

ssjskipp

Reply To Post Reply & Quote

Posted at: 8/16/06 07:59 PM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 724

Okay, so I made this little missile targeting AI:
Here
And as you can see [click to fire a missile], the missile automatically rotate to the new target. I Know why, but what I would like to know, is an efficent, non-laggy script that'll ease it into the rotation.

Any idea? [I've tried using the mx.tweens, some a > _rotation, etc. but rotation goes 0-360, so it makes it act weird...]

Here's my method of getting the rotation:
//Move the missile in the direction it's rotated to
_x += s*Math.sin((_rotation*Math.PI)/180);
_y -= s*Math.cos((_rotation*Math.PI)/180);
//Get rotation
x = this._x-my_target._x;
y = this._y-my_target._y;
a = -Math.atan2(x, y)/(Math.PI/180);
//Set Rotation
_rotation = a;

All of this is on the missile itself, and under onClipEvent(enterFrame){}
where my_target is the movieclip of it's current target.

Ideas?

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 8/16/06 08:04 PM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,968

instead of setting it to A, how about something like this?

if(_roation<a){
_roation++;
} else {
_rotation--;
}


None

ssjskipp

Reply To Post Reply & Quote

Posted at: 8/16/06 08:11 PM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 724

Tried it, rotations go from 0-360
so what that'll look like
say you're ay 0°
you have to rotate to 288°...
insed of going backwards like 50, you go clockwise about 288°, amknig it mess up, :o

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

ssjskipp

Reply To Post Reply & Quote

Posted at: 8/16/06 11:06 PM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 724

Bump!

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

Go0gley

Reply To Post Reply & Quote

Posted at: 8/17/06 12:07 AM

Go0gley LIGHT LEVEL 19

Sign-Up: 07/08/04

Posts: 218

Well first make sure rotation and a are 0-360....

_rotation += _rotation<0 ? 360 : (_rotation>=360 ? -360 : 0);
a += a<0 ? 360 : (a>=360 ? -360 : 0);

Then find the difference between the two angles, and make that 0-360 as well....

diff = a-_rotation;
diff += diff<0 ? 360 : (diff>=360 ? -360 : 0);

Now you know exactly how many degrees the missile has to turn to meet its target (without worrying about crossing from 359 degrees onto 0).


None

ssjskipp

Reply To Post Reply & Quote

Posted at: 8/17/06 12:50 AM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 724

...can Iget that translated into if then else, etc? I'm not good with the alternatve ways [? : etc.]

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

Go0gley

Reply To Post Reply & Quote

Posted at: 8/17/06 12:57 AM

Go0gley LIGHT LEVEL 19

Sign-Up: 07/08/04

Posts: 218

Haha... ok (if/else tends to run faster btw and I always use it when I'm not in a rush)

To make angle d between 0-360 degrees...

if (d<0) {
d+=360;
} else if (d>=360) {
d-=360;
}

That's assuming it never goes beyond -720 and 720 which it doesn't in ur situation


None

ssjskipp

Reply To Post Reply & Quote

Posted at: 8/17/06 01:40 AM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 724

wait, so now it's 0-360, but my problem still remains, jumping the 359-0 barrier ><

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

Go0gley

Reply To Post Reply & Quote

Posted at: 8/17/06 02:15 AM

Go0gley LIGHT LEVEL 19

Sign-Up: 07/08/04

Posts: 218

That's why instead of using the absolute rotation, we're gonna specify our own rotation (diff) relative to the rotation of the missile.

diff = a-_rotation;
if (diff<0) {
diff += 360;
} else if (diff>=360) {
diff -= 360;
}

So...

if (diff<=180) {
_rotation += turnspeed;
} else {
_rotation -= turnspeed;
}

Here's a graphic representation - http://www.pixfolder.com/images/4121.jpg - when _rotation = 60 and a = 300, the missile has to rotate anticlockwise (which crosses between 359 and 0 for _rotation but not for diff, so no problem)

If you're still having trouble understanding, I can give you one of my homing codes for you to figure out how they work.


None

ssjskipp

Reply To Post Reply & Quote

Posted at: 8/17/06 02:27 PM

ssjskipp LIGHT LEVEL 14

Sign-Up: 10/16/03

Posts: 724

Ah, thanks :D! I get it now!

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."


None

Paranoia

Reply To Post Reply & Quote

Posted at: 8/17/06 02:34 PM

Paranoia DARK LEVEL 31

Sign-Up: 04/22/05

Posts: 9,119

At 8/17/06 01:40 AM, ssjskipp wrote: wait, so now it's 0-360, but my problem still remains, jumping the 359-0 barrier ><

Actually, rotations like this are theoretically infinate (or looping), where 0º = 360º = 720º = 1080º and so on. So, giving something a rotation of 361º would be the same as giving it 1º.

And as best as I can remember, I believe rotations in Flash go from -180º to 180º. Those are, at least, the values I get from finding angles.

A rate of change in sanity with respect to time.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 09:11 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!