[bb] Enemy chasing with collision by Jeppe Nielsen [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : Enemy chasing with collision
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Shows how enemies can chase a target, without sliding into same position. No blitz collisions used, only math.

Code :
Code (blitzbasic) Select
Graphics3D 800,600,16,2

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

;create player entity
player=CreateSphere()
EntityFX player,1

;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

ce=CreateCube()

cvel=vel

PositionEntity ce,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(ce))
dy#=(y-EntityY(ce))
dz#=(z-EntityZ(ce))

;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 ce,dx*cvel,dy*cvel,dz*cvel

For cc.creature=Each creature

;do not test against it self
If cc<>c

;vector to another enemy
dx#=(EntityX(cce)-EntityX(ce))
dy#=(EntityY(cce)-EntityY(ce))
dz#=(EntityZ(cce)-EntityZ(ce))

;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 ce,-dx*cvel,-dy*cvel,-dz*cvel

EndIf

EndIf

Next

Next

End Function


Comments :


puki(Posted 1+ years ago)

 I like that "Jeppe" - reminds me of "Ross C's" one.I can imagine the little white sphere shouting "Chase me, Chase me.".


Rob Farley(Posted 1+ years ago)

 That's really cool, feels like gauntlet!


Zach3D(Posted 1+ years ago)

 You stole this someone, it was already posted a long time ago


Damien Sturdy(Posted 1+ years ago)

 <div class="quote"> You stole this someone, it was already posted a long time ago </div>Dipstick :P Look at the date of the original post! LOL :)


Grey Alien(Posted 1+ years ago)

 lol. "oops"


Leiden(Posted 1+ years ago)

 Haha Grey Alien ;)