Ok a real headache here
I am trying to fire a sphere for where I am looking at, an the colide with geometry
I tried to use
Vector3f direction = cam.getDirection(); ///
bullet.addForce(direction.normalizeLocal());
so then that did not work the shperes just always went of to the left also could not apply the forces.
anyway so i went to the turorials, tests and examples
I use used and modified the target/shooting example, now when I applied it to my code, the bullets just went trough everything
I can not make them clide with anything how can i solve this problem???
class FireBullet extends KeyInputAction {
int numBullets;
public void performAction(InputActionEvent evt) {
DynamicPhysicsNode bullet = getPhysicsSpace().createDynamicNode();
bullet.setName( "Ball" );
float radius = 2; //(float) (4.0f* Math.random());
Sphere ball = new Sphere( "Ball", 10, 10, radius );
Utils.color( ball, ColorRGBA.yellow, 10 );
bullet.attachChild( ball );
bullet.setMaterial( Material.IRON );
/** Move bullet to the camera location */
bullet.setLocalTranslation(new Vector3f(cam.getLocation()));
/
* Update the new world locaion for the bullet before I add a
* controller
*/
//bullet.updateGeometricState(0, true);
/
* Add a movement controller to the bullet going in the camera's
* direction
/
bullet.addController(new BulletMover(bullet, new Vector3f(cam.getDirection())));
//Vector3f direction = cam.getDirection(); ///
//bullet.addForce(direction.normalizeLocal()); //if i un comment these and comment bullet.add above, then
//spheres colide but i cannot aim. they
bullet.setCollisionGroup( magicGroup );
bullet.generatePhysicsGeometry();
rootNode.attachChild(bullet);
bullet.updateRenderState();
}
}
class BulletMover extends Controller {
private static final long serialVersionUID = 1L;
/* Bullet that's moving /
DynamicPhysicsNode bullet;
//bullet.setCollisionGroup( 'magicGroup' );
/* Direciton of bullet /
Vector3f direction;
/* speed of bullet /
float speed = 1000;
/* Seconds it will last before going away /
float lifeTime = 5;
BulletMover(DynamicPhysicsNode bullet2, Vector3f direction) {
this.bullet = bullet2;
this.direction = direction;
this.direction.normalizeLocal();
}
public void update(float time) {
lifeTime -= time;
/* If life is gone, remove it /
if (lifeTime < 0) {
rootNode.detachChild(bullet);
bullet.removeController(this);
return;
}
/* Move bullet */
Vector3f bulletPos = bullet.getLocalTranslation();
bulletPos.addLocal(direction.mult(time * speed));
bullet.setLocalTranslation(bulletPos);
}
}
ok did the whole thing in one line of code
bullet.setLinearVelocity(cam.getDirection().normalizeLocal().mult(1000));
so why did addforce not work???
bullet.addForce(cam.getDirection().normalizeLocal().mult(1000));
should work!!!..???
I noticed core dump did use the code in his 3 gravity well simulation
node.setLocalTranslation(cam.getLocation().clone());
node.addForce(cam.getDirection().mult(node.getMass()*1000));
eg addforce as I suspected, this code does not work for the function/class that is very similar…
this may be a bug???
Your code works fine, its just that the Sphere you create is very big and heavy, so you need to apply a lot of force to move it.
If you enabled gravity (which is on by default) your sphere will just fall to the floor.
i added a modified example which works for me fine.
public class TestFireBullet extends SimplePhysicsGame {
@Override
protected void simpleInitGame() {
input.addAction(new FireBullet(), "firebullet", KeyInput.KEY_SPACE, false);
getPhysicsSpace().setDirectionalGravity(new Vector3f());
}
class FireBullet extends KeyInputAction {
int numBullets;
public void performAction(InputActionEvent evt) {
DynamicPhysicsNode bullet = getPhysicsSpace().createDynamicNode();
bullet.setName( "Ball" );
float radius = 2; //(float) (4.0f* Math.random());
Sphere ball = new Sphere( "Ball", 10, 10, radius );
Utils.color( ball, ColorRGBA.yellow, 10 );
bullet.attachChild( ball );
bullet.setMaterial( Material.IRON );
/** Move bullet to the camera location */
bullet.setLocalTranslation(new Vector3f(cam.getLocation()));
Vector3f direction = cam.getDirection();
bullet.addForce(direction.normalizeLocal().mult(1000000));
bullet.generatePhysicsGeometry();
bullet.computeMass();
rootNode.attachChild(bullet);
bullet.updateRenderState();
}
}
public static void main(String[] args) {
TestFireBullet game = new TestFireBullet();
game.setConfigShowMode(ConfigShowMode.AlwaysShow);
game.start();
}
}