Sign in to follow this  
Yoyoyoyo

Finding an angle relative to current angle

Recommended Posts

Righty, so;

I have a current rotation value of an object stored as radians, i am looking for a way to find the object's right and left, in a radian value.
This will allow me to move(strafe) left and right relative to it's heading.

So current angle = #
angle right = # (function here... )

Ive tried converting to degrees and then back again, but its making it overly complex i think, and doesn't sort the problem of angle being relative.
Anyone familiar with this problem?

Share this post


Link to post
Share on other sites
Heres my solution



if (Keyboard.GetState().IsKeyDown(Keys.A))
{


speed = 0.35f;
// degrees = 270.0f;
degrees = MathHelper.ToDegrees(pRot);
degrees -= 90;
pRotStrafe = MathHelper.ToRadians(degrees);


pVlx = (float)Math.Cos(pRotStrafe) * speed;
pVly = (float)Math.Sin(pRotStrafe) * speed;

pPos -= new Vector2(pVlx, pVly);



}

if (Keyboard.GetState().IsKeyDown(Keys.D))
{
speed = 0.35f;
degrees = MathHelper.ToDegrees(pRot);
degrees += 90;
pRotStrafe = MathHelper.ToRadians(degrees);

pVrx = (float)Math.Cos(pRotStrafe) * speed;
pVry = (float)Math.Sin(pRotStrafe) * speed;
pPos -= new Vector2(pVrx, pVry);

}


Strikes me as a lot of computation but it does work.

To alleviate all the questions i badger the boards with i can at least post solutions as i find them. :)

Share this post


Link to post
Share on other sites
Typically you'd do this using vector math rather than angles/trig. For example, given a vector (x, y), the perpendicular vectors are (-y, x) and (y, -x) (which is basically what you're computing here, but in a less straightforward way).

Share this post


Link to post
Share on other sites
You can cut out the conversion here, since Pi/2 (radians) = 90 (degrees).

[code]
degrees = MathHelper.ToDegrees(pRot);
degrees -= 90;
pRotStrafe = MathHelper.ToRadians(degrees);
[/code]
[color="#1C2837"] [/color]
[color="#1C2837"][size="2"]would become...[/size][/color]
[color="#1C2837"] [/color]
[color="#1C2837"][size="2"][code]
pRotStrafe = pRot - Pi/2;
[/code][/size][/color]
[color="#1C2837"] [/color]
[color="#1C2837"] [/color]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this