2

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.

Spriggsy
  • 196
  • 1
  • 18
  • 1
    https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/BaseBulletTest.java#L218 – Xoppa Jun 24 '14 at 17:26
  • 1
    Id looked into that before, but was looking for something else. thanks for your help, `Ray ray = cam.getPickRay( Gdx.graphics.getWidth() /2, Gdx.graphics.getHeight()/2); ((btRigidBody)obj.body).applyCentralImpulse(ray.direction.scl(100f));` worked a treat – Spriggsy Jun 24 '14 at 17:48

2 Answers2

2

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);
Avi
  • 1,066
  • 1
  • 15
  • 37
  • Are you sure you want to use the camera position for the force? Doesn't sound right to me. – Steve Smith Jun 11 '20 at 13:01
  • @SteveSmith i don't use the camera position for the force ... force is just a constant as in your comment – Avi Jun 15 '20 at 14:27
  • You're applying `dir` as an impulse force, and `dir` is set to the camera position added to the direction (and is then modified). – Steve Smith Jun 16 '20 at 15:18
  • yes, but if you look at your code, its exactly the same, the difference is that i manually calculate the direction vector and scale it up with force, while you use the cam.direction ... the result is the same – Avi Jun 17 '20 at 09:17
  • OP wants to fire cubes in the direction of the camera. To take your code, if the cam is at (x, y, z) 10, 10, 10, and the cam direction is (say) up (0, 1, 0), added together becomes 10, 11, 10. If you normalize that and multiply by a force of (say) 1, you'll apply an impulse force of (approx) 5.5, 6.1, 5.5, which wouldn't fire the cubes upwards. With my code, it would be an impulse force of 0, 1, 0, i.e. up. – Steve Smith Jun 17 '20 at 14:32
0

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.

Steve Smith
  • 2,244
  • 2
  • 18
  • 22