Oh, thanks! Didn’t realize, stuff like that is “ok”… now I’m just still not sure where to put all the “stuff”.
Is it a very bad idea, to create such “hybrid” objects as an “Actor.class” with its own update method and have the simple update loop over all of those “actors” after they register themselves in a “global” array (in addition to attaching themselves to the root node + their associated collision shape with physicsspace)? Then I can just extend Node, put a “public Geometry the_ball_this_invisible_actor_is_sitting_on” in it and have the “actor node” follow this Geometry inside its own update methode…
If I modify my already existing “Actor.class” like that…: [java]
package mygame;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.Node;
public class Actor extends Node {
boolean _active = false;
public Shape _shape;
public float _size;
public Material _material;
public Vector3f _angularForce= new Vector3f();
public Vector3f _linearForce= new Vector3f();
public Geometry _body;
public RigidBodyControl _physics;
public float _speed=1.0f;
Actor() {
this(1.0f);
}
Actor(float size) {
this(Shape.BALL, size);
}
Actor(Shape shape) {
this(shape, 1.0f);
}
Actor(Shape shape, Material mat) {
this(shape, 1.0f, mat);
}
Actor(Shape shape, float size) {
this (shape, size, World.defaultMat());
}
Actor(Shape shape, float size, Material mat) {
_size = size;
_shape= shape;
_material = mat;
setShape(shape);
_body.setMaterial(_material);
_physics=_body.getControl(RigidBodyControl.class);
_physics.setDamping(0.5f, 0.5f);
_physics.setFriction(0.8f);
World._root.attachChild(this);
Start();
}
void setSize(float size) {
_size = size;
}
void setShape(Shape shape) {
_shape=shape;
switch (_shape) {
case PLANE:
Box floor = new Box(Vector3f.ZERO, 100, 1f, 100);
_body = new Geometry("Floor", floor);
_body.setLocalTranslation(new Vector3f(0f, -3, 0f));
_body.addControl(new RigidBodyControl(new MeshCollisionShape(_body.getMesh()), 0));
break;
case BALL:
Sphere sphere = new Sphere(16, 16, .5f);
_body = new Geometry("Ball", sphere);
_body .setLocalTranslation(1, 1, 1);
_body .addControl(new RigidBodyControl(.001f));
_physics=_body.getControl(RigidBodyControl.class);
_physics.setMass(1.0f);
_physics.setRestitution(0.8f);
break;
case CUBE:
break;
}
attachChild(_body);
Restart();
}
void update(float tpf) {
if (_active) {
_physics.applyTorque(_angularForce.mult(tpf * 100 * _speed));
_physics.applyCentralForce(_linearForce.mult(tpf * 100 * _speed));
}
setLocalTranslation(_body.getLocalTranslation());
}
void Start() {
if (!World._actors.contains(this)) {
World.AddActor(this);}
if (!_active) {
_active=true;
World._space.add(_body);
World._root.attachChild(_body);
}
}
void Stop() {
if (_active) {
_active=false;
World._space.remove(_body);
World._root.detachChild(_body);
}
}
void Destroy() {
Stop();
World.RemoveActor(this);
}
void Restart() {
Stop();
Start();
}
}[/java]
…It works fine. Fits “great” to my World.class (which most likely is an Abomination in itself already :3). Somehow I can’t shake the feeling that I’m doing this all wrong?
Also I’d guess this probably will kill any cpu pretty soon if I continue to throw around dynamic physics actors like that?
(oops, sry, this looks longer than I thought now that it’s in the browser. This forum needs a preview function ;))