Code: Select all
btRigidBody* addSphere(float rad, float x, float y, float z, float mass)
{
btTransform t; // Variable to generate the spheres position and rotation
t.setIdentity();
t.setOrigin(btVector3(x, y, z)); // Initialize Bullet function which allows for velocity and movement.
btSphereShape* sphere = new btSphereShape(rad); // Correct Bullet shape to use along with OpenGL quad.
btVector3 inertia(0, 0, 0); // Initialize inertia function to be calculated.
if (mass != 0.0)
sphere->calculateLocalInertia(mass, inertia); // Calculate inertia of the sphere.
btMotionState* motion = new btDefaultMotionState(t); //set the position (and motion)
btRigidBody::btRigidBodyConstructionInfo info(mass, motion, sphere, inertia); //create the constructioninfo, you can create multiple bodies with the same info
btRigidBody* body = new btRigidBody(info); //let's create the body itself
world->addRigidBody(body); //and let the world know about
speed = rand() % 201 + -100;
if (direction == 1) {
body->setLinearVelocity(btVector3(speed, 0, 0));
}
else if (direction == 2) {
body->setLinearVelocity(btVector3(0, speed, 0));
}
else if (direction == 3) {
body->setLinearVelocity(btVector3(0, 0, speed));
}
body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
body->getMotionState()->getWorldTransform(t);
bodies.push_back(new bulletObject(body,1,1.0,0.0,0.0)); //to be easier to clean, I store them a vector
body->setUserPointer(bodies[bodies.size() - 1]);
return body;
}
Code: Select all
int sphereCount = 85;
for (int i = 1; i <= sphereCount; i++) {
direction = rand() % 2 + 1;
int randomX = rand() % 200 + 1;
xLocation = randomX;
int randomY = rand() % 100 + 1;
yLocation = randomY;
int randomZ = rand() % 200 + 1;
zLocation = randomZ;
int randomRadius = rand() % 2 + 1;
addSphere(randomRadius, xLocation, yLocation, 0.0, 1.0);
Thank you