Source Code: MoveTowards2D( )
rating: 0+x

MoveTowards2D - move towards a point

Syntax

NewX#, NewY# = MoveTowards2D ( X#, Y#, X2#, Y2#, Speed# )

  • NewX# - new calculated X position
  • NewY# - new calculated Y position
  • X# - current X position
  • Y# - current Y position
  • X2# - current X position of point to move towards
  • Y2# - current Y position of point to move towards
  • Speed# - speed of movement

Explanation

MoveTowards2D() calculates a new X/Y position based on speed in the direction towards a point.

Examples

Move Towards a Point

The example below uses MoveTowards2D() to move towards a point. In this case, the point is the mouse cursor.

SetFPS 60

ObjectX# = 0
ObjectY# = 0

Do

ObjectX#, ObjectY# = MoveTowards2D( ObjectX#, ObjectY#, MouseX(), MouseY(), 2 )    
    Cls 0
        Circle ObjectX#, ObjectY#, 10, False
    Sync
Loop

PSub MoveTowards2D(X#, Y#, X2#, Y2#, Speed#)
    Dir# = GetAngle2D(X#,Y#,X2#,Y2#)
    NewX# = CosNewValue(X#,Dir#,Speed#)
    NewY# = SinNewValue(Y#,Dir#,Speed#)
EndPSub NewX#, NewY#

Source Code

This version works with types and variables.

PSub MoveTowards2D(X#, Y#, X2#, Y2#, Speed#)
    Dir# = GetAngle2D(X#,Y#,X2#,Y2#)
    NewX# = CosNewValue(X#,Dir#,Speed#)
EndPSub NewX#
PSub MoveTowards2D(X#, Y#, X2#, Y2#, Speed#)
    Dir# = GetAngle2D(X#,Y#,X2#,Y2#)
    NewY# = SinNewValue(Y#,Dir#,Speed#)
EndPSub NewY#

This version works with variables.

PSub MoveTowards2D(X#, Y#, X2#, Y2#, Speed#)
    Dir# = GetAngle2D(X#,Y#,X2#,Y2#)
    NewX# = CosNewValue(X#,Dir#,Speed#)
    NewY# = SinNewValue(Y#,Dir#,Speed#)
EndPSub NewX#, NewY#

Related Pages

Source Code

Categories: Movement : Source Code