Moving object from point to point...

BlitzPlus Forums/BlitzPlus Programming/Moving object from point to point...

Valgar(Posted 2004) [#1]
Hallo to all.
I'm writing a couple of function for moving object across the screen;the difficult (for me) is that i don't know how to moving an object from poin A to point B.
If i decide that point a is (example) 200,100 and point b is the player position (example) 400,400 , how can i tell my enemy the direction that is going to follow?(all in pixel unit)
Example: enemy x100 y200
player x400 y400
enemy must go from x 100 y 200 to x400 y400 of one pixel at a time.

I think that math is involved (very very bad at that....) something like calculation of the angle of the distance ecc ecc is right?
If not there are other simplier method of doing this?
Thanks in advance.


Valgar(Posted 2004) [#2]
I've also read of look-up table....what are these for?


Valgar(Posted 2004) [#3]
I have searched forr look-up tables and found this piece of code....
But if you cut&paste and execute....when those object have made a movement...i have "array out of bounds" error.....
Here's the code:(don't remember how to paste correctly in the forum...)


;SIN/COS Lookup Table
Dim Sin2#(360)
Dim Cos2#(360)
For loop=0 To 359
Sin2#(loop)=Sin(loop)
Cos2#(loop)=Cos(loop)
Next










Const scrX=320,scrY=240
;------------------------------------
Global r1#,r2#,r3#
;------------------------------------
;setup
Graphics scrX,scrY,32,2
SetBuffer BackBuffer()
;------------------------------------
;loadgrafx
Global backGnd=LoadImage("go.bmp")
Global enemys=LoadImage("enemy.bmp")
;------------------------------------
;loop
While Not KeyDown(1)
Cls
TileImage backGnd,ixTim,iyTim
circleMov()
strightMov()
updownMov()

Flip
Wend
End
;Circle Motion
Function circleMov()
x1=160+Sin2#(r1)*32
y1=64+Cos2#(r1)*32
DrawImage enemys,x1,y1
r1=r1+2.5
End Function
;------------------------------------
;Forward & Backward Motion
Function strightMov()
x2=144+Sin2#(r2)*32
y2=192
DrawImage enemys,x2,y2+2
r2=r2+2.5
End Function
;------------------------------------
;Up & Down Motion
Function updownMov()
x3=144
y3=124+Cos2#(r3)*32
DrawImage enemys,x3,y3+2
r3=r3+2.5
End Function


I think i have this error because when i reach 360 degrees the lookup table must be reset to o.....don't know how!
Thanks.


QuietBloke(Posted 2004) [#4]
If you dont care about the angle but just want to know how much the object has to move in the x and y directions each frame to move at a speed of 1 pixel then you could just do the following ( using the example you gave for the values:

1. Find the total distance in the x and y directions.

xmove = 400 - 200 = 200
ymove = 400 - 100 = 300

2. If you treat this as 2 lines of a right angled triangle then you can find the straight line distance between the start and end position.

distance = SQRT(200 * 200 + 300 * 300 ) = 360.5551

So in a straight line the distance is 360.5551 pixels.

3. If you divide the xmove and ymove by the total distance you will get the distance you have to move in the x and y directions to move 1 pixel distance.

xmove = 200 / 360.5551 = 0.5547
ymove = 300 / 360.5551 = 0.8320

There you have it. If every frame you add xmove and ymove to your sprite x and y position it will move towards your target at a speed of 1 pixel.

I hope that makes sense and is actually correct. Im not at my machine so I cant prove it but Im sure I will be corrected if this is wrong.


Valgar(Posted 2004) [#5]
Very very thanks!!!
Yes that make sense at all :P
I don't know very well this sort of math things (even if they interest me a lot...i think i can buy some math books)
Your piece of information has give me a great boost.
I have corrected the code above (i have reach discovered the error!it was't in the look-up table but in the function determining the angle!).
I have another question:i have read to use sin-cos table to give some speed boost to my code,the function that you tell me "SQRT" is cpu intensive?
Because i use these function to move a lot of object onscreen at the same time.
Here's the correct code:

Dim Sin2#(360)
Dim Cos2#(360)
For loop=0 To 359
Sin2#(loop)=Sin(loop)
Cos2#(loop)=Cos(loop)
Next










Const scrX=320,scrY=240
;------------------------------------
Global r1#,r2#,r3#
;------------------------------------
;setup
Graphics scrX,scrY,32,2
SetBuffer BackBuffer()
;------------------------------------
;loadgrafx
Global backGnd=LoadImage("go.bmp")
Global enemys=LoadImage("enemy.bmp")
;------------------------------------
;loop
While Not KeyDown(1)
Cls
TileImage backGnd,ixTim,iyTim
circleMov()
strightMov()
updownMov()

Flip
Wend
End
;Circle Motion
Function circleMov()
x1=160+Sin2#(r1)*32
y1=64+Cos2#(r1)*32
DrawImage enemys,x1,y1
r1=r1+2.5
If r1=>359 Then r1=0
End Function
;------------------------------------
;Forward & Backward Motion
Function strightMov()
x2=144+Sin2#(r2)*32
y2=192
DrawImage enemys,x2,y2+2
r2=r2+2.5
If r2=>359 Then r2=0
End Function
;------------------------------------
;Up & Down Motion
Function updownMov()
x3=144
y3=124+Cos2#(r3)*32
DrawImage enemys,x3,y3+2
r3=r3+2.5
If r3=>359 Then r3=0
End Function


QuietBloke(Posted 2004) [#6]
OK.. I think I understand what you are after now. The solution I gave would be good if you wanted to move objects along straight lines or where you only need to make the calculation whenever the object target has changed.

If your objects are moving in a circular motion then I think your method is probably the way to go.

Are all your objects going to be using a radius of 32 ?
If they are then you can create your lookup tables to store the values multiplied by 32 and your main code doesnt need to do a multiply for each object.


Valgar(Posted 2004) [#7]
I have made 32 only accidental....
If i make "*32" after "sin(loop)" in the upper portion of the code...my lookup table is going right?
Or i'm making errors???


Valgar(Posted 2004) [#8]
Here i have modified my code to see if it move straight like you tell me...
But the object don't move at all O_O'

Here's the code:

Graphics 800,600,32,2
SetBuffer BackBuffer()

Global go=LoadImage("go.bmp")
Global enemy=LoadImage("enemy.bmp")
MidHandle go
MidHandle enemy
Global xmove2
Global ymove2
Global velocita=1
xpart=100
ypart=100
xdest=600
ydest=300
x=xpart
y=ypart
xmove=xdest-xpart
ymove=ydest-ypart
distance=Sqr(500*500+200*200)


;MAIN
While Not KeyDown(1)
xmove=500/distance
ymove=200/distance
Cls
DrawImage go,xdest,ydest
DrawImage enemy,x,y
x=x+xmove
y=y+ymove
Print distance
Flip
Wend
;END

Don't know where i make errors.


QuietBloke(Posted 2004) [#9]
Sorry about the slow reply I didnt get time last night to check the forums.

Once again Im at work so I cannot test this but I think I can see the problem.
You need to change the code after your ;Main comment to be:

While Not KeyDown(1)
x=x+ xmove
y=y+ymove
Cls
DrawImage go,xdest,ydest
DrawImage enemy,x,y
x=x+xmove
y=y+ymove
Flip
Wend
;END

I hope that fixes it for you :)


Valgar(Posted 2004) [#10]
Sorry for bothering you,but i already tryiedn to change into what you tell me but don't work....
And if i never used the distance between object i don't know where to go.


Réno(Posted 2004) [#11]
;taget A to B

x1#=300.0;A position X
y1#=300.0;A position Y

x2#=0.0;B position X
y2#=0.0;B position Y

x#=0.0;bullet position X
y#=0.0;bullet position Y
speed#=1.0;bullet speed

angle#=0.0;angle


;game loop

angle=ATan2((y2 - y1),((x2 - x1));do it just one time

x=(x+Cos( angle ))*speed)
y=(y+Sin( angle ))*speed)

drawblock yourimage,x,y


Valgar(Posted 2004) [#12]
Ok this works,but i notice that when the object is near the end accelerate itself O_O'


Réno(Posted 2004) [#13]
You must use floats !!


Valgar(Posted 2004) [#14]
Yes i have used float.
But it seems to me that accelerate itself near the end....maybe it's my impression.