EntityColided?

Blitz3D Forums/Blitz3D Beginners Area/EntityColided?

collision works diffrent

first assign your cubes or whatever to a special collision-type (don't confuse with type variables)

entitytype cube,1
entitytype cube2,2

this means cube is now member of the collision-family 1 and cube2 of family 2.

Now define how these 2 families will react on eachother:

Collisions 1,2,3,3 ; means family1 to family2, sphere-to-box-check,slide2
Well, now we have defined how a Collsion for Cube into Cube2 should be handled, but not visaversa yet, so:
Collisions 2,1,3,3

BTW Unfortunately the Command "Collision" which sets up Collision Handling, is not listed in the "Entity Collision" Command Reference Chapter (simon?), ok.

Whenever you use Sphere to Box Collsion, you need to define their Collsion size and Radius:

EntityRadius cube,1.4
EntityRadius cube2,1.4
entitybox cube,-1,-1,-1,2,2,2
entitybox cube2,-1,-1,-1,2,2,2

The command EntityCollided does NOT need two Entity Handles, but a entityhandle plus a collision-type, so in this example use:

if entitycollided(cube,2)
or
if entitycollided(cube2,1)

and finally you need to call Updateworld to execute the Collisionchecks each frame.



Now everything together:

Graphics3D 800,600
SetBuffer BackBuffer()

camera=CreateCamera()
CameraViewport camera,2,0,800,600

light=CreateLight()

family1=1
family2=2

cube=CreateCube()
PositionEntity cube,0,0,5
entitytype cube,family1
entityradius cube,1.4
entitybox cube,-1,-1,-1,2,2,2

cube2=CreateCube()
PositionEntity cube2,0,2,5
entitytype cube2,family2
entityradius cube2,1.4
entitybox cube2,-1,-1,-1,2,2,2


collisions 1,2,3,3
collisions 2,1,3,3

While Not KeyHit(1)

If KeyDown(200) :MoveEntity cube,0,0.05,0:EndIf 
If KeyDown(208) :MoveEntity cube,0,-0.05,0:EndIf
If KeyDown(203) :MoveEntity cube,-0.05,0,0:EndIf
If KeyDown(205) :MoveEntity cube,0.05,0,0:EndIf

If EntityCollided(cube,family2) Then PositionEntity cube2,Rnd(-2,2),Rnd(-2,2),5 
If EntityCollided(cube2,family1) Then PositionEntity cube,Rnd(-2,2),Rnd(-2,2),5 

TurnEntity cube2,0,.5,0
UpdateWorld()
RenderWorld()
Flip
Wend

End