move asteroid in a straight line

BlitzMax Forums/BlitzMax Programming/move asteroid in a straight line

slenkar(Posted 2008) [#1]
im using this code for my asteroids,

Type rock
Global bigrock:timage=LoadImage("incbin::bigrock.png")
Global smallrock:timage=LoadImage("incbin::medrock.png")
Global medrock:timage=LoadImage("incbin::smallrock.png")
Field rotation,x#,y#,health,direction,image:timage,speed
Field xspeed#,yspeed#
Global list:TList

Function Create()
Local r:rock

r:rock=New rock
r.x=Rand(1,640)
r.y=Rand(1,480)
r.health=Rand(1,30)
r.rotation=Rand(1,360)
r.direction=Rand(1,360)
r.speed=Rand(1,3)
r.image=bigrock
r.xspeed:+ Cos(r.direction)*r.speed
r.yspeed:+ Sin(r.direction)*r.speed 
ListAddLast(rock.list,r)
End Function

Function update_all()
Local r:rock
For r:rock=EachIn rock.list
r.update
Next
End Function

Method update()

Self.x=Self.x+Self.xspeed
Self.y=Self.y+Self.yspeed
SetRotation Self.rotation
If Self.x < 0
Self.x = GraphicsWidth()
EndIf
If Self.x > GraphicsWidth()
Self.x = 0
EndIf
If Self.y < 0
Self.y = GraphicsHeight()
EndIf
If Self.y > GraphicsHeight()
Self.y = 0
EndIf

Self.rotation=Self.rotation+1
SetBlend(alphablend)
DrawImage Self.image,Self.x,Self.y
DrawText Self.direction,Self.x,Self.y
End Method

End Type


but my asteroids dont go in straight lines they curve about the screen making it impossible to avoid them.


EDIT - I just realized its because I am rotating the asteroids, but I dont get why their rotation affects their direction, when they are 2 seperate attributes.


Brucey(Posted 2008) [#2]
How about drawing your asteroid from the center-point of it, rather than the top-left.

MidHandleImage() ?

:o)


slenkar(Posted 2008) [#3]
yeah they are already,

the only thing that stops the asteroids going in a straight line is the fact that I am rotating them, which doesnt make sense to me.
As soon as I stop rotating them they are OK, but I am not using rotation in the calculations at all.


Grey Alien(Posted 2008) [#4]
I can't see any calls to MidHandleImage or are you using automidhandle?


slenkar(Posted 2008) [#5]
automidhandle


TaskMaster(Posted 2008) [#6]
Maybe your asteroids are being affected by the gravity of some nearby planet. Maybe they are stuck in orbit around it.

Check your long range sensors. :)


Jesse(Posted 2008) [#7]
you have to use automidhandle before the images are loaded or it will not be applied to them.
I did some mods to your type and I got it to work.
I hope this helps you understand it better:

[edited]


slenkar(Posted 2008) [#8]
thanks! it worked,
I was also having collision problems that went away when I fixed the code.


Grey Alien(Posted 2008) [#9]
knew it was an automidhandle issue! I should asked WHEN you were calling it ;-)


Brucey(Posted 2008) [#10]
Midhandles... who'd have thought it?! :-)