2d top down rotations
Hey all,
I''ve recently been programming quite a few little games of varying styles in dos (mainly a 2d platformer and a Shooting Gallery), using DJGPP & the allegro library, but the one thing that keeps stumping me is how to do a 2d, top-down racing game... Most of it i''m comfortable with, except one key issue: the angle of the car, and therefore the direction it''s travelling in... I want to be able to rotate my car with the left and right keys (for example) and accelerate by pressing the up arrow, but I just can''t figure out how to calculate what angle the car should be facing... I don''t need to worry about gradual acceleration / decceleration (I can do that later), but I just want to get the simple car turning right and left and then moving in the correct direction... I assume I need to do something using er... well, degrees or something... erm...
Forgive me if there''s no simple explanation (in which case i''m getting ahead of myself) but i''m sure I could just make an array with the necessary x and y velocities for each angle... or maybe not
Cheers for any help,
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
well, you can give it an x,y, and a rotation variable. you set the rotation variable at the beginning of the game or level or whatever. anyways, each frame you rotate the sprite of the car by the rotation measurement. pressing left or right keys could incerment or decrement the rotation valuye. allegro comes with a function to rotate sprites so it should be pretty easy.
JoeMont001@aol.com www.polarisoft.n3.net
JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
but remember, the allegro library''s rotation function takes a value from 0 to 255 as the rotation amount. So a rotation of 90 degrees would be a value of 64 and 180 degrees would be 128. Understand ?
So you need to do something like this
now some of those function names might not be exact because it has been a long time since I used allegro but you can get the idea.
drawRotatedSprite(
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
So you need to do something like this
int degreeAngle;int xPos;int yPos;void rotate(){ if (rightKeyPressed) { degreeAngle += turnRate; } else if (leftKeyPressed) { degreeAngle -= turnRate; } if (upKeyPressed) { int radians = degreeAngle * 0.0174444; // convert degrees to radians xPos += cos(radians); yPos += sin(radians); }}void drawPlayer(){ int rotateValue = degreeAngle * 0.711111; // convert to allegro rotate value drawRotatedSprite(backBuffer, mySprite, xPos, Ypos, rotateValue);}
now some of those function names might not be exact because it has been a long time since I used allegro but you can get the idea.
drawRotatedSprite(
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
Yeah, but it''s not the drawing of the sprite i''m reffering to, specifically... I''m talking about, based on whatever angle the sprite is facing, what *direction* it should travel... I can''t just say ''if (we_want_to_go_forward) x ++;'' because that would only work if we were at an angle of 90 Degrees... So I need to work out what the x and y velocities should be, based on what angle we''re at (and the max speed of the car, for example). I was thinking of having something like...
etc. But, i need to fill those arrays with their correct values at all 360 angles... see? I hope that was slightly more clear
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
//at the top of the .cppfloat xv[360];float xy[360];//later, when we wan''t to accelerate or turnif (key[KEY_UP]){ x += xv[current_angle]; y += xy[current_angle];}if (key[KEY_LEFT]){ current_angle --;}
etc. But, i need to fill those arrays with their correct values at all 360 angles... see? I hope that was slightly more clear
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
Ah, looks like ncsu121978 just beat me to it
Okidoke, i'll try something like that...
I assume, where you say:
xPos += cos(radians);
yPos += sin(radians);
I can multiply that by the speed I want to travel at? Maybe not, but bear with me, maths isn't my strong point
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
Edited by - Llamasoft.net on July 30, 2000 2:44:28 PM
Okidoke, i'll try something like that...
I assume, where you say:
xPos += cos(radians);
yPos += sin(radians);
I can multiply that by the speed I want to travel at? Maybe not, but bear with me, maths isn't my strong point
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
Edited by - Llamasoft.net on July 30, 2000 2:44:28 PM
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
Have a direction in degrees. Then do this:
x = sin(direction)
y = cos(direction)
That does a coordinate transform from polar to cartesian coordinates. Make sure you are using degrees sine not radians sine. Then you multiply x and y by the velocity of the car.
------------------------------
#pragma twice
x = sin(direction)
y = cos(direction)
That does a coordinate transform from polar to cartesian coordinates. Make sure you are using degrees sine not radians sine. Then you multiply x and y by the velocity of the car.
------------------------------
#pragma twice
xPos += (speed * cos(radians));
yPos += (speed * sin(radians));
and if I were you then I would use a lookup table for the sin and cos because they are SLOW functions and it seems as though you will be calling them alot
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
yPos += (speed * sin(radians));
and if I were you then I would use a lookup table for the sin and cos because they are SLOW functions and it seems as though you will be calling them alot
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
quote: Original post by ncsu121978
xPos += (speed * cos(radians));
yPos += (speed * sin(radians));
and if I were you then I would use a lookup table for the sin and cos because they are SLOW functions and it seems as though you will be calling them alot
That''s pretty much exactly what I thought... thankyou very, very much
I love this forum...
Cheers everyone!
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
First things first.
your doing a top-down 2D racer? STOPPPPPPPPPPP
thats my idea :-)
http://www.positech.co.uk
your doing a top-down 2D racer? STOPPPPPPPPPPP
thats my idea :-)
http://www.positech.co.uk
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement