Results 1 to 6 of 6

Thread: [RESOLVED] Converting 2d to 3d

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Resolved [RESOLVED] Converting 2d to 3d

    This is my current code for a 2d movement of a ship:

    Ship.Velocity.x = Ship.msngSpeed * System.Math.Sin(Ship.msngHeading)
    Ship.Velocity.y = Ship.msngSpeed * System.Math.Cos(Ship.msngHeading)

    Ship.Acceleration.x = Ship.ACCEL * System.Math.Sin(Ship.msngFacing)
    Ship.Acceleration.y = Ship.ACCEL * System.Math.Cos(Ship.msngFacing)

    sngXComp = Ship.Velocity.x + Ship.Acceleration.x
    sngYComp = Ship.Velocity.y + Ship.Acceleration.y

    Ship.msngSpeed = System.Math.Sqrt(sngXComp ^ 2 + sngYComp ^ 2)
    If Ship.msngSpeed > Ship.MAXSPEED Then Ship.msngSpeed = Ship.MAXSPEED

    If sngYComp > 0 Then Ship.msngHeading = System.Math.Atan(sngXComp / sngYComp)
    If sngYComp < 0 Then Ship.msngHeading = System.Math.Atan(sngXComp / sngYComp) + PI

    Ship.msngX = Ship.msngX + Ship.Velocity.x
    Ship.msngY = Ship.msngY - Ship.Velocity.y
    I am wanting to move this to a 3d world.

    I presume that I change the .y additions to .z

    This is my current turning code:
    Code:
            If Inp.IsKeyPressed(MTV3D65.CONST_TV_KEY.TV_KEY_LEFT) = True Then
                Ship.msngFacing = Ship.msngFacing - Ship.ROTATION_RATE * PI / 180
            End If
            If Inp.IsKeyPressed(MTV3D65.CONST_TV_KEY.TV_KEY_RIGHT) = True Then
                Ship.msngFacing = Ship.msngFacing + Ship.ROTATION_RATE * PI / 180
            End If
    I need to change the Ship.msngFacing (and the Ship.msngHeading I presume) to be a 3d coordinate for the direction of the 3d ship. Can I please have some help with this?

    Canning
    Last edited by Simon Canning; Oct 27th, 2011 at 10:59 PM.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Converting 2d to 3d

    The code you show for the 2D world isn't clear to me.
    You make the x- and y-components of accelaration out of the accelaration and "facing" (what is the difference to heading?, heading+/- drift?)
    You make something called sngXComp out of the x components of speed and accelartion, you do the same for Y
    And you make the speed (use in step 1) out of those sngXComp and sngYComp???
    I'm lost!

    To move to 3D you need to make a decision, do you want to move on a flat (virtual)plane, in this case a ship will always be at z=0 (disregarding the up and down with waves).
    If you want to move on the surface of the earth, you need to do more calculations, since a movement on the surface will change the z-component as well.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Converting 2d to 3d

    I will explain my 2d version (which is working by the way)

    I have a 2d ship:

    Its facing angle is msngFacing
    Its current heading (momentum) is msngHeading
    Its speed is msngSpeed
    Its X Coordinate is msngX
    Its Y Coordinate is msngY
    Its Acceleration is ACCEL
    Its MaxSpeed is MAXSPEED
    Its rotation rate is ROTATION_RATE
    Its velocity vector is Velocity
    Its acceleration vector is Acceleration

    When I apply thrust, the following code is executed:

    Code:
                Ship.Velocity.x = Ship.msngSpeed * System.Math.Sin(Ship.msngHeading)
                Ship.Velocity.y = Ship.msngSpeed * System.Math.Cos(Ship.msngHeading)
    
                Ship.Acceleration.x = Ship.ACCEL * System.Math.Sin(Ship.msngFacing)
                Ship.Acceleration.y = Ship.ACCEL * System.Math.Cos(Ship.msngFacing)
    
                sngXComp = Ship.Velocity.x + Ship.Acceleration.x
                sngYComp = Ship.Velocity.y + Ship.Acceleration.y
    
                Ship.msngSpeed = System.Math.Sqrt(sngXComp ^ 2 + sngYComp ^ 2)
                If Ship.msngSpeed > Ship.MAXSPEED Then Ship.msngSpeed = Ship.MAXSPEED
    
                If sngYComp > 0 Then Ship.msngHeading = System.Math.Atan(sngXComp / sngYComp)
                If sngYComp < 0 Then Ship.msngHeading = System.Math.Atan(sngXComp / sngYComp) + PI
    
                Ship.msngX = Ship.msngX + Ship.Velocity.x
                Ship.msngY = Ship.msngY - Ship.Velocity.y
    In my 3d world, I have a 3d terrain, but keep the y coordinate (the height of the ship) to be constant.
    I have some VB code for you too look at if you would like.

    Canning

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Converting 2d to 3d

    ..and the "apply thrust" would be a change of Ship.Accel?

    How is your 3D terrain build up? Would the sea always have the same coordinate for height( i'd call that z)? in this case there would be no need to claculate something like Climb, Climbrate etc for a ship.
    If you would be using Submarines or aircraft however, you could use climb, climbrate etc seperate from the 2D values. Or do you want to base all 3D-calculations based on a single 3D thrust applied?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Converting 2d to 3d

    All I want is to move the shift forward when I apply the thrust. The 3d terrain is all sorted.

    Ship.Accel is a variable of the rate of increase for the acceleration when thrusting forward. It stays the same and is the value to add to the acceleration vector when thrusting.

    Canning
    Last edited by Simon Canning; Oct 28th, 2011 at 01:59 AM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Converting 2d to 3d

    OK, all done.
    Last edited by Simon Canning; Oct 28th, 2011 at 08:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width