Page 1 of 1

Make something move in a specific degree

Posted: Thu Sep 22, 2011 9:25 am
by superLED
I would like to be able to move my player, or whatever it may be, in a specific degree.
As for now, I have to make a single function for each degree (usually only up, down, left, right).
But I want a function like "void moveDeg(int degree, int distance)".

How would I make this function?

And also, I am working on a game that features joysticks. Here I get the the X position and the Y position of the analog stick (one of them).
How could I convert those numbers to a degree?

Even though this may only be a math question, I'll let you know that I'm using C++.

I would be grateful for any links and posts. Thanks!

Re: Make something move in a specific degree

Posted: Thu Sep 22, 2011 9:27 am
by k1net1k
sounds like vector based movement

Re: Make something move in a specific degree

Posted: Thu Sep 22, 2011 10:47 am
by N64vSNES
Do you really need angles for this?

Just check the X/Y values of the joystick.

For example:

Code: Select all

if ( JoyX < 0 )
{
     PosX -= movementVal;
}
else if ( JoyX > 0 )
{
     PosX += movementVal;

if ( JoyY < 0 )
{
     PosY -= movementVal;
}
else if ( JoyY > 0 )
{
     PosY+= movementVal;
}
I have no idea exactly what libraries/framework you're using or even what input device you're trying to use. But I find it hard to believe you can only get the angle of the analog stick....

Re: Make something move in a specific degree

Posted: Thu Sep 22, 2011 10:57 am
by short
Vector addition!

Image

If you need further explanation, just ask. You just need to understand a little about vectors.

Hmm my edit seems to have disappeared...

Take a look at this video on vectors, I know you've seen it!!

http://www.youtube.com/watch?v=sDDRZv5QM80

Re: Make something move in a specific degree

Posted: Thu Sep 22, 2011 11:31 am
by Falco Girgis
I'm fairly certain you are misusing the term "degree" here. I was thinking you wanted some sort of rotational mathematics, but what you're asking for does sound like simple vector addition...

Re: Make something move in a specific degree

Posted: Thu Sep 22, 2011 3:49 pm
by superLED
N64vSNES wrote:Do you really need angles for this?

Just check the X/Y values of the joystick.

For example:

Code: Select all

*code*
I have no idea exactly what libraries/framework you're using or even what input device you're trying to use. But I find it hard to believe you can only get the angle of the analog stick....
I am sing SDL. I am getting the X and Y position of the axis. Something between +3600 to -3600 on each axis.
If I used this code, wouldn't the object move pretty fast if the axis is all the way to 3600?
short wrote:Vector addition!

*image*

If you need further explanation, just ask. You just need to understand a little about vectors.

Hmm my edit seems to have disappeared...

Take a look at this video on vectors, I know you've seen it!!

http://www.youtube.com/watch?v=sDDRZv5QM80
Thanks for that beauiful paper! I will look into that and see what I can get out of it ^^
GyroVorbis wrote:I'm fairly certain you are misusing the term "degree" here. I was thinking you wanted some sort of rotational mathematics, but what you're asking for does sound like simple vector addition...
Yes. I think I really ment angle. But that may be a invalid term as well, given that I don't have english as my mother thounge, and I am not that talented with math. But I hope the point of the question was clear anyway.
I will look around for vector addition, and see what I can find. Thanks!

Re: Make something move in a specific degree

Posted: Thu Sep 22, 2011 4:05 pm
by k1net1k
superLED wrote: I am sing SDL. I am getting the X and Y position of the axis. Something between +3600 to -3600 on each axis.
If I used this code, wouldn't the object move pretty fast if the axis is all the way to 3600?
you could always calculate this a percentage, and then multiply that by the amount of movement you actually want..