AI and collisions

Blitz3D Forums/Blitz3D Programming/AI and collisions

Tyler(Posted 2004) [#1]
I've got a command that creates a creature when you press a button, and it pretty much just drops it right where you are when you press it. What I want to know is, how do I keep all those moving AI components (chasing the player after created) from running into each other and becoming one entity eventually, graphically horrible? I got each one on a sphere-based sliding collision, but it doesn't work with moving things. Thanks in advance!


jhocking(Posted 2004) [#2]
Sphere to sphere collision works fine with moving objects. I'm guessing your problem is that the entities are overlapping already when you create them. You said that new entities are created right on top of the player. Collision detection won't work with entities which are already overlapping, only with entities which start out apart. So you'll have to change your code so that new entities are created offset from each other.


poopla(Posted 2004) [#3]
A: Use tokomak or some other solution to simply solve it by having collisions protect them from merging.

B: Each loop check distances between each of the monsters, if they're withing a threshhold, move them away by a percentage of the threshhold they managed to break.

[Edit] That's very true, spheres whouldnt move through one another m8. So standard blitz collisions should do this for ya :).


Jeppe Nielsen(Posted 2004) [#4]
Just wrote this, hope it is of some use. It dosnt use blitz collision system, but a simple math rutine instead:

Graphics3D 800,600,16,2

cam=CreateCamera()
RotateEntity cam,90,0,0
PositionEntity cam,0,50,0

;create player entity
player=CreateSphere()

;create 10 randomly positioned creatures, with random velocity aswell
For n=1 To 10
	creaturenew(Rnd(-20,20),0,Rnd(-20,20),Rnd(0.1,0.5))
Next

Repeat
	
	;make the creatures home in on player
	creatureupdate(EntityX(player),EntityY(player),EntityZ(player))
	
	;control player with mouse
	PositionEntity player,MouseX()/10-30,0,-MouseY()/10+30
	
	RenderWorld
	
	Text 400,0,"Control player with mouse",1,0
	Text 400,20,"Creatures will not slide into each other",1,0
	
	
	
	Flip
	
	
Until KeyDown(1)
End

Type creature

Field e ;entity

Field vel# ;velocity

End Type

Function CreatureNew(x#,y#,z#,vel#)
	
	c.creature=New creature
	
	c\e=CreateCube()
	
	c\vel=vel
	
	PositionEntity c\e,x,y,z
	
End Function

Function CreatureUpdate(x#,y#,z#,size#=2) ;size is 2 here as a created cube is 2 units wide
	
	For c.creature=Each creature
	
		;vector to x,y,z
		
		dx#=(x-EntityX(c\e))
		dy#=(y-EntityY(c\e))
		dz#=(z-EntityZ(c\e))
		
		;length of vector
		
		l#=Sqr(dx*dx+dy*dy+dz*dz)
		
		;make the vector a unit vector, length = 1
		
		dx=dx/l
		dy=dy/l
		dz=dz/l
		
		TranslateEntity c\e,dx*c\vel,dy*c\vel,dz*c\vel
		
		For cc.creature=Each creature
			
			;do not test against it self
			If cc<>c
			
				;vector to another enemy
				dx#=(EntityX(cc\e)-EntityX(c\e))
				dy#=(EntityY(cc\e)-EntityY(c\e))
				dz#=(EntityZ(cc\e)-EntityZ(c\e))
				
				;length of vector
				
				l#=Sqr(dx*dx+dy*dy+dz*dz)
				
				
				;enemies collide if they er within range:
				
				If l<size
					
					;make the vector a unit vector, length = 1
					
					dx=dx/l
					dy=dy/l
					dz=dz/l
					
					TranslateEntity c\e,-dx*c\vel,-dy*c\vel,-dz*c\vel
					
				EndIf
					
			EndIf
			
		Next
		
	Next
	
End Function