find new position based on angle and length

You must understand the Game Editor concepts, before post here.

find new position based on angle and length

Postby pixelpoop » Thu Feb 08, 2007 11:54 pm

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?
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby Fuzzy » Fri Feb 09, 2007 5:42 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

thanks

Postby pixelpoop » Fri Feb 09, 2007 11:48 pm

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//
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest