too many bullet objects?

Hello everybody!

This is my first post and I have been playing with panda3d for about 1 month! I am amazed by the community and capabilities of the engine!

I am currently working on (yet another) a space game. I am using bulletphysics to calculate my collisions and am quite happy with the results. But! Now I want to have a looottt of asteroid and I am having performance issue for as few as 200 asteroids. I am now wondering if I do something wrong or if I am not using the best tool for my needs (basic collision detection would be sufficient in my case and bullet might be overkill). Here is how I load an asteroid:

I first load the asteroid.egg in the bullet setup and call the create_asteroid function:

self.asteroid_shape = loader.loadModel('models/asteroid.egg')
for i in range(0, 200):
	self.create_asteroid()       

then I create the asteroids

def create_asteroid(self):		
	x=randint(-1000,1000)
	y=randint(-1000,1000)
	z=randint(-1000,1000)
	radius = 12
	shape = BulletSphereShape(radius)
	self.asteroid = self.worldNP.attachNewNode(BulletRigidBodyNode('asteroid'))
	self.asteroid.node().setMass(120)
	self.asteroid.node().addShape(shape)
	self.asteroid.setPos(x, y, z)
	self.asteroid.setH(20.0)
	self.asteroid.node().notifyCollisions(True)
	self.asteroid.setCollideMask(BitMask32.allOn())
	self.world.attachRigidBody(self.asteroid.node())
	self.asteroid_shape.instanceTo(self.asteroid)

Once again, I think that bullet might be overkill for my need, but I have no clue as to what collision handler I should use instead (or if I am only using bullet wrong). My need it looooottts of objects (I have a quite capable computer).

Many thanks to you all!

Couple of options. You can try and freeze/sleep the asteroids with setActivationState(0). You can try creating a grid of bullet worlds, but then you would need code to handle the transition of objects between bullet worlds. Yet another option is to have fewer but larger asteroids (or a random range of sizes).

On an unrelated note you will want code to check if there is already an asteroid in that spot to prevent double placement.

Thanks for your answer!

I successfully deactivated the objects with

self.asteroid.node().set_deactivation_enabled(1)
self.asteroid.node().set_active(0)

But there is no impact whatsoever on the performance. Maybe I am missing something?

I tried to change the 3d model to a simple one to see what happen and still no change. So the lag really is from bullet.

I’ve been playing with the settings and thinking about it and I think that making fewer and bigger asteroid would probably be the best way to go (like you said). Less asteroids, more control on them (position/direction).

Unless someone notice a real flaw in my code I think that I will go with that.

Thanks again!

Comment out the line where you attach the bullet node tot he bullet world, see if you have the same performance issues. Then leave the bullet code in but comment out parenting the mesh to render.

Graphics cards have issues with how many objects are drawn on the screen at once. A card can draw an object with a million polygons no sweat. But 500 separate triangles will bring even the most powerful to a crawl.

If you are finding that your mesh count in an issue you might want to look into hardware instancing.

Thank you croxis. I tried what you said and there is no lag at all. I could generate 2000 asteroids without any lag and 10 000 was still playable. I don’t know if there are any “light” collision detection system? I don’t really need any physics calculations, only movement, rotation and collision detection.

One more option for bullet. Set the asteroid mass to 0. These will make the objects static but still be used for collisions.

Panda has a built in physics engine but it might not be what you are looking for and I do not know how its performance compares. There are physx and ODE bindings as well.

Tried it and many other options, but no, no way to make it work. But! even if less pretty, the box shape still fit the bill and allow me to have 200 objects at the same time with a frame rate of 30 (instead of 8).

We do have a performance problem with several hundred Bullet nodes. It’s not the physics simulation itself, but the fact that each object is it’s own scene graph node. If you profile you will most likely find that the scene graph updates are what eats away the CPU time. Unfortunately this is a basic design decision of how we expose Bullet.

One thing which might help is the RigidBodyCombiner. If I remember right I used it before to solve such problems.