GLBasic forum

Main forum => GLBasic - en => Topic started by: Foo on 2009-Oct-05

Title: Move from point A to B
Post by: Foo on 2009-Oct-05
Hello,

I would like to move an object from point A to B. I don't know how, but sure something with sin and cos...
Code (glbasic) Select

TYPE TObject
x = 0
y = 0
speed = 0
direction = 0
ENDTYPE

LOCAL obj AS TObject

GETSCREENSIZE sx, sy
obj.x = sx/2
obj.y = sy/2

obj.speed = 5 //Five pixels per loop
obj.direction = 0 // 0 degree, right direction
LIMITFPS 10

destinationX = obj.x + RND(50)
destinationY = obj.x + RND(50)

WHILE TRUE
INC tick, 1
PRINT "Tick:" + tick, 10, 1
// Big guess:
// How can I move the obj to destinationX AND destinationY with a 5 pixel speed every tick
// ??????????????????????????????
IF obj.x = destinationX THEN CONTINUE
IF obj.y = destinationY THEN CONTINUE
SETPIXEL(obj.x, obj.y, RGB(255, 0, 0))
SHOWSCREEN
WEND


Can anybody help?
Title: Re: Move from point A to B
Post by: MrTAToad on 2009-Oct-05
Take a look at : http://www.glbasic.com/forum/index.php?topic=1389.msg9091#msg9091 (http://www.glbasic.com/forum/index.php?topic=1389.msg9091#msg9091)
Title: Re: Move from point A to B
Post by: Moru on 2009-Oct-05
Argh, you beat me to it :-)
Title: Re: Move from point A to B
Post by: Foo on 2009-Oct-05
Think thats not the answer, as far as I understand:

QuoteNow we have an "X" moving from X=100 to X=300 at constant speed. Great!
However, most of the times you need the speed non-constant. When you
push your game logo into view, you might want it starting fast and slow
down to a halt.
He is talking about game logos and accleration - not really moving an object from A(startX, startX) to B(endX, endY)


Title: Re: Move from point A to B
Post by: MrTAToad on 2009-Oct-05
Logo's are just an example.  An alternative is at http://www.miscthings.co.uk/LineMove.gbas (http://www.miscthings.co.uk/LineMove.gbas)
Title: Re: Move from point A to B
Post by: Foo on 2009-Oct-12
Hmm,

without comments I am not able to understand that code. :/

Anyway I found a proper solution - taken from source code from a java game engine.
Title: Re: Move from point A to B
Post by: FutureCow on 2009-Oct-16
MrTAToad - what about putting a few line description of what the code does at the top of it with an example of how to call it for everyone's benefit?
Also, have you thought about changing it to allow for accelleration / decelleration rather than constant speed? Just a thought! :D