Hi guys
i wrote a class for a projectile to shoot on a ball. i don’t want the ball moving if the projectile hits the ball(no applied forces).
This workes sometimes fine, but sometime the ball get an impulse from the projectile. It looks like the collition event is not cleared
after the collition-method. or maybe the collition-method(from rigidbody) of the ball is called first.
event.clean() doesn’t fix the problem.
do you got any idea whats wrong here?
the projectile class looks like this:
[java]
public class PistolBullet {
private final static int DAMAGE = 10;
private Spatial bullet;
private Vector3f direction;
private float BULLET_SPEED = 30f;
private float BULLET_LENGTH = 0.5f;
private Vector3f spaceBox = new Vector3f(0.1f, 0.1f, 0.1f);
private MyCustomControl phys;
public PistolBullet(Player player) {
float possitionOffset = 10f;
Vector3f camDirection = LevelManager.getInstance().getCamera().getDirection();
camDirection.y = 0;
camDirection = camDirection.normalize();
direction = camDirection.mult(BULLET_SPEED);
Box box = new Box(new Vector3f(spaceBox.x / 2, spaceBox.y / 2, spaceBox.z / 2), spaceBox.x, spaceBox.y, spaceBox.z);
Vector3f position = new Vector3f(0f,0f,0f).add(camDirection.mult(possitionOffset));
bullet = new Geometry("PistolBullet", box);
bullet.move(position);
Material mat1 = MaterialFactory.getInstance().getMaterial(EMaterial.PISTOL_BULLET);
bullet.setMaterial(mat1);
BoxCollisionShape boxShape = new BoxCollisionShape(spaceBox);
phys = new MyCustomControl(boxShape, 1f);
phys.setSpatial(bullet);
phys.setCollisionShape(new BoxCollisionShape(spaceBox));
phys.setCollisionGroup(GlobalConstants.COLLISION_GROUP_WEAPONS);
phys.setCollideWithGroups(GlobalConstants.COLLISION_GROUP_WEAPONS);
GameState.getInstance().getBulletAppState().getPhysicsSpace().add(phys);
GameState.getInstance().getBulletAppState().getPhysicsSpace().addCollisionListener(phys);
GameState.getInstance().getBulletAppState().getPhysicsSpace().addTickListener(phys);
bullet.addControl(phys);
}
public class MyCustomControl extends RigidBodyControl implements PhysicsCollisionListener, PhysicsTickListener {
MyCustomControl me;
public MyCustomControl(BoxCollisionShape b, float mass) {
super(b, mass);
me = this;
}
public void collision(PhysicsCollisionEvent event) {
Object other = null;
if (event.getObjectA() == this) {
other = event.getObjectB();
} else if (event.getObjectB() == this) {
other = event.getObjectA();
}
if(other instanceof RigidBodyControl && ((RigidBodyControl) other).getUserObject() instanceof Ball){
((Ball) ((RigidBodyControl) other).getUserObject()).getPlayer().decHealth(DAMAGE);
}
// remove the projectile after hit something
GameState.getInstance().getApp().enqueue(new Callable() {
public Object call() throws Exception {
GameState.getInstance().getBulletAppState().getPhysicsSpace().removeCollisionListener(me);
GameState.getInstance().getBulletAppState().getPhysicsSpace().remove(me);
GameState.getInstance().getStateNode().detachChild(bullet);
return null;
}
});
}
public void prePhysicsTick(PhysicsSpace space, float tpf) {
// this is to clear the gravity
this.clearForces();
final float finalTpf = tpf;
GameState.getInstance().getApp().enqueue(new Callable() {
public Object call() throws Exception {
phys.setLinearVelocity(direction);
return this;
}
});
}
public void physicsTick(PhysicsSpace space, float tpf) {
}
}
}
[/java]
The Ball Class looks like this:
[java]
public class Ball {
private Spatial ballSpatial;
private RigidBodyControl ballPhysics;
private Player owner;
public Ball(Player p) {
owner = p;
Sphere sphere = new Sphere(21, 21, GlobalConstants.BALL_RADIUS, true, false);
// init Graphics
ballSpatial = new Geometry("ball", sphere);
Material mat1 = MaterialFactory.getInstance().getBallMaterial(GameState.getInstance().getAssetManager(), p.getColor());
ballSpatial.setMaterial(mat1);
sphere.setTextureMode(Sphere.TextureMode.Projected);
GameState.getInstance().getStateNode().attachChild(ballSpatial);
SphereCollisionShape ballShape =
new SphereCollisionShape(GlobalConstants.BALL_RADIUS);
// init Physics
ballPhysics = new RigidBodyControl(ballShape, GlobalConstants.BALL_MASS);
ballSpatial.addControl(ballPhysics);
GameState.getInstance().getBulletAppState().getPhysicsSpace().add(ballPhysics);
ballPhysics.setFriction(GlobalConstants.BALL_FRICTION);
ballPhysics.setRestitution(GlobalConstants.BALL_BOUNCYNESS);
ballPhysics.setSleepingThresholds(GlobalConstants.STOP_LINEAR_TRESHOLD, GlobalConstants.STOP_ANGULAR_TRESHOLD);
ballPhysics.setDamping(GlobalConstants.BALL_LINEARDUMPING, GlobalConstants.BALL_ANGULARDUMPING);
ballPhysics.setPhysicsLocation(LevelManager.getInstance().getLevel().getStartPoint());
ballPhysics.setCollisionGroup(GlobalConstants.COLLISION_GROUP_BALLS);
ballPhysics.setCollideWithGroups(GlobalConstants.COLLISION_GROUP_BALLS);
p.setBall(this);
}
public Player getPlayer() {
return owner;
}
}
[/java]