Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. How to determine the next x,y coordinate for a tank in a 2-D game...

How to determine the next x,y coordinate for a tank in a 2-D game...

Scheduled Pinned Locked Moved Algorithms
tutorialquestiongame-devhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Edmundisme
    wrote on last edited by
    #1

    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

    7 M 2 Replies Last reply
    0
    • E Edmundisme

      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

      7 Offline
      7 Offline
      73Zeppelin
      wrote on last edited by
      #2

      Define the motion using a vector. The position coordinates are given by: x = r*cos(theta) y = r*sin(theta) where r is the distance traveled and theta is the angle. You can get more description here[^]. You'll also have to assign a velocity.

      I'm the ocean. I'm a giant undertow.

      M E 2 Replies Last reply
      0
      • E Edmundisme

        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

        M Offline
        M Offline
        Matthew Butler 0
        wrote on last edited by
        #3

        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

        E 1 Reply Last reply
        0
        • 7 73Zeppelin

          Define the motion using a vector. The position coordinates are given by: x = r*cos(theta) y = r*sin(theta) where r is the distance traveled and theta is the angle. You can get more description here[^]. You'll also have to assign a velocity.

          I'm the ocean. I'm a giant undertow.

          M Offline
          M Offline
          Matthew Butler 0
          wrote on last edited by
          #4

          Beat me to it. (Within 3 minutes, more than 3 hours from the original question, what are the chances of that?)

          Matthew Butler

          7 1 Reply Last reply
          0
          • 7 73Zeppelin

            Define the motion using a vector. The position coordinates are given by: x = r*cos(theta) y = r*sin(theta) where r is the distance traveled and theta is the angle. You can get more description here[^]. You'll also have to assign a velocity.

            I'm the ocean. I'm a giant undertow.

            E Offline
            E Offline
            Edmundisme
            wrote on last edited by
            #5

            Thanks, I appreciate it!

            1 Reply Last reply
            0
            • M Matthew Butler 0

              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

              E Offline
              E Offline
              Edmundisme
              wrote on last edited by
              #6

              Thanks!

              CPalliniC 1 Reply Last reply
              0
              • E Edmundisme

                Thanks!

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                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

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • M Matthew Butler 0

                  Beat me to it. (Within 3 minutes, more than 3 hours from the original question, what are the chances of that?)

                  Matthew Butler

                  7 Offline
                  7 Offline
                  73Zeppelin
                  wrote on last edited by
                  #8

                  Pretty low I would imagine....wacky!

                  I'm the ocean. I'm a giant undertow.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups