Requesting assistance for custom control

Greetings all,

I am new to JMonkey and I am trying to make a custom control. I have made a copy of the program I am trying to write and reduced it down in size. The basics of what the program is trying to be is a side scrolling fighting game, like mortal combat or the like, with boxes acting as place holders for actual 3d models. There are two boxes a blue one and the red one. The blue box should apporoch the red box and then attack it. I had the same kind of controls working for the update loop and that worked quite nicely, but now I would like to move that logic to a custom control. I am having issues with the control interacting with the object it is assigned to that being the blue box. I thought that “this.getWorldTranslation()” would work, I have also tried super but that did not know either. I think this should be easy for someone who accutally knows what they are doing but I clearly do not. In the custom control “LogingControl” the variable “b” is the state variable 1 is idle and moves the box inside attacking distance of the other box. 2 is the attack move, a headbut in the case of the box. 3 is a pain move that the box will react to being hit. If someone could also tell me how to make a reference of the red box so that the control attached to the blue box can tell the red box to use the pain move that would be great. I will attach my code.


    package jme3test.helloworld;

    import com.jme3.app.SimpleApplication;
    import com.jme3.font.BitmapText;
    import com.jme3.material.Material;
    import com.jme3.math.Vector3f;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.shape.Box;
    import com.jme3.math.ColorRGBA;
    import com.jme3.renderer.RenderManager;
    import com.jme3.renderer.ViewPort;
    import com.jme3.scene.Node;
    import com.jme3.scene.Spatial;
    import com.jme3.scene.control.AbstractControl;
    import java.util.ResourceBundle.Control;

    public class SSF1 extends SimpleApplication {

    Geometry blue = null;
    Geometry red = null;
    public float tot = 0;

    public static void main(String[] args){
    final SSF1 app = new SSF1();
    app.start();
    }

    @Override
    public void simpleInitApp() {

    /** create a blue box at coordinates (1,-1,1) */
    Box box1 = new Box( Vector3f.ZERO, 1f,2f,.5f);
    blue = new Geometry("Box", box1);
    Material mat1 = new Material(assetManager,
    "Common/MatDefs/Misc/Unshaded.j3md");
    mat1.setColor("Color", ColorRGBA.Blue);
    blue.setMaterial(mat1);
    blue.move(-5,0,-3);

    /** create a red box straight above the blue one at (1,3,1) */
    Box box2 = new Box( Vector3f.ZERO, 1f,2f,.5f);
    red = new Geometry("Box", box2);
    Material mat2 = new Material(assetManager,
    "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.setColor("Color", ColorRGBA.Red);
    red.setMaterial(mat2);
    red.move(5,0,-3);

    rootNode.attachChild(blue);
    rootNode.attachChild(red);

    blue.lookAt(red.getWorldTranslation(), new Vector3f(0,1,0) );
    red.lookAt(blue.getWorldTranslation(), new Vector3f(0,1,0) );

    }

    /* This is the update loop */
    @Override
    public void simpleUpdate(float tpf) {
    guiNode.detachAllChildren();
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    BitmapText helloText = new BitmapText(guiFont, false);
    helloText.setSize(guiFont.getCharSet().getRenderedSize());
    //helloText.setText( Float.toString(tot) );
    helloText.setText(Float.toString(blue.getWorldTranslation().distance(red.getWorldTranslation())));
    helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
    guiNode.attachChild(helloText);


    }
    }
    class LogicControl extends AbstractControl {
    float t = 0;
    public float tot = 0;
    boolean dir = true;
    int b = 0;
    public LogicControl(){} // empty serialization constructor

    @Override
    protected void controlUpdate(float tpf){
    if ( b == 0 ) {
    if ( this.getWorldTranslation().distance(super.red.getWorldTranslation()) >= 2f) {
    this.setLocalTranslation(new Vector3f( (this.getLocalTranslation().getX() + .02f), (this.getLocalTranslation().getY()) , (this.getLocalTranslation().getZ() )));
    } else {
    b = 1;
    }
    } else if ( b == 1 ) {
    if ( (tot <= .7f) && (dir==true) ){
    t = 1.7f * tpf;
    tot += t;
    this.rotate(t,0f,0f);
    } else if(tot > 0) {
    dir = false;
    t = -1.7f * tpf;
    if ( tot < 0 ) { t = 0; }
    tot += t;
    this.rotate(t,0f,0f);
    } else if ( tot <= 0 ) {
    b = 0;
    dir = true;
    tot = 0;
    this.rotate( ( this.getLocalRotation().getX() * -1 ),0f,0f);
    }
    } else if ( b == 2 ) {
    if ( (tot >= -.7f) && (dir==true) ){
    t = -1.7f * tpf;
    tot += t;
    } else if(tot < 0) {
    dir = false;
    t = 1.7f * tpf;
    if ( tot > 0 ) { t = 0; }
    tot += t;
    this.rotate(t,0f,0f);
    } else if ( tot >= 0 ) {
    b = 0;
    }
    }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp){
    // Optional: rendering manipulation (for advanced users)
    }
    @Override
    public LogicControl cloneForSpatial(Spatial spatial){
    final LogicControl control = new LogicControl();
    //Optional: use setters to copy userdata into the cloned control
    // control.setIndex(i); // example
    control.setSpatial(spatial);
    return control;
    }
    }

there is a spatial instance, so you can do this inside your control in the update loop:

[java]spatial.getWorldTranslation ()[/java]

1 Like

I think it would be a Holiday Miracle if all new posters saw the little Java button:

http://i.imgur.com/724HP.png



So their code didn’t look like some kind of 1980s dot matrix printer art. :slight_smile:

Wow! thank you for the super fast reply. “spatial.getWorldTranslation ()” worked as expected. Thank you. Would you be able to tell me the best way to get a reference for the red box for the control? I have some code samples below is this the right approach? how do I access the method created in the control? “blue.addControl(LogicControl);” gives an error “cannot find symbol” do you know why I am getting this error. Thank you for your help.



[java]

public class SSF1 extends SimpleApplication {



public void simpleInitApp() {



blue.addControl(LogicControl); // this produces an error “cannot find symbol”





class LogicControl extends AbstractControl {



public Geometry enemy = null;



public void SetEnemy( Geometry tgeo ) {

if ( tgeo != null ) { enemy = tgeo; }

}

[/java]



Edited by pspeed to remove 1980s printer art.

[java]LogicControl logicControl = new LogicControl ();

blue.addControl (logicControl);

logicControl.SetEnemy (); [/java]

?

1 Like

Note: JME is a Java game engine and requires the developer to be fairly proficient at Java already. Trying to learn a new language and 3D game programming at the same time is nearly impossible.

1 Like

wezrule thank you for your assistance I was missing “LogicControl logicControl = new LogicControl ();” and my game is running. thank you.



pspeed I may be new to JME but I am *fairly proficient with java. Most of the java applications I have made were business oriented and not game oriented though I have made some 2d games.

@michael-goff-at-work said:
[java]
blue.addControl(LogicControl); // this produces an error &quot;cannot find symbol&quot;
[/java]


@michael-goff-at-work said:
pspeed I may be new to JME but I am *fairly proficient with java. Most of the java applications I have made were business oriented and not game oriented though I have made some 2d games.


Sorry for the assumption. But most Java developers with any real experience know that the line you posted won't compile since LogicControl is not a valid symbol on its own... and could trivially figure out why.

This is not a JME problem at all but a Java problem.
1 Like

I have to agree with @normen and @pspeed ^^