I’m attempting to fire bullets from my gun in my OpenGL game, by spawning a new bullet model on every left click. However, the bullet is only being rendered while I click, and disappears straight afterwards (because it’s being done in an if statement maybe?).
Here’s my code (which is inside my main game’s do-while loop):
static int lcOldState = GLFW_RELEASE;
int lcNewState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
if (lcNewState == GLFW_RELEASE && lcOldState == GLFW_PRESS && bulletCount > 0) {
glm::vec3 bulletPosition = glm::vec3(getPosition().x, getPosition().y, getPosition().z);
glm::quat bulletRotation = glm::normalize(glm::quat(glm::vec3(-1.5708, 0.03054326, 0)));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, bulletTexture);
glUniform1i(TextureID, 0);
glm::mat4 BulletRotationMatrix = glm::toMat4(bulletRotation);
glm::mat4 BulletTranslationMatrix = translate(mat4(), glm::vec3(0.35, 0, -3));
glm::mat4 BulletScaleMatrix = scale(mat4(), glm::vec3(0.1f, 0.1f, 0.1f));
glm::mat4 ModelMatrix2 = BulletTranslationMatrix * BulletRotationMatrix * BulletScaleMatrix;
glm::mat4 MVP2 = ProjectionMatrix * ModelMatrix2;
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP2[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix2[0][0]);
// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2);
glVertexAttribPointer(
0, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer2);
glVertexAttribPointer(
1, // attribute
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 3rd attribute buffer : normals
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer2);
glVertexAttribPointer(
2, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer2);
// Draw the triangles
glDrawElements(GL_TRIANGLES, bulletIndices.size(), GL_UNSIGNED_SHORT, (void*)0);
btDefaultMotionState* bulletMotionstate = new btDefaultMotionState(btTransform(
btQuaternion(bulletRotation.x, bulletRotation.y, bulletRotation.z, bulletRotation.w),
btVector3(bulletPosition.x, bulletPosition.y, bulletPosition.z)
));
btRigidBody::btRigidBodyConstructionInfo bulletRigidBodyCI(bulletMass, bulletMotionstate, bulletCollisionShape, bulletInertia);
btRigidBody *bulletRigidBody = new btRigidBody(bulletRigidBodyCI);
rigidbodies.push_back(bulletRigidBody);
dynamicsWorld->addRigidBody(bulletRigidBody);
btTransform bulletTrans;
bulletRigidBody->getMotionState()->getWorldTransform(bulletTrans);
bulletPosition.x = bulletTrans.getOrigin().getX();
bulletPosition.y = bulletTrans.getOrigin().getY();
bulletPosition.z = bulletTrans.getOrigin().getZ();
bulletCount--;
}
lcOldState = lcNewState;
Also, my other problem is creating my bullet’s rigidbody at the bullet’s location, since I’m drawing the bullet in camera space (my gun is drawn in camera space, and is therefore attached to the camera like an FPS style gun). I can’t draw my bullet in world space because the bullet needs to spawn at the end of my gun model, which could be facing any direction.
Is there a way to get the bullet to stay rendered, and ideally, once it’s drawn in camera space then place it in the world space so that it has world coordinates (which I can populate my bulletPosition vector with and create a rigidbody at that point)?
Cheers.
P.S. my getPosition() method just gets the player position for now, I want it to get the bullet’s location in world space if possible.