1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

2d movement

  1. Sep 22, 2011 #1
    I am writing a 2d computer game and need some help.

    I have a space ship that is moving(or stationary) and I want to move it to a destination coordinate.

    Here are the main variables:
    Code (Text):
    Const PI = 3.14159          'Mmmm.. Pi
    Const ACCEL = 0.1           'Rate of increase of speed
    Const ROTATION_RATE = 15    'Rotation speed
    Const SHIP_RADIUS = 10      'Distance from center of triangle to any vertex
    Const MS_DELAY = 25         'Milliseconds per frame (25 = 40 frames per second)

    Dim msngFacing As Single    'Angle the ship is facing (ok, ok, it's a triangle, not a ship! Shut it!)
    Dim msngHeading As Single   'Current direction in which ship is moving
    Dim msngSpeed As Single     'Current speed with which ship is moving
    Dim msngX As Single         'Current X coordinate of ship within form
    Dim msngY As Single         'Current Y coordinate of ship within form
     
  2. jcsd
  3. Sep 22, 2011 #2
    If my ship is not moving, it is pretty easy, just rotate to the target and apply thrust. My problem is if the ship is already moving.

    Can I please have some help with the physics ideas behind this and/or some advice on formulas?

    thanks
     
  4. Sep 22, 2011 #3

    Drakkith

    User Avatar

    Staff: Mentor

    This looks like it might belong in the Computer and Technology forum since it's programming. Perhaps an admin will move it.
     
  5. Sep 22, 2011 #4

    DavidSnider

    User Avatar
    Gold Member

    If the ship is already moving then why not just adjust the direction vector?
     
  6. Sep 22, 2011 #5
    If the ship is already moving nothing changes. Just turn the ship so it points in the opposite direction of the motion and apply thrust. Just add the thrust to the current velocity vector. Also you should use x and y coordinates for velocity and acceleration vectors. Only use polar coordinates for the direction the ship is pointing.

    So what you end up with is this:

    dx is the x velocity
    dy is the y velocity
    ddx is the x acceleration
    ddy is the y acceleration

    x = x + dx;
    y = y + dy;
    dx = dx + ddx;
    dy = dy + ddy;

    All you have to do now is calculate your accelerations based on the direction your ship is pointing and the thrust and adjust ddx and ddy accordingly. If there's no thrust just set ddx and ddy to 0. All you need to do is change ddx and ddy and the motions and velocities will work out automatically.
     
  7. Sep 22, 2011 #6
    OK, I will play around with my vb code to see if I can implement it.

    Dr Morbius, do you have any experience with vb?
     
  8. Sep 22, 2011 #7

    DavidSnider

    User Avatar
    Gold Member

    Are you using VB or VB .NET?
     
  9. Sep 22, 2011 #8
    I am testing it in VB6 at the moment, but am going to convert it to VB.NET when I have some time.

    The link to the vb6 file is: http://canning.co.nz/shpshoot.zip

    It is a simple physics demo, about moving a 2d ship with some physics.
     
  10. Sep 22, 2011 #9
    First things first, I will convert my variables into velocity and acceleration vectors. I will get that fixed, then work on the moving to coordinate problem.

    Am I corect in saying that the Xvelocity = speed * Sin(Heading) and Yvelocity = speed * Cos(Heading)?

    and the Xacceleration = ACCEL * Sin(Facing angle) and Yacceleration = ACCEL * Cos(Facing angle)?
     
    Last edited: Sep 22, 2011
  11. Sep 23, 2011 #10
    This is actually already done in the Physics subroutine. Here is the code:

    Code (Text):
    Private Sub Physics()

    Dim sngXComp As Single  'Resultant X and Y components
    Dim sngYComp As Single
    Dim i As Integer
           
        'Thrust
        If mblnUpKey Then
            mblnUpKey = False
            'Determine the X and Y components of the resultant vector
            sngXComp = Ship.msngSpeed * Sin(Ship.msngHeading) + Ship.ACCEL * Sin(Ship.msngFacing)
            sngYComp = Ship.msngSpeed * Cos(Ship.msngHeading) + Ship.ACCEL * Cos(Ship.msngFacing)
            'Determine the resultant speed
            Ship.msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)
            If Ship.msngSpeed > 5 Then Ship.msngSpeed = 5
            'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
            If sngYComp > 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp)
            If sngYComp < 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp) + PI
        End If
       
        Ship.msngX = Ship.msngX + Ship.msngSpeed * Sin(Ship.msngHeading)
        Ship.msngY = Ship.msngY - Ship.msngSpeed * Cos(Ship.msngHeading)
       
        If Ship.msngX < 0 Then Ship.msngX = ScreenWidth
        If Ship.msngY < 0 Then Ship.msngY = ScreenHeight
        If Ship.msngX > ScreenWidth Then Ship.msngX = 0
        If Ship.msngY > ScreenHeight Then Ship.msngY = 0


    End Sub
     
    Should I keep this code as it is, or implement some vector variables?
     
    Last edited: Sep 23, 2011
  12. Sep 23, 2011 #11
    OK, I had some spare time so I added some vector variables. Here is the code now:

    Code (Text):
    Private Sub Physics()

    Dim sngXComp As Single  'Resultant X and Y components
    Dim sngYComp As Single
    Dim i As Integer
                       
        'Thrust
        If mblnUpKey Then
            mblnUpKey = False
           
            Ship.Velocity.x = Ship.msngSpeed * Sin(Ship.msngHeading)
            Ship.Velocity.y = Ship.msngSpeed * Cos(Ship.msngHeading)
           
            Ship.Acceleration.x = Ship.ACCEL * Sin(Ship.msngFacing)
            Ship.Acceleration.y = Ship.ACCEL * Cos(Ship.msngFacing)

            sngXComp = Ship.Velocity.x + Ship.Acceleration.x
            sngYComp = Ship.Velocity.y + Ship.Acceleration.y
           
            Ship.msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)
            If Ship.msngSpeed > 5 Then Ship.msngSpeed = 5
            'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
            If sngYComp > 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp)
            If sngYComp < 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp) + PI
        End If
       
        Ship.msngX = Ship.msngX + Ship.Velocity.x
        Ship.msngY = Ship.msngY - Ship.Velocity.y
       
        If Ship.msngX < 0 Then Ship.msngX = ScreenWidth
        If Ship.msngY < 0 Then Ship.msngY = ScreenHeight
        If Ship.msngX > ScreenWidth Then Ship.msngX = 0
        If Ship.msngY > ScreenHeight Then Ship.msngY = 0


    End Sub

     
    I just had a look at Star Sonata, and this is the way they do it: When a new destination is selected (with the mouse), the ship comes to a stop, rotates to the new coordinate and then moves to it. I would like to implement this style if possible.

    I have got the ship to stop, now I want to turn to the destination, thrust towards it and then stop exactly on it.
     
    Last edited: Sep 23, 2011
  13. Sep 28, 2011 #12
    Use that only if you want to set the ship's velocity to a specific value but if you do that while your ship's already moving in another direction it will look like your ship suddenly changes direction. If you instead change the direction by changing the accelerations the motion will look more like a smooth turn.

    ddx = thrust * cos(heading), ddy = thrust * sin(heading)

    http://en.wikipedia.org/wiki/Polar_...rting_between_polar_and_Cartesian_coordinates
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: 2d movement
  1. Vertical Movement (Replies: 7)

  2. Contrail movement. (Replies: 2)

  3. Air movement (Replies: 9)

  4. Movement of a plank (Replies: 13)

Loading...