I’ve been working on a plugin for the past few weeks to help me build my world in the SDK. One of the many features of this plugin is to be able to add a “physics barrier”, which is a simple quad geometry with a rigid body control attached to it.
Now to do so, I created a class “PhysicsBarrier” that extends Geometry, and I’ve also created a class “JmePhysicsBarrier” that extends JmeGeometry, so that I can have it as a custom node in the scene explorer. The classes are shown below:
PhysicsBarrier
package com.fadorico.plugins.tiledTerrainLib.spatials;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.gde.core.scene.SceneApplication;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Quad;
import java.io.IOException;
public class PhysicsBarrier extends Geometry{
private float width;
private float height;
private RigidBodyControl physicsControl;
public PhysicsBarrier(){
this(5, 5);
}
public PhysicsBarrier(float width, float height){
super("Physics barrier", new Quad(width, height));
this.width = width;
this.height = height;
physicsControl = new RigidBodyControl(0);
addControl(physicsControl);
setCullHint(CullHint.Always);
//Material mat = new Material(SceneApplication.getApplication().getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
//setMaterial(mat);
}
public float getWidth(){
return width;
}
public float getHeight(){
return height;
}
public void setWidth(float width){
this.width = width;
((Quad)getMesh()).updateGeometry(width, height);
physicsControl.setEnabled(false);
physicsControl.setEnabled(true);
}
public void setHeight(float height){
this.height = height;
((Quad)getMesh()).updateGeometry(width, height);
physicsControl.setEnabled(false);
physicsControl.setEnabled(true);
}
@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
OutputCapsule capsule = ex.getCapsule(this);
capsule.write(width, "Width", 5);
capsule.write(height, "Height", 5);
}
@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule capsule = im.getCapsule(this);
width = capsule.readFloat("Width", 5);
height = capsule.readFloat("Height", 5);
}
}
JmePhysicsBarrier
package com.fadorico.plugins.tiledTerrainLib.spatials;
import com.jme3.gde.core.sceneexplorer.nodes.JmeGeometry;
import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatial;
import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatialChildren;
import com.jme3.gde.core.sceneexplorer.nodes.SceneExplorerNode;
import com.jme3.scene.Geometry;
import java.awt.Image;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.loaders.DataObject;
import org.openide.nodes.Sheet;
import org.openide.util.ImageUtilities;
@org.openide.util.lookup.ServiceProvider(service=SceneExplorerNode.class)
public class JmePhysicsBarrier extends JmeSpatial{
private static Image smallImage = ImageUtilities.loadImage("com/fadorico/plugins/tiledTerrainLib/icons/barrier.gif");
private PhysicsBarrier physicsBarrier;
public JmePhysicsBarrier(){
}
public JmePhysicsBarrier(PhysicsBarrier spatial, JmeSpatialChildren children){
super(spatial, children);
getLookupContents().add(spatial);
physicsBarrier = spatial;
setName(spatial.getName());
}
@Override
public Image getIcon(int type) {
return smallImage;
}
@Override
public Image getOpenedIcon(int type) {
return smallImage;
}
@Override
protected Sheet createSheet() {
Sheet sheet = super.createSheet();
Sheet.Set set = Sheet.createPropertiesSet();
set.setDisplayName("Physics barrier");
set.setName(PhysicsBarrier.class.getName());
if (physicsBarrier == null) {
return sheet;
}
set.put(makeProperty(physicsBarrier, float.class, "getWidth", "setWidth", "Width"));
set.put(makeProperty(physicsBarrier, float.class, "getHeight", "setHeight", "Height"));
sheet.put(set);
return sheet;
}
@Override
public Class getExplorerNodeClass(){
return JmePhysicsBarrier.class;
}
@Override
public Class getExplorerObjectClass(){
return PhysicsBarrier.class;
}
@Override
public org.openide.nodes.Node[] createNodes(Object key, DataObject key2, boolean readOnly) {
JmeSpatialChildren children=new JmeSpatialChildren((com.jme3.scene.Spatial)key);
children.setReadOnly(readOnly);
return new org.openide.nodes.Node[]{new JmePhysicsBarrier((PhysicsBarrier) key, children).setReadOnly(readOnly)};
}
}
I can successfully add my physics barrier into the scene explorer. However, when I save & close and then try to reopen my scene, I get this weird exception:
java.lang.ClassCastException: com.fadorico.plugins.tiledTerrainLib.spatials.PhysicsBarrier cannot be cast to com.fadorico.plugins.tiledTerrainLib.spatials.PhysicsBarrier
at com.fadorico.plugins.tiledTerrainLib.spatials.JmePhysicsBarrier.createNodes(JmePhysicsBarrier.java:75)
at com.jme3.gde.core.sceneexplorer.nodes.JmeSpatialChildren.createNodes(JmeSpatialChildren.java:138)
at org.openide.nodes.Children$Keys$KE.nodes(Children.java:1662)
............
Does anyone know why I would be getting this??? I mean clearly I’m trying to cast the right object but for some reason it doesn’t recognize it.
I guess its also worth mentioning that I’ve been following this tutorial here http://wiki.jmonkeyengine.org/doku.php/sdk:development:sceneexplorer and that I use the last available nightly build.