Going nuts with the enemies

Blitz3D Forums/Blitz3D Programming/Going nuts with the enemies

SkyCube(Posted 2005) [#1]
Hello everyone,

First I want to thank everyone who has answered my previous posts. One of the things I like best about Blitz3D is the community support. I hope to someday be able to give advice to others, too! Well here's my question: I am trying to make an entity that follows another. I must make the enemy face the player entity and follow it. However, if I use PointEntity all enemy entities will be on top of the player quite fast. I want to make the enemy make a gradual rotation until it faces the player. I tried to this with AlignToVector, but maybe I misunderstood what this function does.

I thought AlignToVector made the Entity's axises point the specified location in 3d space. So if the axises point to the player, making the pitch, yaw and roll come down to zero should do the trick, right? It doesn't, however. Does anyone know any other way how this might be accomplished?

Here's my code:

While Not KeyDown(1)

If KeyDown(200) ;up
MoveEntity cube2,0,0,1
End If

If KeyDown(208) ;down
MoveEntity cube2,0,0,-1
End If


If KeyDown(203) ;left
MoveEntity cube2,-1,0,0
End If


If KeyDown(205) ;right
MoveEntity cube2,1,0,0
End If

AlignToVector cube,EntityX(cube),EntityY(cube),EntityZ(cube),1,1
AlignToVector cube,EntityX(cube),EntityY(cube),EntityZ(cube),3,1
AlignToVector cube,EntityX(cube),EntityY(cube),EntityZ(cube),2,1


If EntityRoll(cube) >0 Then TurnEntity cube,0,0,-1
If EntityYaw(cube) >0 Then TurnEntity cube,0,-1,0
If EntityPitch(cube) >0 Then TurnEntity cube,-1,0,0


UpdateWorld
RenderWorld

Text 0,0,Str(EntityPitch(cube))
Text 0,20,Str(EntityYaw(cube))
Text 0,40,Str(EntityRoll(cube))


Flip

Wend


WolRon(Posted 2005) [#2]
You were using AlignToVector incorrectly (aligning cube to itself) and you didn't need the three >0 checks.
Also, if you want it to turn slowly, you need to set a value less than 1 for AlignToVector.
Graphics3D 640, 480
cam = CreateCamera()
light = CreateLight()
MoveEntity cam, 0, 0, -10

cube = CreateCube()
cube2 = CreateCube()
EntityColor cube2, 255, 0, 0


While Not KeyDown(1) 

	If KeyDown(200) ;up 
		MoveEntity cube2,0,0,1 
	End If 
	
	If KeyDown(208) ;down 
		MoveEntity cube2,0,0,-1 
	End If 
	
	
	If KeyDown(203) ;left 
		MoveEntity cube2,-1,0,0 
	End If 
	
	
	If KeyDown(205) ;right 
		MoveEntity cube2,1,0,0 
	End If 
	
	AlignToVector cube, EntityX(cube2), EntityY(cube2), EntityZ(cube2), 1, .01 
	AlignToVector cube, EntityX(cube2), EntityY(cube2), EntityZ(cube2), 3, .01 
	AlignToVector cube, EntityX(cube2), EntityY(cube2), EntityZ(cube2), 2, .01 
	
	
;	If EntityRoll(cube) >0 Then TurnEntity(cube,0,0,-1 )
;	If EntityYaw(cube) >0 Then TurnEntity(cube,0,-1,0 )
;	If EntityPitch(cube) >0 Then TurnEntity(cube,-1,0,0) 
	
	
	UpdateWorld 
	RenderWorld 
	
	Text 0,0,Str(EntityPitch(cube)) 
	Text 0,20,Str(EntityYaw(cube)) 
	Text 0,40,Str(EntityRoll(cube)) 
	
	
	Flip 

Wend
End
What are the forum codes?


SkyCube(Posted 2005) [#3]
Huh? I thought AlignToVector didn't move the entity, just the axises (don't know if "axises" is the right word :)) so I had to set the entity rotation myself! But no, AlignToVector does the rotation itself. Thanks WolRon!

What do you mean by "the forum codes"?


Rook Zimbabwe(Posted 2005) [#4]
Skycube... You know more than you think! Program code looks good! On the forum code you need to type {code} and {/code} but use [ for { and ] for }.... :)
-RZ


WolRon(Posted 2005) [#5]
It's a LINK. You click on it, and it will show you the forum codes that you can use on this website.


GfK(Posted 2005) [#6]
You may find that DeltaPitch() and DeltaYaw() will offer finer control for what you're trying to do.


SkyCube(Posted 2005) [#7]
You are right WolRon, hadn't noticed the link :) I didn't I could do that in the forums.


DeltaPitch()? DeltaYaw()? Haven't seen those in the manual... Are they available with an update? I will check them out, though (maybe I just haven't seen those functions). Thanks again everyone!


big10p(Posted 2005) [#8]
You probably need to update your docs pak. They show up in my online manual.


SkyCube(Posted 2005) [#9]
Thanks big, I didn't know there are updates for the docs too.

OK, I'm still going nuts with the enemies. I managed to get the "enemy" cube to follow me like this:
While Not KeyDown(1)

If KeyDown(200) ;up
MoveEntity cube2,0,0,1
End If

If KeyDown(208) ;down
MoveEntity cube2,0,0,-1
End If


If KeyDown(203) ;left
MoveEntity cube2,-1,0,0
End If


If KeyDown(205) ;right
MoveEntity cube2,1,0,0
End If


AlignToVector cube,EntityX(cube2),EntityY(cube2),EntityZ(cube2),1,0.05
AlignToVector cube,EntityX(cube2),EntityY(cube2),EntityZ(cube2),3,0.05
;AlignToVector cube,EntityX(cube2),EntityY(cube2),EntityZ(cube2),2,0.01
If EntityX(cube) > EntityX(cube2) Then MoveEntity cube,-0.5,0,0
If EntityZ(cube) > EntityZ(cube2) Then MoveEntity cube,0,0,-0.5

If EntityX(cube) < EntityX(cube2) Then MoveEntity cube,0.5,0,0
If EntityZ(cube) < EntityZ(cube2) Then MoveEntity cube,0,0,0.5

UpdateWorld
RenderWorld

Text 0,0,Str(EntityPitch(cube))
Text 0,20,Str(EntityYaw(cube))
Text 0,40,Str(EntityRoll(cube))

If EntityCollided(cube2,1) Then Text 0,60,"Ouch!"
If EntityCollided(cube,2) Then Text 0,60,"Ouch!"

Flip


But that's just a test I am doing. In reality, I need this code to work for 200 enemy entities. Here's the code for that:

For x = 0 To 199
  If EntityInView(malomeshes(x),camera) Then
    active(x) = 1
  End If
Next
;movimiento de los malos
For x = 0 To 199
  If active(x)=1 
    AlignToVector malomeshes(x),EntityX(player),0,EntityZ(player),1,0.05
    AlignToVector malomeshes(x),EntityX(player),EntityY(player),EntityZ(player),3,0.05
  
    If EntityX(malomeshes(x)) > EntityX(player) Then MoveEntity malomeshes(x),-0.5,0,0
    If EntityZ(malomeshes(x)) > EntityZ(player) Then MoveEntity malomeshes(x),0,0,-0.5

    If EntityX(malomeshes(x)) < EntityX(player) Then MoveEntity malomeshes(x),0.5,0,0 
    If EntityZ(malomeshes(x)) < EntityZ(player) Then MoveEntity malomeshes(x),0,0,0.5
  End If

Next 


"malomeshes" is an array containing my 200 enemy entities. The "active" array tells which enemies become active as they enter the player's view. The code is basically the same save for the number of entities and the names of the entities, so why won't it work with the 200 enemies? Any hints for that?

This seems too difficult. Maybe I'm better off using pure trigonometry to do the enemy rotation.


big10p(Posted 2005) [#10]
I haven't tested this code but I think you want something like this:

For x = 0 To 199
  If EntityInView(malomeshes(x),camera) Then
    active(x) = 1
  End If
Next

;movimiento de los malos
For x = 0 To 199

  If active(x)
    ; Calculate vector from enemy to player.
    vec_x# = EntityX(player,1) - EntityX(malomesh(x),1)
    vec_y# = EntityY(player,1) - EntityY(malomesh(x),1)
    vec_z# = EntityZ(player,1) - EntityZ(malomesh(x),1)
    
    ; Gradually align enemy to vector.
    AlignToVector malomeshes(x),vec_x,vec_y,vec_z,3,0.05
		
    ; Move enemy along vector.
    MoveEntity malomeshes(x),0,0,0.5
  End If

Next