Spriggsy
Spriggsy

Reputation: 306

apply force to an object in specefic direction with bullet physics

I'm trying to fire cubes from the camera origin, using the cameras direction as the firing line. I would like to be able to alter the amount of power delivered in the shot with an int.

After looking though various bits of code found on the web, the following seems to be what I am looking for.

obj.body.applyCentralImpulse(Vector3);    

Where Vector3 giving the forces in the X,Y,Z

The following gives the direction the camera is looking at

Vector3 dir = cam.direction;

how can I combine the dir and the required force?

many thanks.

Upvotes: 3

Views: 4583

Answers (2)

Steve Smith
Steve Smith

Reputation: 2271

This should work:-

Vector3 dir = new Vector3(cam.direction);
obj.body.applyCentralImpulse(dir.scl(force));

Just replace force with various values until you get one that works well.

Upvotes: 0

Avi
Avi

Reputation: 1164

This is how it works for me:

float force = 10.0f;   //but any value that works for you
Vector3 dir = normalize(cam.origin + cam.direction) * force; //suppose your camera moves around
body->applyCentralImpulse(dir);

Upvotes: 3

Related Questions