How to determine the next x,y coordinate for a tank in a 2-D game...
-
Hello, I'm putting together a Silverlight 2 demo application. It is going to be a simple 2-D tank game where you can use the arrow keys to rotate and move the tank. The tank can point in any direction (360 possible directions, right?). I've written the code to rotate the tank, but I'm not sure where to begin regarding moving the tank. How can I take the angle of the tank (say, 156 degrees for example) and use that to determine the next x,y position of the tank when it moves forward? I'm sure there must be a simple equation for this, but I don't know what it would be called or even how to google this problem. Any ideas? Thanks, Ian
-
Hello, I'm putting together a Silverlight 2 demo application. It is going to be a simple 2-D tank game where you can use the arrow keys to rotate and move the tank. The tank can point in any direction (360 possible directions, right?). I've written the code to rotate the tank, but I'm not sure where to begin regarding moving the tank. How can I take the angle of the tank (say, 156 degrees for example) and use that to determine the next x,y position of the tank when it moves forward? I'm sure there must be a simple equation for this, but I don't know what it would be called or even how to google this problem. Any ideas? Thanks, Ian
-
Hello, I'm putting together a Silverlight 2 demo application. It is going to be a simple 2-D tank game where you can use the arrow keys to rotate and move the tank. The tank can point in any direction (360 possible directions, right?). I've written the code to rotate the tank, but I'm not sure where to begin regarding moving the tank. How can I take the angle of the tank (say, 156 degrees for example) and use that to determine the next x,y position of the tank when it moves forward? I'm sure there must be a simple equation for this, but I don't know what it would be called or even how to google this problem. Any ideas? Thanks, Ian
Just use basic geometry/maths... nextXpos = lastXpos + speed * sin(angle); nextYpos = lastYpos - speed * cos(angle); [angle is measured from the vertical, clockwise] The only things you need to remember are: > Math.Sin and Math.Cos take the angle argument in radians not degrees. > Sin and Cos return double precision numbers: which may round to 0 or 1 if you are using integer positions and therefore the tank will not move as you want. Hope this helps.
Matthew Butler
-
Beat me to it. (Within 3 minutes, more than 3 hours from the original question, what are the chances of that?)
Matthew Butler
-
Thanks, I appreciate it!
-
Just use basic geometry/maths... nextXpos = lastXpos + speed * sin(angle); nextYpos = lastYpos - speed * cos(angle); [angle is measured from the vertical, clockwise] The only things you need to remember are: > Math.Sin and Math.Cos take the angle argument in radians not degrees. > Sin and Cos return double precision numbers: which may round to 0 or 1 if you are using integer positions and therefore the tank will not move as you want. Hope this helps.
Matthew Butler
Thanks!
-
Thanks!
Edmundisme wrote:
Thanks!
Well, should be tanks, instead... :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Beat me to it. (Within 3 minutes, more than 3 hours from the original question, what are the chances of that?)
Matthew Butler
Pretty low I would imagine....wacky!
I'm the ocean. I'm a giant undertow.