Enemy turn arround player position

BlitzMax Forums/BlitzMax Programming/Enemy turn arround player position

hub(Posted 2007) [#1]
Hi !
how to code this :
When distance between the player and the enemy is less than 300 pixels the enemy start turn arround the player. When the player move, it continue to turn arround it ! Thanks for your help and yours ideas !

Just a code to begin
Strict

Graphics 1024,768


Global Enemy_x# = 300
Global Enemy_y# = 400

Global Player_x# = GraphicsWidth() / 2
Global Player_y# = GraphicsHeight() / 2
Global d#
Global angle# = 0.0

MoveMouse Player_x#, Player_y#

While Not KeyDown(KEY_ESCAPE)

	Cls

	SetColor 255,255,255
	DrawOval Player_x#-2, Player_y#-2,4,4
	SetColor 0,255,0
	DrawOval Enemy_x#-2, Enemy_y#-2,4,4
	
	Player_x# = MouseX()
	Player_y# = MouseY()
	
	SetColor 255,0,0
	
	d# = Float ( (Player_x# - Enemy_x#) * (Player_x# - Enemy_x#) + (Player_y# - Enemy_y#) * (Player_y# - Enemy_y#) )
	If d < 300 * 300 Then
		
		If angle# < 360 Then 
			angle# = angle# + 1.0
		Else
			angle# = 0.0
		End If
		
		Local xmov# = Cos (Angle#) * 2.0
		Local ymov# = Sin (Angle#) * 2.0
		
		Enemy_x# = Enemy_x# + xmov#	
		Enemy_y# = Enemy_y# + ymov#		
		
	End If

	SetColor 255,255,255
	DrawText "d=" + Sqr(d),0,0
	
	Flip

Wend



hub(Posted 2007) [#2]
another try. No very smooth and realistic ! How to fix the rotation speed (the same for any distance) ?
Strict

Graphics 1024,768


Global Enemy_x# = 200
Global Enemy_y# = 400

Global Player_x# = GraphicsWidth() / 2
Global Player_y# = GraphicsHeight() / 2
Global d#
Global angle# = 0.0

MoveMouse Player_x#, Player_y#

Local bStartTurn = False

While Not KeyDown(KEY_ESCAPE)

	Cls

	SetColor 255,255,255
	DrawOval Player_x#-2, Player_y#-2,4,4
	SetColor 0,255,0
	DrawOval Enemy_x#-2, Enemy_y#-2,4,4
	
	Player_x# = MouseX()
	Player_y# = MouseY()
	
	SetColor 255,0,0
	
	d# = Float ( (Player_x# - Enemy_x#) * (Player_x# - Enemy_x#) + (Player_y# - Enemy_y#) * (Player_y# - Enemy_y#) )
	If d < 300 * 300 Then
		bStartTurn = True
	End If
	
	If bStartTurn = True
		
		If angle# < 360 Then 
			angle# = angle# + 1.0
		Else
			angle# = 0.0
		End If
		
		Local xmov# = Cos (Angle#) * Sqr(d)
		Local ymov# = Sin (Angle#) * Sqr(d)
		
		Enemy_x# = Player_x# + xmov#	
		Enemy_y# = Player_y# + ymov#		
		
	End If

	SetColor 255,255,255
	DrawText "d=" + Sqr(d),0,0
	
	Flip

Wend




tonyg(Posted 2007) [#3]
You can use atan2 to get the angle to the player.


hub(Posted 2007) [#4]
why use atan2 ?


tonyg(Posted 2007) [#5]
This might explain or this or this .
There's quite a few others as well.
<edit> Might even be this


Jesse(Posted 2007) [#6]
do you mean something like this:

adopted from my guided misile code.


hub(Posted 2007) [#7]
many thanks for this tonyg and jesse.


hub(Posted 2007) [#8]
just need help now to set the speed enemy the same for any enemy-player distance ! please help !

Strict

Graphics 1024,768


Global Enemy_x# = 200
Global Enemy_y# = 400

Global Player_x# = GraphicsWidth() / 2
Global Player_y# = GraphicsHeight() / 2
Global d#
Global angle# = 0.0
Global Speed# = 1.0

Const Pi_div_180# = Pi / 180.00

MoveMouse Player_x#, Player_y#

Local bStartTurn = False

While Not KeyDown(KEY_ESCAPE)

	Cls

	SetColor 255,255,255
	DrawOval Player_x#-2, Player_y#-2,4,4
	SetColor 0,255,0
	DrawOval Enemy_x#-2, Enemy_y#-2,4,4
	
	Player_x# = MouseX()
	Player_y# = MouseY()
	
	SetColor 255,0,0
	
	d# = Float ( (Player_x# - Enemy_x#) * (Player_x# - Enemy_x#) + (Player_y# - Enemy_y#) * (Player_y# - Enemy_y#) )
	If d < 300 * 300 Then
		bStartTurn = True
		angle# = ATan2 (Enemy_y#-player_y#, Enemy_x#-Player_x#)
	End If
	
	If bStartTurn = True
		
		If angle# < 360 Then 
			angle# = angle# + Speed#*360.0/60.0 '60 is FPS
		Else
			angle# = 0.0
		End If
		
		d = Sqr(d)
		If d < 3.0 Then d = 3.0
		
		Local xmov# = Cos (Angle# + Pi_div_180#) * d
		Local ymov# = Sin (Angle# + Pi_div_180#) * d
		
		Enemy_x# = Player_x# + xmov#	
		Enemy_y# = Player_y# + ymov#		
		
	End If

	SetColor 255,255,255
	DrawText "d=" + Sqr(d),0,0
	
	Flip

Wend



hub(Posted 2007) [#9]
My pb is now to set a constant speed (same speed if the enemy is near or far the player position). Have you an idea to do this ?


Jesse(Posted 2007) [#10]
to get the angle movement. you do it like this:
angle = arc/radius (in your case arc being the speed and radius half D)
note: the result will be in radians so you need to convert it back to degrees by multiplying the result by 57.2957.


hub(Posted 2007) [#11]
if i understand this :
angle = angle + speed# / (0.5 * sqr(d)) * 57.2957 ?

when i test, this is not the same speed. Where is my error ?



Jesse(Posted 2007) [#12]
I don't know what you are trying to acomplish but it is a consistent speed. it is doing what is supposed to do. What I am thinking is that you want it to go slower the smaller the radius is. am I correct? remove the cls from your sample and see. All of the steps are about 4 pixels apart. whether a small radius or large. if you want it to go slower/faster you need to find a factor by which to decrease/increase the speed depending on the size of the radius.

I googled the formula just to make shure I remembered it correctly:
http://www.themathpage.com/aTrig/arc-length.htm


hub(Posted 2007) [#13]
you are correct. to go slower the smaller the radius is. But the code produce the contrary for me !

i want that the enemy take the same time to accomplish the circle, this for any radius ! ... hope that this is this that i want ;-) if you have a formulae ... i'm interested !


Dreamora(Posted 2007) [#14]
Same time for any radius means that

2 * Radius * Pi / Desired Time = Speed gives you the actual speed your object needs to have.


hub(Posted 2007) [#15]
with dreamora code, this is not that i want. try it, i'm tired need to sleep ! Time to go to bed for me ! Thanks for your help :!