Hey,
So I downloaded bullet today because I've heard nothing but good things about it. Unfortunately, after reading a quick tutorial, something went terribly wrong XD. So I created plane to serve as the ground and then added a sphere above it and let it fall onto the plane. It hits the ground like I'd expect and then just starts infinitely accelerating towards the negative x and keeping a constant z and y. Here is my code:
//
// physics.cpp
// Spud Engine
//
// Created by Logan on 8/6/14.
// Copyright (c) 2014 Logan. All rights reserved.
//
#include "physics.h"
physicsWorld::physicsWorld()
{
//create all the world components
bulletConfiguration = new btDefaultCollisionConfiguration();
bulletDispatcher = new btCollisionDispatcher(bulletConfiguration);
bulletBroadPhaseInterface = new btDbvtBroadphase();
bulletSolver = new btSequentialImpulseConstraintSolver();
//create the world
bulletWorld = new btDiscreteDynamicsWorld(bulletDispatcher,bulletBroadPhaseInterface,bulletSolver,bulletConfiguration);
bulletWorld->setGravity(btVector3(0,-10,0));
//here are some temp gemoetries
btTransform t;
t.setIdentity();
t.setOrigin(btVector3(0,0,0));
btStaticPlaneShape* plane = new btStaticPlaneShape(btVector3(0,1,0),0.0);
btMotionState* motionStatePlane = new btDefaultMotionState(t);
btRigidBody::btRigidBodyConstructionInfo info(0.0,motionStatePlane,plane);
btRigidBody* planeBody = new btRigidBody(info);
rigidBodies.push_back(planeBody);
bulletWorld->addRigidBody(planeBody);
addRigidSpehere(1, glm::vec3(0,3,0));
}
physicsWorld::~physicsWorld()
{
//delete all the rigid bodies
for (int i = 0; i < rigidBodies.size(); i ++)
{
delete rigidBodies[i];
}
//delete everything
delete bulletWorld;
delete bulletSolver;
delete bulletBroadPhaseInterface;
delete bulletDispatcher;
delete bulletConfiguration;
}
void physicsWorld::update(double time)
{
bulletWorld->stepSimulation(time);
}
btRigidBody* physicsWorld::addRigidSpehere(float rad, glm::vec3 position)
{
btTransform t;
t.setIdentity();
t.setOrigin(btVector3(position.x,position.y,position.z));
btSphereShape* sphere = new btSphereShape(rad);
//calculate inertia
btVector3 inertia(0.0,0.0,0.0);
sphere->calculateLocalInertia(1.0, inertia);
btMotionState* motionState = new btDefaultMotionState(t);
btRigidBody::btRigidBodyConstructionInfo info(1.0,motionState,sphere,inertia);
btRigidBody* sphereBody = new btRigidBody(info);
rigidBodies.push_back(sphereBody);
bulletWorld->addRigidBody(sphereBody);
return sphereBody;
}
And Im updating it with:
SceneRenderer.currentScene->PhysicsWorld->update(1.0/60.0);
Also, both printing out and rendering are demonstrating that problem. Here is the code for that bit:
//temp to test out physics
btTransform t;
btVector3 origin;
float mat[16];
PhysicsWorld->rigidBodies[1]->getMotionState()->getWorldTransform(t);
t.getOpenGLMatrix(mat);
origin = t.getOrigin();
std::cout << origin.x() << " " << origin.y() << " " << origin.z() << std::endl;
RenderEngine.translateToCameraSpace(cam);
glMultMatrixf(mat);
PhysicsSpehere->render(program);
I just started messing around with this, so if anyone can help, I would really appreciate it. Thanks.
EDIT: after a bit more messing around as well as implementing a few other things, it seems that there is a problem with the inertia. Is there something Im doing wrong there?