Is there any way to make objects face any angle?

BlitzMax Forums/BlitzMax Beginners Area/Is there any way to make objects face any angle?

fhetskr(Posted 2012) [#1]
I'm back again! So is there any way to make a character object or enemy object move or shoot in any direction? What I'm looking for is how to move something at angles that are not vertical, diagonal, or horizontal. So basically, I want to make objects travel with no limitations on direction.

Please just tell me if there is no way to do this.


GfK(Posted 2012) [#2]
Vectors, really. By that, all you need to store is the player's velocity and direction and with some simple maths, you can rotate and move the player at any angle you want.

Try this - Cursor keys to move:
Strict

Graphics 800,600

Local player:tPlayer = New tPlayer

While Not AppTerminate()
	Cls
	player.update()
	player.draw()
	Flip
Wend	

Type tPlayer
	Field Angle:Float
	Field Speed:Float
	
	Field x:Float
	Field y:Float
	
	Method New()
		Self.x = 400
		Self.y = 300
		Self.Angle = 0
		Self.Speed = 0
	End Method	
	
	Method update()
		If KeyDown(KEY_UP)
			Self.Speed:+0.01
			If Self.Speed >=1
				Self.Speed = 1
			EndIf
		EndIf
		If KeyDown(KEY_DOWN)
			Self.Speed:-0.01
			If Self.Speed <=0
				Self.Speed = 0
			EndIf
		EndIf
		If KeyDown(KEY_LEFT)
			Self.Angle:-1
			If Self.Angle <0
				Self.Angle:+360
			EndIf
		EndIf	
		If KeyDown(KEY_RIGHT)
			Self.Angle:+1
			If Self.Angle >360
				Self.Angle:-360
			EndIf
		EndIf
		Self.x = Cos(Self.Angle) * Self.Speed + Self.x
		Self.y = Sin(Self.Angle) * Self.Speed + Self.y	
	End Method
	
	Method draw()
		DrawOval Self.x-4,Self.y-4,8,8
		DrawLine Self.x,Self.y,Cos(Self.Angle) *20 + Self.x,Sin(Self.Angle) * 20 + Self.y
	End Method
End Type



Midimaster(Posted 2012) [#3]
if you use FLOAT or DOUBLE variable types for X and Y components of an actor, you can move in any direction.

code only symbolic:

Graphics 800,600
Global X#=400,Y#=300,addX#,addY#
Global Angle%=25
Repeat
     Cls
     addX=Sin(Angle)
     addY=Cos(Angle)
     X=X+addX
     Y=Y+addY
    DrawOval X,Y,10,10
    Flip 1
Until KeyHit(Key_Escape)


upps... GFK was faster...

Last edited 2012

Last edited 2012


fhetskr(Posted 2012) [#4]
sorry for the late reply. thanks for the code, this will do nicely. The only question I have is whether or not this works for projectiles. if not, could someone explain how to do that?


Jesse(Posted 2012) [#5]
you need to learn to use Sin Cos and ATan2. People can give you sample code but if you don't understand those simple Trig function you won't get far with the code samples.

Sin and Cos are useful for movement in the direction desired. To move an object 7 units in the desired angle(direction) it's done like this:
x = x + Cos(angle) * 7
y = y + Sin(angle) * 7

the seven can be replaced by a speed variable.

Atan2 is useful for finding the direction a projectile needs to travel to find a target.

the desired angle:

destinationAngle = ATan2(enemyY - gunY, enemyX - gunX)

and you move the projectile like this:

bulletx = bulletx + Cos(destinationAngle) * speed
bullety = bullety + Sin(destinationAngle) * speed

Last edited 2012