Select the Class after Collision

Hey guy it’s me again^^

I will write a little game, so i have to select different objetcs and move them. I have a code like

private MyNinja test;

CollisionResults result = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

rootNode.collideWith(ray, result);

if(result.size() > 0){

//Kollidiert

CollisionResult closest = result.getClosestCollision();



if(closest.getGeometry().getName().contains(“Ninja”)){

isSelected = true;

selected = closest.getGeometry();

System.out.println(“selected”);

}

My selectable has a code like:

public class MyNinja{

private Node ninja_node;

private Spatial ninja;

public void doSomething(){…}



}

Now, i want call closest.doSomething or something like

EDIT: cloeset (do a pointer on test) test.doSomething();

Do you have some ideas how to handle this problem?

may i use the wrong softwarepatern ?

Thanks for your reply

Dominique :slight_smile:

Use a Control on the Ninja that you can access for storing your data instead of extending Spatial, Geometry or Node, also be aware that the geometry you get returned is probably a child of the Node you look for.

hell yeah i found something like that, but i wanst sure wheter it works in my case

thank for your quick reply i will test it :slight_smile:

I hope a double post is here not the end of the world.

Normen are you able to give me a little example or explain a little more in detail what i have to do ?

Sry for my stupiness and thanks for your help :frowning:

Dominique

[java]

//implement a separate Control class instead of extending

public class MyControl extends AbstactControl{

Data myData

public Data getMyData(){

return myData;

}



}

//then add it to the model

MyControl control = new MyControl();

model.addControl(control);

//later when collision was detected:

Geometry collisionGeometry = closest.getGeometry();

//try and find MyControl directly on Geometry

MyControl control = collisionGeometry.getControl(MyControl.class);

//if no control is found, find it in some parent of this Geometry, should be the model we look for if it has this control.

if(control == null){

Spatial spat = collisionGeometry.getParent();

while(control == null && spat != null){

control = spat.getControl(MyControl.class);

spat = spat.getParent();

}

}

[/java]

Nice it works great thank you very much :smiley:



May another Question, make it sense if i want to write a little game which consits of some units and buildings…

i.e.



object extends AbstractControl → Unit → bla → bla

→ Building → Home → …



Thanks for your help :slight_smile:

Yeah, just never extend but instead use Controls, this way you can add any functionality you code to any spatial, no matter what base class it is extended from. Look at the MonkeyZone code for some inspiration, as you see the code is only messy where I made the mistake to start extending classes (for networking) :wink:

Nice thank you man, i believe i had understand how to handle this usefull stuff.

I will look at the codezone :slight_smile:

So I putting all togehter may you can say if it was what you mean

[java] public class Control_Unit extends AbstractControl{

private Node rootNode;

private AssetManager assetManager;

private Cinematic mycin;

private MotionTrack mytrack;

private MotionPath mypath;

public Control_Unit() {

this.rootNode = Main.this.rootNode;

this.assetManager = Main.this.assetManager;

mycin = new Cinematic(rootNode,10,LoopMode.Loop);

stateManager.attach(mycin);

mycin.play();

mypath = new MotionPath();

}

public void MoveUnit(){

mypath.enableDebugShape(assetManager, rootNode);

mytrack = new MotionTrack(this.getSpatial(),mypath);

mycin.addCinematicEvent(timer.getTimeInSeconds()+0.5f, mytrack);

mypath = new MotionPath();

}

//add waypoints …



}

public class Ninja(){…}

private Ninja test;

private Ninja test2;



simpleInit(){

//some light and meshs



Control_Unit myControl = new Control_Unit();

Control_Unit myControl2 = new Control_Unit();

test = new Ninja(assetManager, new Vector3f(0,0,0));

test2 = new Ninja(assetManager, new Vector3f(10,0,0));

rootNode.attachChild(test.getNinjaNode());

rootNode.attachChild(test2.getNinjaNode());

test.getNinja().addControl(myControl);

test2.getNinja().addControl(myControl2);

}

//more stuff

// like action handling

private Control_Unit con;

private boolean isSelected = false;

private ActionListener actionListener = new ActionListener() {

@Override

public void onAction(String arg0, boolean arg1, float arg2) {

// TODO Auto-generated method stub

if(arg0.equals(“Move”) && !arg1 && isSelected == false){

CollisionResults result = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

rootNode.collideWith(ray, result);

if(result.size() > 0){

CollisionResult closest = result.getClosestCollision();

mark.setLocalTranslation(closest.getContactPoint());

rootNode.attachChild(mark);

Geometry t = closest.getGeometry();

con = new Control_Unit();

con = t.getControl(Control_Unit.class);

if(con == null){

Spatial spat = t.getParent();

while(con == null && spat != null){

con = spat.getControl(Control_Unit.class);

spat = spat.getParent();

}

}

isSelected = true;

}

}

else if(arg0.equals(“Move”) && !arg1 && isSelected == true) {

//con.AddWay(con.getSpatial().getLocalTranslation()); //cause error discription below

//con.AddWay(new Vector3f(0,0,0));

//EDIT

con.AddWay(new Vector3f(con.getSpatial().getLocalTranslation))); // now it works

CollisionResults result = new CollisionResults();

Ray ray = new Ray(cam.getLocation(),cam.getDirection());

rootNode.collideWith(ray, result);

if(result.size() > 0){

CollisionResult closest = result.getClosestCollision();

mark.setLocalTranslation(closest.getContactPoint());

rootNode.attachChild(mark);

con.AddWay(closest.getContactPoint());

con.MoveUnit();

isSelected = false;

}

}[/java]

Screenshot works:

http://www.xup.in/dl,15351886/works.png/

Screenshot fails:

http://www.xup.in/dl,13897381/fails.png/

Now I am able to controll my ninja and it nearly works as you can see.

But was happend if i want to let both fight. The fight sequence could be implemented in the Control_Unit class but how to handle lifepoints or something like that?

My second question is what did i make wrong that my ninja’s don’t stop at the last waypoint?

EDIT: Solved, i have to create a new instance of Vector3f and it works well.



Sure i could implement a MotionListener but in an other sample i solved it without so i dont believe that it is required. Any Idea?

Hope you are able to help me again

Thanks

Dominique