Moving a character/object in any given direction

Started by kevin, March 20, 2009, 11:02:04 PM

Previous topic - Next topic

kevin

 
Moving a character/object in any given direction


 This version uses TRIG
PlayBASIC Code: [Select]
; run this program really slow so we can see it
Setfps 10


// how fast should it move
Speed =5


; run the whole thing in a loop..
Do

// Pick angle to move in at random (0 to 360 degrees)
Angle#=rnd(360)

// Plot the distance we're moving from the origin point.
For Distance=0 to 200 step speed

// clear the screen
Cls 0

// calc the X/y# coordinate from the origin.
x#=400+cos(angle#)*Distance
y#=300+sin(angle#)*Distance

// draw the origin point
centertext 400,300-20,"Start POint"
circlec 400,300,5,true,rgb(255,0,0)

print "Angle:"+str$(Angle#)+" Degrees"

Circle x#,y#,5,true
centertext x#,y#-25,"Distance From Start"+Str$(Distance)

Sync
next
loop





 Here's a version using LERP (linear interpolation) function.

PlayBASIC Code: [Select]
   // Force the program to run slower so we can see what's it's doing
Setfps 20

// Start of the progtrams main DO/LOOP
Do

// Randomly Pick our objects Starting point
OriginX#=Rnd(GetScreenWidth())
OriginY#=Rnd(GetScreenHeight())

// Randomly Pick the objects destination point
DestX# =Rnd(GetScreenWidth())
DestY# =Rnd(GetScreenHeight())


// run through and plot the objects movement along this path
for lp=0 to 100

// Clear the screen to black
cls rgb(0,0,0)


// DRaw Starting point
Circlec OriginX#,OriginY#,5,true,rgb(255,255,255)

// DRaw Destination point
Circlec DestX#,DestY#,5,true,rgb(255,255,255)

// Draw line to represent the path the object is taking
Linec OriginX#,OriginY#,DestX#,DestY#,rgb(0,0,200)


// Calc a point along this path at a given percentage
x#,y#= Lerp(OriginX#,OriginY#,DestX#,DestY#, lp/100.0)

// point the current posiiton
Circlec X#,Y#,5,true,rgb(255,0,0)

// Show the screen to the user
Sync

// Continue this FOR/NEXT loop
next

// Jump back to the DO startment to keep this program running
loop

Psub Lerp(OriginX#,OriginY#,DestX#,DestY#, Scaler#)
Scaler#=ClipRange#(Scaler#,0,1)
x#=OriginX#+((DestX#-OriginX#)*Scaler#)
y#=OriginY#+((DestY#-OriginY#)*Scaler#)
Endpsub x#,y#







hartnell

Semi related : lengthdir_x( ) and length_dir( ) are exceedingly useful functions in GM (gm sucks!) that PB doesn't have.

http://gamemaker.wikicomplete.info/gml:lengthdir_x

-- hartnell

kevin

#2
 Considering , finding the length is as simple subtracting the Origin from the dest..

LengthX=DestX-OriginX
LengthY=DestY-OriginY
Length = Sqrt((LengthX*LengthX)+(LengthY*LengthY) )

hartnell

In these functions, you already know the length. :)

They are used to place a game object at a specific distance in a direction from a point.

It makes gyruss-like orbital movement a breeze :


if(keyboard_check(vk_left))
{
    dir -= 2;
}
if(keyboard_check(vk_right))
{
    dir += 2;
}

image_angle = point_direction(x,y,320,240);
x = lengthdir_x(220,dir) + 320
y = lengthdir_y(220,dir) + 240


It also makes any kind of positional offset natural and easy.

This is the kind of math function that gives the same kind of improvement as GetDistance2D() does.

Simple, easy to use, makes code more readable, and doesn't scare people who are scared of math.

-- hartnell

kevin

#4
There's function that already do this.  But you could just wrap them.  

PlayBASIC Code: [Select]
Psub LengthDir_X(RAdius#,Angle#)
Dist#=CosRadius(Angle#,Radius#)
EndPsub Dist#

Psub LengthDir_Y(RAdius,Angle#)
Dist#=SinRadius(Angle#,Radius#)
EndPsub Dist#





PlayBASIC Code: [Select]
   if keyboard_check(vk_left) then   dir = dir-2
if keyboard_check(vk_right) then dir = dir+2

RotateSprite ThisSprite,DIR
PositionSprite ThisSprite, CosNewValue(320,dir,220),SinNewValue(320,dir,220)





hartnell

Thanks! :)

The problem is the name. It's geared towards people who already know the math. The average noob just things, "length/distance from in a direction". I'll point to these functs in my docs.

-- hartnell

kevin


   Regardless of what a function is named, the user still has to understand the usage, in order to use it.