Rotations

BlitzMax Forums/BlitzMax Programming/Rotations

Twinprogrammer(Posted 2012) [#1]
Hello,

I've been programming a simple shooter and want to know how to get my bullets to move towards the mouse's current position. Here's an example of my code:
Function Updatebullets()
	
	For b:tbullet = EachIn bulletlist
	        'Do I use setrotation ?
		DrawText b.text, b.x, b.y
		b.x: 'What ?
		b.y: 'What ?
	SetRotation(0)
	Next 
End Function



matibee(Posted 2012) [#2]
You don't need setrotation unless you want to align your bullet sprite to its direction of travel. You need to keep track of your bullet direction and move it along that path.

Here is the simplest demo I could knock up...

SuperStrict

Type bullet
	
	Field xdir:float
	Field ydir:Float 
	
	Field x:float
	Field y:float
	
	Global blist:TList = New TList 
	
	Function Create:bullet( x:Float, y:Float, xdir:Float, ydir:float )
		Local n:bullet = New bullet
		n.x = x
		n.y = y
		n.xdir = xdir / 50
		n.ydir = ydir / 50
		blist.addlast( n )
		Return n
	End Function 
	
	Method update()
		x :+ xdir
		y :+ ydir
		If ( x < -10 Or x > 610 Or y < -10 Or y > 610 ) blist.remove( Self )
	End Method 
	
	Function updateall()
		For Local b:bullet = EachIn blist
			b.update()
		Next 
	End Function 
	
	Function drawall()
		For Local b:bullet = EachIn blist
			b.draw()
		Next 
	End Function 
	
	Method draw()
		DrawRect( x - 1,  y - 1, 3, 3 )
	End Method 
End Type 


Global playerX:Int = 300
Global playerY:Int = 400

Graphics 600, 600

While Not AppTerminate()
	bullet.Updateall()

	Cls 
		bullet.Drawall()
		DrawRect( playerX - 5, playerY, 10, 4 )

		If ( KeyDown( KEY_A ) ) playerX = Max( 0, playerX - 5 ) 
		If ( KeyDown( KEY_D ) ) playerX = Min( 600, playerX + 5 ) 

		If MouseDown(1)	bullet.Create( playerX, playerY, MouseX() - playerX, MouseY() - playerY )
	
	Flip 1
Wend 



Jesse(Posted 2012) [#3]
removed Matibee's code is more complete.

Last edited 2012


Twinprogrammer(Posted 2012) [#4]
It works now !


GfK(Posted 2012) [#5]
Instead of Matibee's method (and there's nothing wrong with it if that's how you want to do it), I'd be more inclined to use vectors. That way you only need store the bullet angle and velocity. This will make it much easier if you need to 'steer' the bullets (wind?), or adjust their speed.