Rotating a cube using jBullet collisions
Posted
by
Kenneth Bray
on Game Development
See other posts from Game Development
or by Kenneth Bray
Published on 2012-10-25T05:59:58Z
Indexed on
2012/10/25
11:16 UTC
Read the original article
Hit count: 270
How would one go about rotating/flipping a cube with the physics of jBullet?
Here is my Draw method for my cube object:
public void Draw() {
// center point posX, posY, posZ
float radius = .25f;//size / 2;
glPushMatrix();
glBegin(GL_QUADS);
//top
{
glColor3f(5.0f,1.0f,5.0f); // white
glVertex3f(posX + radius, posY + radius, posZ - radius);
glVertex3f(posX - radius, posY + radius, posZ - radius);
glVertex3f(posX - radius, posY + radius, posZ + radius);
glVertex3f(posX + radius, posY + radius, posZ + radius);
}
//bottom
{
glColor3f(1.0f,1.0f,0.0f); // ?? color
glVertex3f(posX + radius, posY - radius, posZ + radius);
glVertex3f(posX - radius, posY - radius, posZ + radius);
glVertex3f(posX - radius, posY - radius, posZ - radius);
glVertex3f(posX + radius, posY - radius, posZ - radius);
}
//right side
{
glColor3f(1.0f,0.0f,1.0f); // ?? color
glVertex3f(posX + radius, posY + radius, posZ + radius);
glVertex3f(posX + radius, posY - radius, posZ + radius);
glVertex3f(posX + radius, posY - radius, posZ - radius);
glVertex3f(posX + radius, posY + radius, posZ - radius);
}
//left side
{
glColor3f(0.0f,1.0f,1.0f); // ?? color
glVertex3f(posX - radius, posY + radius, posZ - radius);
glVertex3f(posX - radius, posY - radius, posZ - radius);
glVertex3f(posX - radius, posY - radius, posZ + radius);
glVertex3f(posX - radius, posY + radius, posZ + radius);
}
//front side
{
glColor3f(0.0f,0.0f,1.0f); // blue
glVertex3f(posX + radius, posY + radius, posZ + radius);
glVertex3f(posX - radius, posY + radius, posZ + radius);
glVertex3f(posX - radius, posY - radius, posZ + radius);
glVertex3f(posX + radius, posY - radius, posZ + radius);
}
//back side
{
glColor3f(0.0f,1.0f,0.0f); // green
glVertex3f(posX + radius, posY - radius, posZ - radius);
glVertex3f(posX - radius, posY - radius, posZ - radius);
glVertex3f(posX - radius, posY + radius, posZ - radius);
glVertex3f(posX + radius, posY + radius, posZ - radius);
}
glEnd();
glPopMatrix();
Update();
}
This is my update method for the cube position:
public void Update() {
Transform trans = new Transform();
cubeRigidBody.getMotionState().getWorldTransform(trans);
posX = trans.origin.x;
posY = trans.origin.y;
posZ = trans.origin.z;
Quat4f outRot = new Quat4f();
trans.getRotation(outRot);
rotX = outRot.x;
rotY = outRot.y;
rotZ = outRot.z;
rotW = outRot.w;
}
I am assuming I need to use glrotatef, but it does not seem to work at all when I try that..
this is how I have tried to rotate the cubes:
GL11.glRotatef(rotW, rotX, 0.0f, 0.0f);
GL11.glRotatef(rotW, 0.0f, rotY, 0.0f);
GL11.glRotatef(rotW, 0.0f, 0.0f, rotZ);
© Game Development or respective owner