Problem with Physics :(

Hey monkey,





i read all tutorials and now, i wants to create my first little game. I write on a little tower defence game. Every tower has got a fire range to implement that basic feature i used physics.

As “CollisionMesh” I used a Sphere.

My Towers are controlled by TowerControl which uses PhysicsCollisionListener. So I can see if a enemy is inside the sphere or not. The enemy will be attacked and destroy it, as well, but if i create a 2. Tower in the range of the first tower, they only find themselves and don’t attack my enemy. Is there a possbility to iterate throw all objects in the sphere or have i to implement it on a differen way? Hope you are able to help me :slight_smile:



[java]

public PhysicsCollisionListener pcl = new PhysicsCollisionListener() {

public void collision(PhysicsCollisionEvent pce) {

Spatial tower = null;

Spatial enemy = null;

if(pce.getNodeA().getName().equals(“enemy”) || pce.getNodeB().getName().equals(“enemy”))

{

System.out.println(“ATTACK enemy”);

if(pce.getNodeA().getName().equals(“tower”) == true){

tower = pce.getNodeA();

enemy = pce.getNodeB();

}

else{

tower = pce.getNodeB();

enemy = pce.getNodeA();

}

// ATTACK!!

[/java]



Tower.java



[java]

public void buildTower(Vector3f pos)

{

this.pos = pos;

this.lookAt = Vector3f.ZERO;

//temp

Box box = new Box(new Vector3f(0,0,0),1,1,1);

Material mat = new Material(assetManager,“Common/MatDefs/Misc/Unshaded.j3md”);

mat.setColor(“Color”, ColorRGBA.Blue);

towerSpatial = new Geometry(“tower”, box);

towerSpatial.setMaterial(mat);

towerNode.attachChild(towerSpatial);

rootNode.attachChild(towerNode);

towerSpatial.setLocalTranslation(pos.x,pos.y+1.0f,pos.z);

towerSpatial.setUserData(“range”, this.range);

towerSpatial.setUserData(“speed”,this.speed);

towerSpatial.setUserData(“damage”, this.damage);

towerSpatial.addControl(new TowerControl());

towerSpatial.addControl(gc);

}

[/java]



http://imageshack.us/photo/my-images/266/tag4.png/

And what about make the towers with different names?



[java]

public void buildTower(Vector3f pos, int i)

{



towerSpatial = new Geometry("tower"+i, box);



}

[/java]

Oh,



nice idea, but result.



I tried it like norman said in another post



[java] public PhysicsCollisionListener pcl = new PhysicsCollisionListener() {



Spatial enemy = null;

Spatial tower = null;



public void collision(PhysicsCollisionEvent pce) {

if(1.0f <= timer.getTimeInSeconds()){

timer.reset();

if(pce.getNodeA().getUserData(“killable”) == Boolean.TRUE && pce.getNodeA().getUserData(“killable”)==Boolean.FALSE){

enemy = pce.getNodeA();

tower = pce.getNodeB();

ec = enemy.getControl(EnemyController.class);

System.out.println(ec.getEnemyLife());

ec.lessLife(20);

System.out.println(enemy.getName()+" “+tower.getName());

}else if(pce.getNodeB().getUserData(“killable”) == Boolean.TRUE && pce.getNodeA().getUserData(“killable”)==Boolean.FALSE){

enemy = pce.getNodeB();

tower = pce.getNodeA();

System.out.println(enemy.getName()+” "+tower.getName());

ec = enemy.getControl(EnemyController.class);

System.out.println(ec.getEnemyLife());

ec.lessLife(20);

}

}

}

};[/java]



but won’t work as well :frowning:

[java]public PhysicsCollisionListener pcl = new PhysicsCollisionListener() {



Spatial enemy = null;

Spatial tower = null;



public void collision(PhysicsCollisionEvent pce) {

if(1.0f <= timer.getTimeInSeconds()){

timer.reset();

if(pce.getNodeA().getUserData(“killable”) == Boolean.TRUE && pce.getNodeA().getUserData(“killable”)==Boolean.FALSE){

enemy = pce.getNodeA();

tower = pce.getNodeB();

ec = enemy.getControl(EnemyController.class);

System.out.println(ec.getEnemyLife());

ec.lessLife(20);

System.out.println(enemy.getName()+" “+tower.getName());

}else if(pce.getNodeB().getUserData(“killable”) == Boolean.TRUE && pce.getNodeA().getUserData(“killable”)==Boolean.FALSE){

enemy = pce.getNodeB();

tower = pce.getNodeA();

System.out.println(enemy.getName()+” “+tower.getName());

ec = enemy.getControl(EnemyController.class);

System.out.println(ec.getEnemyLife());

ec.lessLife(20);

}

else{

System.out.println(pce.getNodeA().getName()+” "+pce.getNodeB().getName());

}

}

}

};[/java]



here is a little test



http://imageshack.us/f/190/screenshotzso.png/



have a look at the picture and see the consol output :frowning:



Any Idea’s?



EDIT: Blue are towers and reds are enemys…



The tower will build this way



[java]f (money >= Tower.cost) {

tower = new Tower(assetManager, rootNode);

tower.buildTower(closest.getContactPoint(),++i);

money -= Tower.cost;

bulletAppState.getPhysicsSpace().add(tower.getTowerSpatial());

bulletAppState.getPhysicsSpace().addCollisionListener(new TowerControl().pcl);[/java]

if(pce.getNodeA().getUserData("killable") == Boolean.TRUE && pce.getNodeA().getUserData("killable")==Boolean.FALSE){


:?:?:?:?:?.

According to my almost 2 years of Java Knowledge, this conditions is unreachable xD.
2 Likes

keep in mind that collision meshs are hollow, and that a collision event will only occur when the collision shapes intersect, so if the enemy collision shape is completely inside the tower collision sphere, there will be no collision events.

1 Like

if(pce.getNodeA().getUserData(“killable”) == Boolean.TRUE && pce.getNodeA().getUserData(“killable”)==Boolean.FALSE){



xD

fucking copy & past ^^



@thetoucher



Thank you very mutch :smiley: Any idea how can i handle this stuff??