Entity Collision

Everything related to 3D programming
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Entity Collision

Post by Olby »

Hi,

After so many useful commands were added to PB 3D 5.00 I just could not resist and wanted to give it another try.
The first thing I noticed was the omission of all previous collision event commands. How are we supposed to check for entity collisions without doing the tedious Entity Collide for every possible combination? :( Is there something I overlooked as I cant seem to find any way to just trigger impact sounds, for instance, when a box is kicked about in a 3d room. Information for every collision event such as entities involved, collision point in space, collision normal, velocities etc. would be really useful. I cant think of any average 3d game that would not require such data.

Please let me know if there is a way to do all of the above with the current version of PB.

Thanks.
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Entity Collision

Post by applePi »

Hi Olby
searching the word "coll" in Examples\3D i have found several examples dealing with collision, EntityColide, WorldDebug, FPSFirstPerson, ThirdPerson, TerrainPhysics.
look at ThirdPerson example the man collide with the boxes and the boxes moves. in the FPSFirstPerson.pb example the shooter aim at the barrels and when the ball collide with it the barrels fall down and he also can shoot to other balls and it moves and reflects as physics dictate.
they use RayCollide function and this example can be used to design an exact billiard ball game.
i don't know anything about games. but looking at the examples, here is a ball falling to the ground and it gives a sound when touching the ground.
first you define physics:
EnableWorldPhysics(1)
EntityPhysicBody(#plane,#PB_Entity_StaticBody)
EntityPhysicBody(sphere,#PB_Entity_SphereBody ,2, 5, 0.5)
If EntityCollide(sphere,#plane)
PlaySound(collision)

note also WorldGravity(-9); change gravity to -2 so the ball fall slower, change it to 1 and the ball will rise up (repulsive gravitation)
you need to download this volleyball sound:
http://www.mediafire.com/?gppre763ec5x9fa
try other objects like box, cylinder etc. look at FPSFirstPerson.pb and how the balls reflects from each other, you can add sounds when they reflect like the example beclow
PS: i have failed to see where to insert PlaySound(collision) in the FPSFirstPerson.pb example so it sounds when a collison happened, the example are too advanced. but if we insert PlaySound(collision) after line 239 ApplyEntityImpulse(Shoot...) it will sound at every left click ie when throwing balls

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()
InitSound()
#plane = 8

mainwin = OpenWindow(#PB_Any, 0, 0, 640, 480,"Low Gravity Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(mainwin),0, 0, 640, 480)
Add3DArchive("/", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,30))
camera = CreateCamera(#PB_Any,0,0,400,400)
MoveCamera(camera,0, 4, 9,#PB_Absolute)
CameraLookAt(camera,0, 2, 0)
;CameraRenderMode(camera,#PB_Camera_Wireframe)

CreateMaterial(0, LoadTexture(0, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(0))

sphere=CreateSphere(#PB_Any,1,256,256)
;sphere=CreateCube(#PB_Any,2)
sphere=CreateEntity(#PB_Any,MeshID(sphere),#PB_Material_None,3,6,-4)
EnableWorldPhysics(1)

EntityPhysicBody(#plane,#PB_Entity_StaticBody)
EntityPhysicBody(sphere,#PB_Entity_SphereBody ,2, 5, 0.5)
Result.f = WorldGravity(-9.8)
;Debug Result
RotateEntity(#plane,0,0,9)
MoveEntity(#plane,0,0,0)

LoadSound(collision,"volleyball.wav",100)
;PlaySound(collision,#PB_Sound_Loop)

Repeat
  If EntityCollide(sphere,#plane)
    PlaySound(collision)
    
  EndIf
    
  RenderWorld()

  FlipBuffers()
  
Until WaitWindowEvent(5)=#PB_Event_CloseWindow
FreeSound(collision)
End
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: Entity Collision

Post by Olby »

Thanks for your reply applePi.

EntityCollide() works just fine, but it's useless in "real game scenario". Imagine a 3D world where you have hundreds maybe thousands of entities at the same time. Some of them are colliding at any given moment and physics engine knows exactly when and what is going on in the simulation. Currently you don't have access to that information. There used to be commands such as:
added: ExamineWorldCollisions
added: NextWorldCollision
added: CheckWorldCollisionEntity
added: FirstWorldCollisionEntity
added: SecondWorldCollisionEntity
But now they're gone since 5.00. So the point is, there is no way to create densely populated 3D worlds unless a full collision event system is implemented. EntityCollide() is like working blindfolded, it's just not practical to examine collision state of every possible entity to entity combination.
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Entity Collision

Post by Fred »

It's true, we will take a look at it
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: Entity Collision

Post by Olby »

Thanks Fred, it would really help to develop 3D side of PB.
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
User avatar
A.D.
User
User
Posts: 98
Joined: Tue Oct 06, 2009 9:11 pm

Re: Entity Collision

Post by A.D. »

When do we have these commands back to Purebasic? I hope to have them back soon.
Repeat
PureBasic
ForEver
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Entity Collision

Post by Bananenfreak »

+1

If a project contains a few 100 Entities, you can´t check every Entity to the others with "EntityCollide(sphere,#plane)".
Image
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: Entity Collision

Post by Olby »

Indeed, I've lost interest in 3D library until they can return with a decent collision command set.
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
Post Reply