final Body body2 = shape2.getBody();
Object userData1 = body1.getUserData();
Object userData2 = body2.getUserData();
if (userData1 instanceof IPhysicsComponent && userData2 instanceof IPhysicsComponent) { //Check for ball/star collision
IPhysicsComponent physObj1 = (IPhysicsComponent) userData1;
IPhysicsComponent physObj2 = (IPhysicsComponent) userData2;
// System.out.println("Collided: " + mt4jObj1 + " with " + mt4jObj2);
if (physObj1 instanceof MTComponent && physObj2 instanceof MTComponent) {
MTComponent comp1 = (MTComponent) physObj1;
MTComponent comp2 = (MTComponent) physObj2;
//Check if one of the components is the BALL
MTComponent ball = isHit("ball", comp1, comp2);
final MTComponent theBall = ball;
//Check if one of the components is the GOAL
MTComponent goal1 = isHit("goal1", comp1, comp2);
MTComponent goal2 = isHit("goal2", comp1, comp2);
//Check if a puck was involved
MTComponent bluePuck = isHit("blue", comp1, comp2);
MTComponent redPuck = isHit("red", comp1, comp2);
//Check if a border was hit
MTComponent border = null;
if (comp1.getName() != null && comp1.getName().startsWith("border")){
border = comp1;
}else if (comp2.getName() != null && comp2.getName().startsWith("border")){
border = comp2;
}
if (ball != null){
//CHECK IF BALL HIT A PADDLE
if (enableSound && (bluePuck != null || redPuck != null)){
// System.out.println("PUCK HIT BALL!");
/*
triggerSound(paddleHit);
*/
}
//Check if BALL HIT A GOAL
if (goal1 != null || goal2 != null){
//BALL HIT A GOAL
if (goal1 != null){
System.out.println("GOAL FOR PLAYER 2!");
scorePlayer2++;
}else if (goal2 != null){
System.out.println("GOAL FOR PLAYER 1!");
scorePlayer1++;
}
//Update scores
updateScores();
//Play goal sound
// triggerSound(goalHit);
if (scorePlayer1 >= 15 || scorePlayer2 >= 15){
reset();
}else{
//Reset ball
if (theBall.getUserData("resetted") == null){ //To make sure that we call destroy only once
theBall.setUserData("resetted", true);
app.invokeLater(new Runnable() {
public void run() {
IPhysicsComponent a = (IPhysicsComponent)theBall;
a.getBody().setXForm(new Vec2(getMTApplication().width/2f/scale, getMTApplication().height/2f/scale), a.getBody().getAngle());
// a.getBody().setLinearVelocity(new Vec2(0,0));
a.getBody().setLinearVelocity(new Vec2(ToolsMath.getRandom(-8, 8),ToolsMath.getRandom(-8, 8)));
a.getBody().setAngularVelocity(0);
theBall.setUserData("resetted", null);
}
});
}
}