Collision Detection
Blitz3D Forums/Blitz3D Programming/Collision Detection
| ||
Is there a way to detect the angle of a collision? Eg: Is it a vertical or horizontal collision. Thanks |
| ||
You can use CollisionNX() etc to work out the collision normal, and if CollisionNY() is bigger than the other two it's vertical otherwise its horizontal. |
| ||
I'm having a problem in that even when two object pass next to each other they are having a collision effect. I want them to be able to pass side by side without causing them to "collide". Any way to do this? |
| ||
Okay I am getting somewhere with this, however there are a few nuances. This is my first experiment with Blitz in quite a long time so excuse the basic nature of it. Here's my code: [CODE] ;;;;;;;;;;;;;;;;;;;; ; ; ; Main starts here ; ; ; ;;;;;;;;;;;;;;;;;;;; Graphics3D 800,600 SetBuffer BackBuffer() Camera = CreateCamera() CameraViewport Camera,0,0,GraphicsWidth(),GraphicsHeight() Block = InitGame() Timer = CreateTimer(10) While Not KeyHit(1) x = 0 y = -2 If KeyHit(203) If Not EntityCollided(Block,1) x = - 2 End If End If If KeyHit(205) If Not EntityCollided(Block,1) x = 2 End If End If If EntityCollided(Block,1) If CollisionNY(Block,1) => CollisionNX(Block,1) Block = NewBlock() Else EntityType Block,3 TranslateEntity Block,x,y,0 EntityType Block,1 End If Else TranslateEntity Block,x,y,0 End If UpdateWorld RenderWorld Flip WaitTimer(Timer) Wend ;;;;;;;;;;;;;;;;;;;; ; ; ; Main ends here ; ; ; ;;;;;;;;;;;;;;;;;;;; Function InitGame() For a = 0 To 20 Step 2 x = -10 + a BottomWall = CreateSprite() PositionEntity BottomWall,x,-20,30 EntityType BottomWall,1 Next For a = 0 To 40 Step 2 y = 20 - a LeftWall = CreateSprite() PositionEntity LeftWall,-12,y,30 EntityType LeftWall,1 RightWall = CreateSprite() PositionEntity RightWall,12,y,30 EntityType RightWall,1 Next Block = NewBlock() Collisions 1,1,1,1 Return Block End Function Function NewBlock() Block = CreateSprite() PositionEntity Block,0,22,30 EntityType Block,1 EntityColor Block,Rand(255),Rand(255),Rand(255) Return Block End Function [/CODE] Cursors move the blocks left and right. The problems are that if you go against the right wall the block stops, but against the left wall it keeps going (I over-ride the collision if it isn't a top-bottom collision, or at least that's the intention). Also if you put the blocks in a certain way one on top of the other, and then one to the right, dropping another block on the right bottom one will cause the new block to go over it rather than on top. If you could shed any light on this, and also perhaps suggest better ways to do what I am doing I would appreciate that. Thanks |
| ||
Okay new code - solved alot of the problems by using cubes and setting the collision radius. However still having problems with it hitting the right wall and stopping, and also if you keep moving it into lines of blocks on the left or right it will stop too. [CODE] ;;;;;;;;;;;;;;;;;;;; ; ; ; Main starts here ; ; ; ;;;;;;;;;;;;;;;;;;;; Graphics3D 800,600 SetBuffer BackBuffer() Camera = CreateCamera() CameraViewport Camera,0,0,GraphicsWidth(),GraphicsHeight() Block = InitGame() Timer = CreateTimer(10) While Not KeyHit(1) x = 0 y = -2 If KeyHit(203) If Not EntityCollided(Block,1) x = - 2 End If End If If KeyHit(205) If Not EntityCollided(Block,1) x = 2 End If End If If EntityCollided(Block,1) If CollisionNY(Block,1) >= CollisionNX(Block,1) Block = NewBlock() Else EntityType Block,3 TranslateEntity Block,x,y,0 EntityType Block,1 End If Else TranslateEntity Block,x,y,0 End If UpdateWorld RenderWorld Flip WaitTimer(Timer) Wend ;;;;;;;;;;;;;;;;;;;; ; ; ; Main ends here ; ; ; ;;;;;;;;;;;;;;;;;;;; Function InitGame() For a = 0 To 20 Step 2 x = -10 + a BottomWall = CreateCube() PositionEntity BottomWall,x,-20,30 EntityType BottomWall,1 Next For a = 0 To 40 Step 2 y = 20 - a LeftWall = CreateCube() PositionEntity LeftWall,-12,y,30 EntityType LeftWall,1 RightWall = CreateCube() PositionEntity RightWall,12,y,30 EntityType RightWall,1 Next Block = NewBlock() Collisions 1,1,1,1 Return Block End Function Function NewBlock() Block = CreateCube() EntityRadius Block,0.5 PositionEntity Block,0,22,30 EntityType Block,1 EntityColor Block,Rand(255),Rand(255),Rand(255) Return Block End Function [/CODE] |
| ||
Worked it out - I just needed to detect whether the Y collision was 1. |