Make something move in a specific degree

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Make something move in a specific degree

Post 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!
Last edited by superLED on Thu Sep 22, 2011 9:30 am, edited 1 time in total.
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Make something move in a specific degree

Post by k1net1k »

sounds like vector based movement
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Make something move in a specific degree

Post 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....
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Make something move in a specific degree

Post 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
Last edited by short on Thu Sep 22, 2011 11:48 am, edited 2 times in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Make something move in a specific degree

Post 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...
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: Make something move in a specific degree

Post 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!
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Make something move in a specific degree

Post 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..
Post Reply