Bullet in 2D?

Post Reply
Jesse_yay
Posts: 1
Joined: Sat Nov 17, 2007 10:13 am

Bullet in 2D?

Post by Jesse_yay »

Hello everybody.
I'm planning on making a two dimensional game and want to use realistic physics.
Bullet seems like a very good physics engine, is there any way to implement in 2D?

thanks
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Bullet in 2D?

Post by Erwin Coumans »

Jesse_yay wrote:Hello everybody.
I'm planning on making a two dimensional game and want to use realistic physics.
Bullet seems like a very good physics engine, is there any way to implement in 2D?

thanks
You could add a 'planar' constraint that keeps the rigid bodies in a 2D plane. This can be done using the btGeneric6DofConstraint with the correct settings (a sample showing how to do this would be useful indeed).

Alternative, you could check out a very similar physics engine called Box2D. Most of the ideas and algorithms are the same, but then all in 2D.

Hope this helps,
Erwin
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Bullet in 2D?

Post by chunky »

I just did this the other day. I was hoping someone here might be able to comment on the validity of what I've done. I created a body with no shape or mass, and just left it. Then create another body [the one I actually wish to attach to the plane z=0] and attach it to the first one with a generic6dof:

Code: Select all

btCollisionShape *mZeroFloorShape;
btRigidBody *mZeroFloorBody;

mZeroFloorShape = NULL;
mZeroFloorBody = new btRigidBody(0,NULL,mZeroFloorShape); // z=0 body
mWorld->addRigidBody(mZeroFloorBody);
is in my main physics class. Then when I create the actual ship bodies, I do thus:

Code: Select all

btRigidBody *mShipBody;

/* Do all the ship-body-creation-stuff */

btGeneric6DofConstraint *mShiptoworld;


btTransform identity = btTransform::getIdentity();
	
mShiptoworld = new btGeneric6DofConstraint(*mShipBody, *mTWPhysics->getZeroPlane(),
							identity, identity, false);

mShiptoworld->setLimit(0,1,0); // Disable X axis limits
mShiptoworld->setLimit(1,1,0); // Disable Y axis limits
mShiptoworld->setLimit(2,0,0); // Set the Z axis to always be equal to zero

mShiptoworld->setLimit(3,1,0); // Uncap the rotational axes
mShiptoworld->setLimit(4,1,0); // Uncap the rotational axes
mShiptoworld->setLimit(5,1,0); // Uncap the rotational axes
	
mTWPhysics->addConstraintToWorld(mShiptoworld);// small wrapper 
Does that look right, to the more experienced bullet coders here?

Thank-you very much,
Gary (-;
Cleanrock
Posts: 7
Joined: Tue May 01, 2007 6:23 pm

Re: Bullet in 2D?

Post by Cleanrock »

do this work for you ?

btw, when i searched this forum to find this topic using "bullet in 2d" as search phrase it failed saying that words are too common or too short, hopefully this can be improved
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Bullet in 2D?

Post by chunky »

Well, it certainly *seems* to be working...

Gary (-;
Post Reply