move asteroid in a straight line

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

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.

How about drawing your asteroid from the center-point of it, rather than the top-left.

MidHandleImage() ?

:o)

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.

I can't see any calls to MidHandleImage or are you using automidhandle?

automidhandle

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. :)

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:
Incbin "bigrock.png"
Type rock
	Global bigrock:timage
	Global smallrock:timage
	Global medrock:timage
	Field rotation,x#,y#,health,direction,image:timage,speed
	Field xspeed#,yspeed#
	Global list:TList = CreateList()

	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 Loadimages()
		AutoMidHandle(True)
		bigrock=LoadImage("incbin::bigrock.png")
		smallrock=LoadImage("incbin::medrock.png")
		medrock=LoadImage("incbin::smallrock.png")
	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 image,Self.x,Self.y
		SetRotation 0
		DrawText Self.direction,Self.x,Self.y
	End Method

End Type
Graphics 800,600
rock.loadimages()
Local asteroids:rock = New rock
For Local x% = 1 To 10
	asteroids.Create()
Next

Repeat
	Cls
	asteroids.update_all()
	Flip()
	
Until KeyDown(key_escape)

[edited]

thanks! it worked,
I was also having collision problems that went away when I fixed the code.

knew it was an automidhandle issue! I should asked WHEN you were calling it ;-)

Midhandles... who'd have thought it?! :-)