Panda3d Collisions made simple

I packed a bunch of snippets showing how panda3d collisions works, split in ready to go standalone scripts, organized with progressive difficulty were you should learn the basics for prepare setups to make your scripts react when models collide each other, how to settle those models in a script and also in Blender3D.
Here a small summary of each step you’ll find in the package:

[color=blue]-- Beginner level
[size=100][color=olive]A first approach with Panda3D collisions[/size]
0) quick startup

  1. first very simple setup
  2. simple setup with masking discrimination
  3. collision events
  4. collision events w/ masking
  5. mouse picking
  6. more mouse picking
  7. cheap collisions with isInVIew
  8. panda physics overview
  9. mouse picking in 2D space

[color=green]-- Intermediate level
[size=100][color=olive]Deeper into the topic[/size]

  1. steering an avatar on uneven ground
  2. avatar on uneven ground colliding w/ walls
  3. gravity collider
  4. collision mesh octree

[color=red]-- Advanced level
[size=100][color=olive]Real-world application samples[/size]

  1. real-world use of isInVIew: cheap intruder detection
  2. 2.5D collisions: colliding 2D & 3D objects
  3. target shooter minigame - simple physics ballistic (sorta)

You may snatch all the stuff, scripts and the blender sources, clicking the following links:
BEGINNER PACKAGE
INTERMEDIATE PACKAGE
ADVANCED PACKAGE

[color=indigo][size=150]Pieces Of Advice:[/size]

  • This tutorial is devised to be attended in conjunction with the Blender3D modeler files, therefore if you’re accustomed with a different one, i.e. maya or 3d studio max, I suggest you to download Blender3D nonetheless, study how the sources are structured and then try to reproduce in your beloved modeler.
  • So far the Chicken exporter do not correctly export collider nesting deeper that 1 level, therefore the descend feature stops there. Check here for more.

Beside this stepped tutorial is actually finished it is always perfectible, therefore drop a post to this thread if you find issues and questions blocking you to get what’s going on in a particular point of the tutorial or blender sources.

Great! This is exactly the kind of thing I could use. I’ve been afraid of collisions for a while, but looks like I have no choice now.

I don’t understand this part of the code:

#** this is where will be periodically tested the collision
def traverseTask(task=None):
  for i in range(cHandler.getNumEntries()):
    entry = cHandler.getEntry(i)
    name = entry.getIntoNode().getName()
    dlight.setColor(VBase4(0.8, 0.8, 0.8, 1))
    if task: return task.again

  dlight.setColor(VBase4(0.0, 0.0, 0.0, 1))
  if task: return task.again

The top block is skipped if range(cHandler.getNumEntries())==0, right? But then what is the point of entry and name? I # them out, and the program still works. Is it for (potentially) checking to see what different objects collided with the smiley balloon?

Also, wouldn’t it be clearer if you masked your heart with a LOVE_MASK like the red smiley instead of just a 1?

how less better had you learn it if I ain’t put ‘on purpose’ those litl mistakes, eh?
jokes apart your points are correct, and now I corrected, commented more that part you noted and repacked the zip - keep posting if you find how to improve or find other stuff to fix.

I would like to see a pusher in the next version, like a sad face that you can push around. Maybe even a physics collision would be cool. With that you’ll have covered most of the beginning problems people have with collisions.

Thanks for putting this together. It helps a lot! When its done I’ll fix the grammar for you. I wish there were more collision examples available. This one could be really good (haha, maybe even ‘official’ tutor material :wink: ).

well good to know that - I feared was too thin to be useful so I’m glad for it.

I don’t get: “when it’s done” what? if you referring to the package it is already fixed and uploaded yesterday.

By the way try to speak what you got in mind to cover on the subject so I’ll see to fix an ‘advanced’ sample later.

Sorry, I saw your changes. I meant that when a bunch of cool things like pushers and physics are added in.

ah ok, cool - that pushers thing is something I’m actually missing so I guess will be quite interesting for me as well.

today I reedited the sample splitting it in 2 pieces and commenting out a lot much for the very beginners hoping to make it clearer than before.

I like the new two item setup! There’s a problem, however, that will get people confused regarding your from and into objects. You make your hearts ‘from’ objects and your faces ‘into’ objects. The manual says to make the moving objects ‘from’ objects. I know this is because of your camera trick, but you should note that in the code. Otherwise, it’s a very good example. Thanks for the new version!

well thankyou again - about the from into issue, did YOU get confused? I ask you cos all is commented with fine grain detail so, unless I used bad english, I ain’t fear of that but lemme know otherwise.

#* to have collsions, the engine uses the model geometriy but to optimize the execution time is common practice to use simplified geometries - in our heart egg file we have provided a simple sphere to use for this purpose (look into he blender source to know what I mean) and so we'll gather it from there...
c = heartTrigger.find("**/collider")

Why is the method faster?

JB

cos’ simpler is better
that means to make collision detection on lower polycount surfaces and simpler geometries is less CPU demanding - anyhow here you’ll find a finer explanation: panda3d.org/manual/index.php/Collision_Solids

Today I updated and added 2 more samples to the pack - the new samples do the same things as the formers but in a different, clean and more efficient way using collision events instead to read from a collision queue. Check them out downloading the pack off the first post and of course report what’s wrong or that may be done better.

Another addition of two steps - this time you’ll see how to use collision events for mouse picking using a little more fancy string patterns.

Thanks astelix for the code. I’m trying to understand how collisions work and your example makes a fine sandbox.

Just a typo fix, if you allow me. The global variable pickingEnabledObject misses its ‘b’ (pickingEnabledOject) in the step6.py file. It does not affect it in anyway.

ok thanks
btw I’m open for requests for expand the snippet chain so let me know if you got in mind other collision topics to cover

today I overhauled the whole stuff and added a couple of snips talking about wall and floor collisions - grab and see if interested

WOW! This stuff is great. Thanks astelix!

When could we include this in the samples?

hey thankyou guys for the appreciation
treeform I guess is still incomplete to find a place in the official samples bunch - to me need at least to put in some ‘fluid’ sample and the gravity collider handler. Anyhow I think soon to fill the gaps.