Moving object from point A to B...

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

Valgar(Posted 2003) [#1]
Hallo.
I've a problem.
I have created a "bullet" at position A(examples:100,100) and want to move this bullet at position B(variable....depending on where is another object when i fired it)when i hit mousebutton(1)...
But i don't know how to do it.
In my previous programming language there are some function to move an object from.....to but in blitz there's only "drawimage".
How can i resolve the problem?
I don't want a "homing" bullet,but simply a bullet that when i fire it,it move in the direction of another object,but the direction of the object when the bullet start to move (maybe when the bullet reach it's point,the object is in another point).
Thanks.


Bremer(Posted 2003) [#2]
Well, when you fire the bullet, then store the x and y position of the object you are shooting at and store the x and y of the position you are shooting from.

Then each time you update you check to see if the x and y position of the bullet is equal to the x and y position you stored and if not you either deduct or add depending on whether they are larger or smaller and then draw the bullet in the new position and continue to do so until they reach the target.

I hope that made sense.

function newBullet(x,y,xx,yy)
b.bullet = new bullet
b\x1 = x
b\y1 = y
b\x2 =yy
b\y2 = yy
end function

function updateBullet
if b\x1 < b\x2 then b\x1 = b\x1 + 1
if b\x1 > b\x2 then b\x1 = b\x1 - 1
if b\y1 < b\y2 then b\y1 = b\y1 + 1
if b\y1 > b\y2 then b\y1 = b\y1 - 1
drawimage bulletImg,b\x1,b\y1
if b\x1 = b\x2 and b\y1 = b\y2 then
; what ever you want to do when bullet is there
end if
end function

This is just something I threw together quick, but it mey work :-)


Valgar(Posted 2003) [#3]
Thanks,i try to see if i understand.
b\x1 & b\x2 are the coordinates from i'm shooting.
b\x2 & b\y2 are the coordinates at what i'm shooting at.
The function "newBullet" is called everytime i fire a bullet,so i create a new instance of the bullet each with it's own variable.
When i call the "updateBullet" function i compare the two position and add or subtract 1 (if i want movement of one pixel),and when the two position variable are equal i call an explosion or anything else.

I've understand right?


Valgar(Posted 2003) [#4]
Ok i have incorporated your examples and my code and the result is:
Graphics 640,480,32,1
SetBuffer BackBuffer()

Global sfondo=LoadImage("d:\sfondo.bmp")
Global nemico=LoadImage("d:\torretta.bmp")
Global aereo=LoadImage("d:\jet.tga")
Global bullet=LoadImage("d:\bullet1.bmp")
Global pulsante
Global jetx
Global jety

MidHandle sfondo
MidHandle nemico
MidHandle aereo
MidHandle bullet

Type bullet
	Field x1,y1,x2,y2,durata
End Type



;MAIN

While Not KeyHit(1)

jetx=MouseX()
jety=MouseY()

Cls

DrawImage aereo,jetx,jety
DrawImage nemico,100,100

If MouseHit(1)
	pulsante=1
	create_bullet(100,100,jetx,jety,200)
EndIf
move_bullet()
Flip

Wend

End
;END MAIN


Function create_bullet(xfrom,yfrom,xto,yto,life)
b.bullet = New bullet 
b\x1 = xfrom 
b\y1 = yfrom 
b\x2 =xto
b\y2 =yto
b\durata=life
End Function 

Function move_bullet()
For b.bullet=Each bullet
If b\x1 < b\x2 Then b\x1 = b\x1 + 1 
If b\x1 > b\x2 Then b\x1 = b\x1 - 1 
If b\y1 < b\y2 Then b\y1 = b\y1 + 1 
If b\y1 > b\y2 Then b\y1 = b\y1 - 1 
DrawImage bullet,b\x1,b\y1 
If b\x1 = b\x2 And b\y1 = b\y2 Then Delete b
; what ever you want to do when bullet is there 
;End If 
Exit
Next
End Function 


The problem is that i don't know why start only a bullet at one time even if i hit multiple time the mouse button!
The bullets start to move when each bullet is expired respectively...so only one at time show-up!
And the movement are not "stright" but go up (or down) until the differences of the two y coords are satisfied..after the y coords start to compare the x coords and the missile start to do left(or right).
What i search is a real "line"not a segmented line.
Thanks.


Valgar(Posted 2003) [#5]
Ok,i have made it!
(I'm very happy 'cause i'm not a mathemathical guy hehe)
I admit that i have read some tutorial and some code(in the code archive)and my code isn't mine interely(i think so...).
Here's the code:
Graphics 640,480,32,2
SetBuffer BackBuffer()

Global sfondo=LoadImage("d:\sfondo.bmp")		; a simple sprite
Global nemico=LoadImage("d:\torretta.bmp")		;look up :P
Global aereo=LoadImage("d:\jet.tga")			; ^
Global bullet=LoadImage("d:\bullet1.bmp")		; ^
Global jetx										;declaration of the 2 variable of the jet(at mouse position)...
Global jety										;Not of use now but For future updates

MidHandle sfondo
MidHandle nemico
MidHandle aereo
MidHandle bullet


;the bullet type....the fields looks a little odd but those stuff are only for calculations...
Type bullet
	Field px#,py#,pa#,ex#,ey#,ps#,bs#,a#,p#,e#,ba#,bx#,by#,durata
End Type

Global b.bullet		;i declare global the type so i can access it's variable outside the function where i declare it


;MAIN

While Not KeyHit(1)			;while not esc key is hit

jetx=MouseX()		
jety=MouseY()

Cls

DrawImage aereo,jetx,jety		;the player under mouse position
DrawImage nemico,100,100		;an enemy :)

If MouseHit(1)									;if i hit mousebutton(1) the enemy shoot!
	create_bullet(100,100,jetx,jety,200,25)
EndIf

update_bullet()				;update of all bullets fired

Flip

Wend

End
;END MAIN

Function create_bullet(xfrom,yfrom,xto,yto,life,velocity)
b.bullet = New bullet 
b\durata=life			;of no use now but for future update i use it for simulating a lifespan of 
						;the bullet (examples:it's course it's only 100 Or 200 pixel total...
;initialise the bullet position
b\px#=xto					;player position
b\py#=yto					;player position
b\pa#=Rand(-180,180)		;player angle
b\ex#=xfrom					;enemy position
b\ey#=yfrom					;enemy position
b\ps#=1						;player speed
b\bs#=velocity				;bullet speed....try changin this :P

;calculate routine
b\a#=ATan2(b\ey-b\py,b\ex-b\px)		;used to calculate from 'coordinate system relative' to 'player relative'
b\p#=-b\pa-b\a						
b\e#=ASin((Sin(b\p)/b\bs*b\ps))		;    the main formula....

;get bullet parameters from stuff
b\ba#=-b\a+180+b\e			;bullet angle: calculation return to system relative coords
b\bx#=b\ex					;bullet coordinates
b\by#=b\ey					; ^
End Function

Function update_bullet()
For b.bullet=Each bullet
	b\bx=b\bx+Cos(b\ba)*b\bs*.1		;update bullet according to calculated angle
	b\by=b\by-Sin(b\ba)*b\bs*.1
	DrawImage bullet,b\bx,b\by		;my little bullet... :P
Next
End Function



Very very very thanks to the people who wrote tutorial and code in the archive!
Many thanks


Bremer(Posted 2003) [#6]
I'm not big on math either, and yes the way I described does move the bullet a little funny now that I look at it again. So much for early morning code :-) At least you found a way that works, great.


Valgar(Posted 2003) [#7]
Your code works best for enemies :P
I've tried it and look greath (a bullet is supposed to move straight but an enemy not hehe)