@staugaard said:
@Setekh
Can u make a vid, and throw it on youtube? :)
Unfortunately i do not have the time, so ima post my test case.
To mention, you'll see a lot of snapping, that's cus of 3 things:
- My method of moving the boxes
- No bone rotation constraints
- That skeleton is hating us.
To get better results increse the Iterations.
@nicholas-leehone is just awesome XD
[java]
package ravenclaw;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.Bone;
import com.jme3.animation.LoopMode;
import com.jme3.animation.Skeleton;
import com.jme3.animation.SkeletonControl;
import com.jme3.app.FlyCamAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.MouseInput;
import com.jme3.input.RawInputListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.debug.SkeletonDebugger;
import com.raven.character.control.IkControl;
import com.raven.tools.Tools;
/**
* @author Seth
*/
public class IKControlTest extends SimpleApplication {
private final Node prime = new Node("Prime");
/* (non-Javadoc)
* @see com.jme3.app.SimpleApplication#simpleInitApp()
*/
@Override
public void simpleInitApp()
{
flyCam.setMoveSpeed(10);
Node model = (Node) assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
//model.setLocalScale(0.5f);
model.setLocalTranslation(0, 5, 0);
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White);
rootNode.addLight(al);
Skeleton skeleton = model.getControl(SkeletonControl.class).getSkeleton();
IkControl ikControl = new IkControl(skeleton);
rootNode.attachChild(model);
AnimControl control = model.getControl(AnimControl.class);
AnimChannel animChannel = control.createChannel();
// animChannel.setAnim("RunTop");
// animChannel.setLoopMode(LoopMode.Loop);
//
// animChannel = control.createChannel();
// animChannel.setAnim("RunBase");
// animChannel.setLoopMode(LoopMode.Loop);
Node node = new Node("Target L");
Geometry g1 = Tools.createShape(assetManager, ColorRGBA.Red, true);
g1.scale(1.25f);
node.attachChild(g1);
node.setLocalTranslation(1f, 0f, 0);
prime.attachChild(node);
Node node2 = new Node("Target R");
Geometry g2 = Tools.createShape(assetManager, ColorRGBA.Blue, true);
g2.scale(1.25f);
node2.attachChild(g2);
node2.setLocalTranslation(-1f, 0f, 0);
prime.attachChild(node2);
Node nodea = new Node("Target AL");
Geometry ga1 = Tools.createShape(assetManager, ColorRGBA.Red, true);
ga1.scale(1.25f);
nodea.attachChild(ga1);
nodea.setLocalTranslation(1f, 5f, 0);
prime.attachChild(nodea);
Node nodea2 = new Node("Target AR");
Geometry ga2 = Tools.createShape(assetManager, ColorRGBA.Blue, true);
ga2.scale(1.25f);
nodea2.attachChild(ga2);
nodea2.setLocalTranslation(-1f, 5f, 0);
prime.attachChild(nodea2);
model.addControl(ikControl);
ikControl.setTarget(node);
ikControl.setFirstBone(skeleton.getBone("Calf.L"));
ikControl.setMaxChain(2);
ikControl.setIterations(50);
ikControl.setTargetBone(skeleton.getBone("Foot.L"));
ikControl = new IkControl(skeleton);
model.addControl(ikControl);
ikControl.setTarget(node2);
ikControl.setFirstBone(skeleton.getBone("Calf.R"));
ikControl.setMaxChain(2);
ikControl.setIterations(50);
ikControl.setTargetBone(skeleton.getBone("Foot.R"));
// =======================================
ikControl = new IkControl(skeleton);
model.addControl(ikControl);
ikControl.setTarget(nodea);
ikControl.setFirstBone(skeleton.getBone("Humerus.L"));
ikControl.setMaxChain(2);
ikControl.setIterations(50);
ikControl.setTargetBone(skeleton.getBone("Hand.L"));
ikControl = new IkControl(skeleton);
model.addControl(ikControl);
ikControl.setTarget(nodea2);
ikControl.setFirstBone(skeleton.getBone("Humerus.R"));
ikControl.setMaxChain(2);
ikControl.setIterations(50);
ikControl.setTargetBone(skeleton.getBone("Hand.R"));
skeleton.reset();
skeleton.updateWorldVectors();
rootNode.attachChild(prime);
for (Bone bone : skeleton.getRoots())
{
System.out.println(bone);
}
SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", skeleton);
Material mat2 = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
mat2.getAdditionalRenderState().setWireframe(true);
mat2.setColor("Color", ColorRGBA.Green);
mat2.getAdditionalRenderState().setDepthTest(false);
skeletonDebug.setMaterial(mat2);
skeletonDebug.setLocalTranslation(model.getLocalTranslation());
rootNode.attachChild(skeletonDebug);
flyCam.setDragToRotate(true);
FlyCamAppState state = stateManager.getState(FlyCamAppState.class);
stateManager.detach(state);
flyCam.registerWithInput(inputManager);
inputManager.deleteMapping("FLYCAM_ZoomIn");
inputManager.deleteMapping("FLYCAM_ZoomOut");
inputManager.deleteMapping("FLYCAM_RotateDrag");
inputManager.addMapping("FLYCAM_RotateDrag", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addListener(flyCam, "FLYCAM_RotateDrag");
inputManager.addRawInputListener(new RawInputListener() {
Geometry target;
float distance;
@Override
public void onTouchEvent(TouchEvent evt)
{
}
@Override
public void onMouseMotionEvent(MouseMotionEvent evt)
{
if(target != null) {
Vector3f dir = cam.getWorldCoordinates(inputManager.getCursorPosition(), .9f);
target.getParent().setLocalTranslation(dir);
}
}
@Override
public void onMouseButtonEvent(MouseButtonEvent evt)
{
System.out.println("ress");
if(evt.isPressed() && evt.getButtonIndex() == MouseInput.BUTTON_LEFT) {
Vector3f pos = cam.getWorldCoordinates(inputManager.getCursorPosition(), .0f);
Vector3f dir = cam.getWorldCoordinates(inputManager.getCursorPosition(), .3f);
dir.subtractLocal(pos).normalizeLocal();
CollisionResults rs = new CollisionResults();
prime.collideWith(new Ray(pos, dir), rs);
System.out.println("press");
if(rs.size() > 0) {
CollisionResult result = rs.getClosestCollision();
System.out.println("result");
distance = result.getDistance();
//if(result.getGeometry().getName().startsWith("Target")) {
target = result.getGeometry();
System.out.println("result2");
//}
}
}
else
target = null;
}
@Override
public void onKeyEvent(KeyInputEvent evt)
{
}
@Override
public void onJoyButtonEvent(JoyButtonEvent evt)
{
}
@Override
public void onJoyAxisEvent(JoyAxisEvent evt)
{
}
@Override
public void endInput()
{
}
@Override
public void beginInput()
{
}
});
}
public static void main(String[] args)
{
new IKControlTest().start();
}
}
[/java]