ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Writing a simple driving game (help!!)

(1/3) > >>

chedabob:
Right, so I'm trying my best to write a simple driving game in XNA (that's not important). Basically, it's a 2D top down game, but I'm struggling to get the vehicle to move in the direction it is facing. Does anybody have a formula that would give me the new coordinates based on the source position, rotation, and the distance to travel? I did try:

X = distance * cos(rotation)
Y = distance * sin(rotation)

but I just ended up with a vehicle that would rotate around the point, and sometimes it might just travel in the direction it was facing :p

Ralf Maximus:
COS and SIN functions return a decimal value between -1 and 1, which is probably too miniscule to do what you want without additional multiplication.  I see "distance" up there, but that doesn't tell me much.  Distance in pixels?  Sectors?  Miles?

Generally what I do is keep separate fp variables to hold the accumulated "speed" of the vehicle and add that to the x/y coordinates periodically to generate motion.  Don't modify the x/y position directly -- only modify the vectors (call them vx/vy).

Does that help? 

chedabob:
Distance is the amount in pixels it should travel.


I can move the vehicle about in any direction I want, but calculating how far X and how far Y to travel based on the rotation is the bit I'm struggling with.

mouser:
I'm just so happy to hear that some dc members are coding some games!  :up:
I hope you will share them with us.  I think actually i'm going to go create a special games section for the Programming School to encourage more of this.

f0dder:
Sounds a bit like the trigonometry math involved in good old turtle graphics :)

You should add the cos/sin(angle)*dist to the curx/cury variables, btw.

Navigation

[0] Message Index

[#] Next page

Go to full version