EntityColided?
Blitz3D Forums/Blitz3D Beginners Area/EntityColided?
| ||
Im new to 3D and I have been trying to figure out how to make 2 cubes collide in some way.Graphics3D 800,600 SetBuffer BackBuffer() camera=CreateCamera() CameraViewport camera,2,0,800,600 light=CreateLight() cube=CreateCube() PositionEntity cube,0,0,5 cube2=CreateCube() PositionEntity cube2,0,2,5 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,cube2) Then PositionEntity cube2,Rnd(-2,2),Rnd(-2,2),5 TurnEntity cube2,0,.5,0 UpdateWorld RenderWorld Flip Wend End If EntityCollided(cube,cube2) Then PositionEntity cube2,Rnd(-2,2),Rnd(-2,2),5That is probably way wrong, but there was no example in the command reference. I was trying to make it so when you collide with the spinning cube it will be randomly placed on the screen, but that didn't work. How do you make 2 cubes collide? -Thanks |
| ||
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 |
| ||
Here's your code example modified to do what you want.Graphics3D 800,600 ; Set collision values ground=1 ;Use for ground collisions character=2 ;Add this line for block to block collision scenery=3 ;Use for scenery collisions SetBuffer BackBuffer() camera=CreateCamera() CameraViewport camera,2,0,800,600 light=CreateLight() cube=CreateCube() PositionEntity cube,0,0,5 EntityType cube,character ;Add this line cube2=CreateCube() PositionEntity cube2,0,2,5 EntityType cube2,character ;Add this line Collisions character,character,2,1 ;This line says stop 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 ;EntityCollided ( entity,Type ) If EntityCollided(cube,character)=cube2 Then PositionEntity cube2,Rnd(-2,2),Rnd(-2,2),5 ;Change this line TurnEntity cube2,0,.5,0 UpdateWorld RenderWorld Flip Wend End |