Collision Detection

Blitz3D Forums/Blitz3D Beginners Area/Collision Detection

Jellon(Posted 2004) [#1]
Here is my goal: Be able to detect if a player is touching the ground or not. I tried using a height off the ground, but then if the player gets stuck in cone shaped hole, then they can't jump to get out b/c the game thinks the player is off the ground. There is no function I can find that detects if a player is touching a specific object (say terrain) or not. Any ideas?


doctorskully(Posted 2004) [#2]
Just use Collisions; read about this command in your Blitz Docs to get a better idea. Blitz will automatically act upon collision (yay!) so that way your character won't be going through the ground. (Look up EntityRadius and such while you're at it.)

Here's the basic gist: Set up a radius on one mesh and then a radius, box, or nothing on the other. Then assign an EntityType (a number). Use Collisions to set up collision detection between mesh#1 and mesh#2.

To figure out if one mesh is touching another, then use the loop:

For c = 1 to CountCollisions(mesh)

...

Next

During the loop there are all sorts of commands you can use, such as EntityCollided or CollisionEntity and other good stuff. The BlitzDocs do a much better job at explaining this in detail--check 'em out.

No functions? There's at least ten.


Jellon(Posted 2004) [#3]
Yeah, I get the collisions command and how it acts upon it. I read about all that I could. I like to learn for myself instead of bothering others, however, I want to detect air or ground for other purposes, such as you can't start moving while you're in the air jumping. That means if you're on the ground, cause the object's velocity to be 0 when the key isn't held down, but only slowly decress the velocity while in the air. So I need a way to detect in the air or not. I tried using EntityCollided and I can't get it to work. I'm low on time, I'll come back and post my code later. Basically it always has an entitycollided return of zero, even when it is colliding. I know it knows I'm colliding b/c otherwise I would fall through the ground and I double checked that I typed in the correct text.


doctorskully(Posted 2004) [#4]
Common error before even looking at your code--the EntityCollided command is this: EntityCollided(entity,TYPE). Are you SURE you're putting in the TYPE of the entity (the number) and not the entity itself? Sorry, it's real common, thought I ought to mention it.


Jellon(Posted 2004) [#5]
; Collisions
;Player
Const ColType_Zelon=1
Collisions ColType_Zelon,ColType_Zelon,1,1
Collisions ColType_Zelon,ColType_Solid,2,2
;Objects
Const ColType_Solid=2
Collisions ColType_Solid,ColType_Zelon,2,2

Function InAir(Zelon.Zelon)
If EntityCollided(Zelon\Image3D,ColType_Solid)<>0 Then Return True Else Return False
End Function

Yeah, of corse the terrain is a solid and the zelon (the type of being that I have created) is a zelon. I know they are setup right b/c the "Collisions ColType_Solid,ColType_Zelon,2,2" works just fine. But the EntityCollided returns 0 unless I am holding down the movement button to force my zelon into a wall made from terrain. When I do force it to move into the terrain, it returns some incredibly large number, like 256789 or something like that.
Somehow I overlooked the CollisionEntity Command. It will probably suit my need.
Thanks Everyone.


Jellon(Posted 2004) [#6]
Got it:

Function InAir(Mesh)
;Does it touch the terrain?
If EntityCollided(Mesh,ColType_Solid)<>0 Then
For c=1 To CountCollisions(Mesh)
If CollisionEntity (Mesh,c)=Terrain Then Return False
Next
;Prevents being in air while running down slopes
ElseIf EntityY(Mesh)-TerrainY(Terrain,EntityX(Mesh),EntityY(Mesh),EntityZ(Mesh))<3 Then
Return False
Else
Return True
EndIf
End Function