jMonkeyEngine Docs (original) (raw)

Custom Controls

A com.jme3.scene.control.Control is a customizable jME3 interface that allows you to cleanly steer the behaviour of game entities (Spatials), such as artificially intelligent behaviour in NPCs, traps, automatic alarms and doors, animals and pets, self-steering vehicles or platforms – anything that moves and interacts. Several instances of custom Controls together implement the behaviours of a type of Spatial.

To control global game behaviour see AppStates – you often use AppStates and Control together.

To control the behaviour of spatials:

  1. Create one control for each type of behavior. When you add several controls to one spatial, they will be executed in the order they were added.
    For example, one NPC can be controlled by a PhysicsControl instance and an AIControl instance.
  2. Define the custom control and implement its behaviour in the Control’s update method:
    • You can pass arguments into your custom control.
    • In the control class, the object spatial gives you access to the spatial and subspatials that the control is attached to.
    • Here you modify the spatial’s transformation (move, scale, rotate), play animations, check its environment, define how it acts and reacts.
  3. Add an instance of the Control to a spatial to give it this behavior. The spatial’s game state is updated automatically from now on.
spatial.addControl(myControl);

To implement game logic for a type of spatial, you will either extend AbstractControl (most common case), or implement the Control interface, as explained in this article.

Usage

Use Controls to implement the behaviour of types of game entities.

Examples: You can write

The possibilities are endless.

Example Code

Other examples include the built-in RigidBodyControl in JME’s physics integration, the built-in TerrainLODControl that updates the terrain’s level of detail depending on the viewer’s perspective, etc.

Existing examples in the code base include:

AbstractControl Class

| | The most common way to create a Control is to create a class that extends AbstractControl. | | --------------------------------------------------------------------------------------------- |

The AbstractControl can be found under com.jme3.scene.control.AbstractControl. This is a default abstract class that implements the Control interface.

Usage: Your custom subclass implements the three methods controlUpdate(), controlRender(), setSpatial(), and cloneForSpatial() as shown here:

public class MyControl extends AbstractControl implements Savable, Cloneable {
    private int index; // can have custom fields -- example

    public MyControl(){} // empty serialization constructor

    /** Optional custom constructor with arguments that can init custom fields.
      * Note: you cannot modify the spatial here yet!
      */
    public MyControl(int i){
        // index=i; // example
    }

    /** This method is called when the control is added to the spatial,
      * and when the control is removed from the spatial (setting a null value).
      * It can be used for both initialization and cleanup.
      */
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        /* Example:
        if (spatial != null){
            // initialize
        }else{
            // cleanup
        }
        */
    }

    /** Implement your spatial's behaviour here.
      * From here you can modify the scene graph and the spatial
      * (transform them, get and set userdata, etc).
      * This loop controls the spatial while the Control is enabled.
      */
    @Override
    protected void controlUpdate(float tpf){
        if(spatial != null) {
            // spatial.rotate(tpf,tpf,tpf); // example behaviour
        }
    }

    @Override
    public Control cloneForSpatial(Spatial spatial){
        final MyControl control = new MyControl();
        /* Optional: use setters to copy userdata into the cloned control */
        // control.setIndex(i); // example
        control.setSpatial(spatial);
        return control;
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp){
        /* Optional: rendering manipulation (for advanced users) */
    }

    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        // im.getCapsule(this).read(...);
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        // ex.getCapsule(this).write(...);
    }
}

See also:

The Control Interface

| | In the less common case that you want to create a Control that also extends another class, create a custom interface that extends jME3’s Control interface. Your class can become a Control by implementing the Control interface, and at the same time extending another class. | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

The Control interface can be found under com.jme3.scene.control.Control. It has the following method signatures:

Usage example:

  1. Create a custom control interface.
public interface MyControlInterface extends Control {  
    public void setSomething(int x); // optionally, add custom methods  
}  
  1. Create custom Controls implementing your Control interface.
public class MyControl extends MyCustomClass implements MyControlInterface {  
    protected Spatial spatial;  
    protected boolean enabled = true;  
    public MyControl() { } // empty serialization constructor  
    public MyControl(int x) { // custom constructor  
        super(x);  
    }  
    @Override  
    public void update(float tpf) {  
        if (enabled && spatial != null) {  
            // Write custom code to control the spatial here!  
        }  
    }  
    @Override  
    public void render(RenderManager rm, ViewPort vp) {  
        // optional for advanced users, e.g. to display a debug shape  
    }  
    @Override  
    public Control cloneForSpatial(Spatial spatial) {  
        MyControl control = new MyControl();  
        // set custom properties  
        control.setSpatial(spatial);  
        control.setEnabled(isEnabled());  
        // set some more properties here...  
        return control;  
    }  
    @Override  
    public void setEnabled(boolean enabled) {  
        this.enabled = enabled;  
    }  
    @Override  
    public boolean isEnabled() {  
        return enabled;  
    }  
    @Override  
    public void setSomething(int z) {  
        // You can add custom methods ...  
    }  
    @Override  
    public void write(JmeExporter ex) throws IOException {  
        super.write(ex);  
        OutputCapsule oc = ex.getCapsule(this);  
        oc.write(enabled, "enabled", true);  
        oc.write(spatial, "spatial", null);  
        // write custom variables ....  
    }  
    @Override  
    public void read(JmeImporter im) throws IOException {  
        super.read(im);  
        InputCapsule ic = im.getCapsule(this);  
        enabled = ic.readBoolean("enabled", true);  
        spatial = (Spatial) ic.readSavable("spatial", null);  
        // read custom variables ....  
    }  
}  

Best Practices

Use the getControl() accessor to get Control objects from Spatials. No need to pass around lots of object references. Here’s an example from the MonkeyZone code:

public class CharacterAnimControl implements Control {
    ...
    public void setSpatial(Spatial spatial) {
        ...
        animControl      = spatial.getControl(AnimControl.class);
        characterControl = spatial.getControl(CharacterControl.class);
        ...
    }
}

You can create custom Control interfaces so a set of different Controls provide the same methods and can be accessed with the interface class type.

public interface ManualControl extends Control {
    public void steerX(float value);
    public void steerY(float value);
    public void moveX(float value);
    public void moveY(float value);
    public void moveZ(float value);
    ...
}

Then you create custom sub-Controls and implement the methods accordingly to the context:

public class ManualVehicleControl   extends ManualControl {...}

and

public class ManualCharacterControl extends ManualControl {...}

Then add the appropriate controls to spatials:

characterSpatial.addControl(new ManualCharacterControl());
...
vehicleSpatial.addControl(new ManualVehicleControl());
...

| | Use the getControl() method on a Spatial to get a specific Control object, and activate its behaviour! ManualControl c = mySpatial.getControl(ManualControl.class); c.steerX(steerX); | | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |