Source Code: MoveSpeedDir2D( )
rating: 0+x

MoveSpeedDir2D - calculate movement based on speed and direction.

Syntax

NewX#, NewY# = MoveSpeedDir2D ( X#, Y#, Speed#, Dir# )

  • NewX# - new calculated X position
  • NewY# - new calculated Y position
  • X# - current X position
  • Y# - current Y position
  • Speed# - speed of motion
  • Dir# - direction of motion

Explanation

MoveSpeedDir2D() calculates a new X/Y position based on speed and direction of movement.

Examples

Move an Object Based on Speed and Direction

SetFPS 60

PlayerX# = GetScreenHeight() / 2
PlayerY# = GetScreenWidth() / 2
PlayerSpeed# = 0
PlayerDir# = 0

Do

    If RightKey()
        PlayerSpeed# = 2
        PlayerDir# = 0
    ElseIf LeftKey()
        PlayerSpeed# = 2
        PlayerDir# = 180
    Else
        PlayerSpeed# = 0
    EndIf    

    PlayerX#, PlayerY# = MoveSpeedDir2D( PlayerX#, PlayerY#, PlayerSpeed#, PlayerDir# )    

    Cls 0
        Circle PlayerX#, PlayerY#, 10, False
    Sync
Loop

PSub MoveSpeedDir2D(x#, y#, speed#, dir#)
    NewX# = CosNewValue(x#,dir#,speed#)
    NewY# = SinNewValue(y#,dir#,speed#)
EndPSub NewX#, NewY#

Source Code

These two PSubs work with types and variables.

PSub MoveSpeedDirX2D(X#, Speed#, Dir#)
    NewX# = CosNewValue(X#, Dir#, Speed#)
EndPSub NewX#
PSub MoveSpeedDirY2D(Y#, Speed#, Dir#)
    NewY# = SinNewValue(Y#, Dir#, Speed#)
EndPSub NewY#

This PSub works with variables only.

PSub MoveSpeedDir2D(x#, y#, speed#, dir#)
    NewX# = CosNewValue(x#,dir#,speed#)
    NewY# = SinNewValue(y#,dir#,speed#)
EndPSub NewX#, NewY#

Related Pages

Source Code

Categories: Movement : Source Code