# Physics Simulation Forum

 All times are UTC

 Page 1 of 1 [ 8 posts ]
 Print view Previous topic | Next topic
Author Message
 Posted: Sun Mar 19, 2017 10:12 pm

Joined: Tue Jan 31, 2017 8:07 pm
Posts: 39
I've been trying to get firing of projectiles to work in my project for a while now. I'm making a basic first person shooter with OpenGL and Bullet, but so far have only been able to fire projectiles based on the camera's position, and not it's rotation (i.e. my projectiles currently always spawn in front of the camera's XYZ position, regardless of which way I've orientated the camera).

Applying physics to an object defined at a point in XYZ coordinates isn't a problem, the problem is just firing a rigidBody from any camera angle. For example, if I'm facing forward and my gun is facing straight ahead, the projectile is created in front of me and appears just after the gun model ends. But, if I turn to the right, the projectile is still created at the same location.

My current method for trying to get this to work is to draw a projectile with OpenGL in front of the camera, and then rotate it based on the camera's orientation (like the projectile orbits the camera). Then I would get the world-coordinates from the model-to-world matrix, and give those to the rigidBody constructor (for the motionstate).

So since this is giving me some issues, does anyone have any suggestions for alternate/better ways to do this? Does anyone have experience with getting this to work?

Cheers.

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Sun Mar 19, 2017 11:16 pm

Joined: Thu Apr 14, 2016 3:44 pm
Posts: 44
Cameras tend to be represented in opengl with a view matrix and projection matrix, create a 3x3 rotation matrix or a quaternion from the view matrix and multiply that by a forward vector like (0,0,1) to get a normal that points where the camera looks. Add this to offset your rigid body's creation location and the rigid body can be shot in this direction by multiplying the normal by a magnitude; myBullet.setLinearVelocity(myForwardVector * magnitude);

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Mon Mar 20, 2017 9:04 pm

Joined: Tue Jan 31, 2017 8:07 pm
Posts: 39
Thanks for the tip, although I'm not entirely sure how I'd do what you suggested.

How can I get a rotation quaternion from the view matrix, a forward vector, where would I add the offset etc.?

Last edited by jimjamjack on Mon May 01, 2017 9:52 pm, edited 1 time in total.

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Thu Mar 23, 2017 11:21 am

Joined: Thu Mar 23, 2017 11:17 am
Posts: 3
Hi!.
Here is some information about how to get and use the forward and the up vector of the camera

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Thu Mar 23, 2017 9:38 pm

Joined: Mon Jul 11, 2016 10:36 am
Posts: 6
You may want to first cast a ray and then fire. Here below is a way do it, the variable direction in the second snippet is coming from the raycast or could be the position of your camera.

Code:
glm::vec3 Camera::castRay(glm::ivec2 mousePosition) {

logStderr(VERBOSE, "mouse position %d, %d...\n", mousePosition.x, mousePosition.y);

float tanFOV = 1.0f / NEAR_PLANE;
float aspect = WINDOW_WIDTH / (float)WINDOW_HEIGHT;

glm::vec3 viewVector = -glm::normalize(m_viewVector);
viewVector *= FAR_PLANE;

glm::vec3 upVector = glm::normalize(m_upVector);
upVector *= FAR_PLANE * tanFOV;

glm::vec3 rightVector = glm::normalize(m_rightVector);
rightVector *= FAR_PLANE * tanFOV;
rightVector *= aspect;

glm::vec3 center = m_cameraPosition + viewVector;

glm::vec3 horizontalDistance = 1.0f / float(WINDOW_WIDTH) * rightVector;
glm::vec3 verticalDistance = upVector / float(WINDOW_HEIGHT);

glm::vec3 raycast = center - 0.5f * rightVector + 0.5f * upVector;

raycast += (float)mousePosition.x * horizontalDistance;
raycast -= (float)mousePosition.y * verticalDistance;

logFileStderr(VERBOSE, "raycast: "); printVec3(raycast);

return raycast;
}

void Inception::shootGeode(std::string typeGeode, const btVector3& direction) {

logStderr(VERBOSE, "MESSAGE: Shooting geode(s)...\n");

std::shared_ptr<Geode> geode;

glm::vec3 cameraPosition = m_camera->getPosition();
btVector3 position = glm2bullet(cameraPosition + 3.0f * glm::normalize(m_camera->getTarget() - cameraPosition));

if (typeGeode == "cube")
geode = m_objectFactory->createCube("cube", new btBoxShape(btVector3(1.0f, 1.0f, 1.0f)), position, "dice");

if (typeGeode == "sphere")
geode = m_objectFactory->createSphere("sphere", new btBoxShape(btVector3(1.0f, 1.0f, 1.0f)), position);

btVector3 velocity = direction;
velocity.normalize();
velocity *= 25.0f;

geode->getRigidBody()->setLinearVelocity(velocity);
}

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Fri Mar 24, 2017 1:02 am

Joined: Tue Jan 31, 2017 8:07 pm
Posts: 39
Thanks for the suggestions, I've looked at that tutorial but I'm still unsure unfortunately. :/

And although many games go for the "using raycast then firing" method, but unfortunately I can't since I need my bullet to appear after my gun model and look as if it's actually being fired since I'm drawing its path through the scene.

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Mon Mar 27, 2017 5:00 pm

Joined: Thu Apr 14, 2016 3:44 pm
Posts: 44
still stuck on this? I think you almost had it figure out.
something like this should offset and shoot your bullet forward or backward.
Code:
float power = 10.f;
glm::vec3 forward = glm::mat3(glm::inverse(ViewMatrix)) * glm::vec3(0,0,1);
glm::vec3 origin = cameraPos + forward;
glm::vec3 vel = forward * power;
myBulletBody.getWorldTransform().setOrigin(btVector3(origin.x,origin,y,origin.z));
myBulletBody.setLinearVelocity(vel.x,vel.y,vel.z);

Top

 Post subject: Re: Best method for firing projectiles from camera?Posted: Mon Mar 27, 2017 5:08 pm

Joined: Tue Jan 31, 2017 8:07 pm
Posts: 39
Thanks guys, works perfectly now. I was just infuriatingly close :p

Using ktfh's method was definitely the best way of doing this (and here I was, trying to do some complex matrix manipulation in OpenGL). Really helped, so cheers.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 8 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 21 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ BULLET PHYSICS LIBRARY USERS    General Bullet Physics Support and Feedback    Release Announcements    Applications, Games, Demos or Movies using Bullet PHYSICS AUTHORING TOOLS, SERIALIZATION AND STANDARDS    Physics authoring tools, serialization, standards and related topics RESEARCH AND DEVELOPMENT IN COLLISION DETECTION & PHYSICS. Don't post Bullet support questions here!    Research and development discussion about Collision Detection and Physics Simulation    Links, Papers, Libraries, Demos, Movies, Comparisons       Non-technical forum and license/patent discussion    Career Opportunities