Page 1 of 1

find new position based on angle and length

PostPosted: Thu Feb 08, 2007 11:54 pm
by pixelpoop
hiya all,

I'm having a coding/math problem. I need to put spriteA a certain distance from spriteB and off in a certain direction/angle. how do I find spriteA's new coordinates?

I'm not sure but I think my problem needs the sin cos tan functions to be solved but I don't know how to use them properly.

any help?

PostPosted: Fri Feb 09, 2007 5:42 am
by Fuzzy
offsetY = sin(angle that you want)*distance that you want;
offsetX = cos(angle that you want)*distance that you want;

then
actorB.x = actor.x + OffsetX;
actorB.y = actor.y + OffsetY;

note that angle and distance are GE variables. Pick two other names.

thanks

PostPosted: Fri Feb 09, 2007 11:48 pm
by pixelpoop
thanks!
I had to add "degtorad" to convert my degrees into radians.
For some reason I couldn't have degtorad resolved and stored in a variable earlier in the script, it had to be a part of the sin and cos lines. So now it gets computed twice instead of once. but it works.

Here is the final code for anyone else that wants it.
The animation # of one actor determines the direction the other actor is off at.

Code: Select all
a=(actorA.animpos*9);
//there are a total of 40 frames of animation (9*40=360 degrees)//
//so "a" equals my slope/direction that I want it at.//

offsetx = sin(degtorad(a))*31;
offsety = cos(degtorad(a))*31;
//degtorad converts degrees"(a)" into radians (sin/cos need to work in radians)//
//31 is the distance the new actor will be from this actor//


actorB.x = actorA.x - offsetx;
actorB.y = actorA.y + offsety;
//this changes the x/y coordinants of the new actor//