How to calcolate moving vector of object by its rotation degress in 2d

Started by
5 comments, last by umen242 7 years, 6 months ago

Hello all

i have object in game which is rotating when i move my joystick , now i need to calculate its movement vector by its rotation
the object always moving forward and controlled by the joystick , the joystick is only setting its direction by degree .
here is the image which illustrate the scene .

i need some formula of the vector which the object will move .

[sharedmedia=core:attachments:33561]

Thanks


Advertisement
The vector is (cos(angle), sin(angle)). This vector is so useful that you generally don't need to deal with the angle at all. The representation of a direction as a unit-length vector is much better for many reasons, even if you find angles more natural.

Thanks for the replay , but can you please explain more , or can you can you please direct me to some links to explain ?

The input of the joystick comes in as two numbers, the x direction and the y direction. The velocity of an object can be represented by the same way. There is no need to convert the joystick direction to an angle, just to convert it back to a x,y pair.

For example, this is doing it with trigonometry

float angle = atan2(joystickInput.y, joystickInput.x);
float magnitude = sqrt(joystickInput.x * joystickInput.x + joystickInput.y * joystickInput.y);

float velocityX = cos(angle) * speed * magnitude;
float velocityY = sin(angle) * speed * magnitude;
Just do this instead

float velocityX = joystickInput.x * speed;
float velocityY = joystickInput.y * speed;
My current game project Platform RPG

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

The vector is (cos(angle), sin(angle)).

note that this is on a unit circle.

if you use axes with pos x = right and pos y = up, and theta measured clockwise from the pos y axis (like compass degrees), your unit direction vector is x = sin(theta), y = cos(theta).

and for a movement distance d, you displacement vector is x = d*sin(theta), y=d*cos(theta).

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

Thanks all!

This topic is closed to new replies.

Advertisement