use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class SceneLoader method endElement.
@Override
public void endElement(String uri, String name, String qName) throws SAXException {
if (qName.equals("node")) {
node = node.getParent();
} else if (qName.equals("nodes")) {
node = null;
} else if (qName.equals("entity")) {
node = entityNode.getParent();
entityNode = null;
} else if (qName.equals("camera")) {
node = cameraNode.getParent();
cameraNode = null;
} else if (qName.equals("light")) {
// apply the node's world transform on the light..
root.updateGeometricState();
if (light != null) {
if (light instanceof DirectionalLight) {
DirectionalLight dl = (DirectionalLight) light;
Quaternion q = node.getWorldRotation();
Vector3f dir = dl.getDirection();
q.multLocal(dir);
dl.setDirection(dir);
} else if (light instanceof PointLight) {
PointLight pl = (PointLight) light;
Vector3f pos = node.getWorldTranslation();
pl.setPosition(pos);
} else if (light instanceof SpotLight) {
SpotLight sl = (SpotLight) light;
Vector3f pos = node.getWorldTranslation();
sl.setPosition(pos);
Quaternion q = node.getWorldRotation();
Vector3f dir = sl.getDirection();
q.multLocal(dir);
sl.setDirection(dir);
}
}
light = null;
}
checkTopNode(qName);
elementStack.pop();
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class SceneLoader method startElement.
@Override
public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
if (qName.equals("scene")) {
if (elementStack.size() != 0) {
throw new SAXException("dotScene parse error: 'scene' element must be the root XML element");
}
String version = attribs.getValue("formatVersion");
if (version == null || (!version.equals("1.0.0") && !version.equals("1.0.1"))) {
logger.log(Level.WARNING, "Unrecognized version number" + " in dotScene file: {0}", version);
}
} else if (qName.equals("nodes")) {
if (root != null) {
throw new SAXException("dotScene parse error: nodes element was specified twice");
}
if (sceneName == null) {
root = new com.jme3.scene.Node("OgreDotScene" + (++sceneIdx));
} else {
root = new com.jme3.scene.Node(sceneName + "-scene_node");
}
node = root;
} else if (qName.equals("externals")) {
checkTopNode("scene");
} else if (qName.equals("item")) {
checkTopNode("externals");
} else if (qName.equals("file")) {
checkTopNode("item");
// NOTE: This part of the file is ignored, it is parsed
// by SceneMaterialLoader in the first pass.
} else if (qName.equals("node")) {
String curElement = elementStack.peek();
if (!curElement.equals("node") && !curElement.equals("nodes")) {
throw new SAXException("dotScene parse error: " + "node element can only appear under 'node' or 'nodes'");
}
parseNode(attribs);
} else if (qName.equals("property")) {
if (node != null) {
String type = attribs.getValue("type");
String name = attribs.getValue("name");
String data = attribs.getValue("data");
if (type.equals("BOOL")) {
node.setUserData(name, Boolean.parseBoolean(data) || data.equals("1"));
} else if (type.equals("FLOAT")) {
node.setUserData(name, Float.parseFloat(data));
} else if (type.equals("STRING")) {
node.setUserData(name, data);
} else if (type.equals("INT")) {
node.setUserData(name, Integer.parseInt(data));
}
}
} else if (qName.equals("entity")) {
checkTopNode("node");
parseEntity(attribs);
} else if (qName.equals("camera")) {
checkTopNode("node");
parseCamera(attribs);
} else if (qName.equals("clipping")) {
checkTopNode("camera");
parseCameraClipping(attribs);
} else if (qName.equals("position")) {
if (elementStack.peek().equals("node")) {
node.setLocalTranslation(SAXUtil.parseVector3(attribs));
} else if (elementStack.peek().equals("camera")) {
cameraNode.setLocalTranslation(SAXUtil.parseVector3(attribs));
}
} else if (qName.equals("quaternion") || qName.equals("rotation")) {
node.setLocalRotation(parseQuat(attribs));
} else if (qName.equals("scale")) {
node.setLocalScale(SAXUtil.parseVector3(attribs));
} else if (qName.equals("light")) {
parseLight(attribs);
} else if (qName.equals("colourDiffuse") || qName.equals("colorDiffuse")) {
if (elementStack.peek().equals("light")) {
if (light != null) {
light.setColor(parseColor(attribs));
}
} else {
checkTopNode("environment");
}
} else if (qName.equals("colourAmbient") || qName.equals("colorAmbient")) {
if (elementStack.peek().equals("environment")) {
ColorRGBA color = parseColor(attribs);
if (!color.equals(ColorRGBA.Black) && !color.equals(ColorRGBA.BlackNoAlpha)) {
// Lets add an ambient light to the scene.
AmbientLight al = new AmbientLight();
al.setColor(color);
root.addLight(al);
}
}
} else if (qName.equals("normal") || qName.equals("direction")) {
checkTopNode("light");
parseLightNormal(attribs);
} else if (qName.equals("lightAttenuation")) {
parseLightAttenuation(attribs);
} else if (qName.equals("spotLightRange") || qName.equals("lightRange")) {
parseLightSpotLightRange(attribs);
}
elementStack.push(qName);
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class BoneContext method buildBone.
/**
* This method builds the bone. It recursively builds the bone's children.
*
* @param bones
* a list of bones where the newly created bone will be added
* @param skeletonOwnerOma
* the spatial of the object that will own the skeleton
* @param blenderContext
* the blender context
* @return newly created bone
*/
public Bone buildBone(List<Bone> bones, Long skeletonOwnerOma, BlenderContext blenderContext) {
this.skeletonOwnerOma = skeletonOwnerOma;
Long boneOMA = boneStructure.getOldMemoryAddress();
bone = new Bone(boneName);
bones.add(bone);
blenderContext.addLoadedFeatures(boneOMA, LoadedDataType.STRUCTURE, boneStructure);
blenderContext.addLoadedFeatures(boneOMA, LoadedDataType.FEATURE, bone);
ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class);
Structure skeletonOwnerObjectStructure = (Structure) blenderContext.getLoadedFeature(skeletonOwnerOma, LoadedDataType.STRUCTURE);
// I could load 'imat' here, but apparently in some older blenders there were bugs or unfinished functionalities that stored ZERO matrix in imat field
// loading 'obmat' and inverting it makes us avoid errors in such cases
Matrix4f invertedObjectOwnerGlobalMatrix = objectHelper.getMatrix(skeletonOwnerObjectStructure, "obmat", blenderContext.getBlenderKey().isFixUpAxis()).invertLocal();
if (objectHelper.isParent(skeletonOwnerOma, armatureObjectOMA)) {
boneMatrixInModelSpace = globalBoneMatrix.mult(invertedObjectOwnerGlobalMatrix);
} else {
boneMatrixInModelSpace = invertedObjectOwnerGlobalMatrix.mult(globalBoneMatrix);
}
Matrix4f boneLocalMatrix = parent == null ? boneMatrixInModelSpace : parent.boneMatrixInModelSpace.invert().multLocal(boneMatrixInModelSpace);
Vector3f poseLocation = parent == null || !this.is(CONNECTED_TO_PARENT) ? boneLocalMatrix.toTranslationVector() : new Vector3f(0, parent.length, 0);
Quaternion rotation = boneLocalMatrix.toRotationQuat().normalizeLocal();
Vector3f scale = boneLocalMatrix.toScaleVector();
bone.setBindTransforms(poseLocation, rotation, scale);
for (BoneContext child : children) {
bone.addChild(child.buildBone(bones, skeletonOwnerOma, blenderContext));
}
return bone;
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class CurvesHelper method transformToFirstLineOfBevelPoints.
/**
* This method transforms the first line of the bevel points positioning it
* on the first point of the curve.
*
* @param startingLinePoints
* the vbevel shape points
* @param firstCurvePoint
* the first curve's point
* @param secondCurvePoint
* the second curve's point
* @return points of transformed bevel
*/
protected Vector3f[] transformToFirstLineOfBevelPoints(Vector3f[] startingLinePoints, Vector3f firstCurvePoint, Vector3f secondCurvePoint) {
Vector3f planeNormal = secondCurvePoint.subtract(firstCurvePoint).normalizeLocal();
float angle = FastMath.acos(planeNormal.dot(Vector3f.UNIT_X));
Vector3f rotationVector = Vector3f.UNIT_X.cross(planeNormal).normalizeLocal();
Matrix4f m = new Matrix4f();
m.setRotationQuaternion(new Quaternion().fromAngleAxis(angle, rotationVector));
m.setTranslation(firstCurvePoint);
Vector3f temp = new Vector3f();
Vector3f[] verts = new Vector3f[startingLinePoints.length];
for (int i = 0; i < verts.length; ++i) {
verts[i] = m.mult(startingLinePoints[i], temp).clone();
}
return verts;
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class TestBoneRagdoll method simpleInitApp.
public void simpleInitApp() {
initCrossHairs();
initMaterial();
cam.setLocation(new Vector3f(0.26924422f, 6.646658f, 22.265987f));
cam.setRotation(new Quaternion(-2.302544E-4f, 0.99302495f, -0.117888905f, -0.0019395084f));
bulletAppState = new BulletAppState();
bulletAppState.setEnabled(true);
stateManager.attach(bulletAppState);
bullet = new Sphere(32, 32, 1.0f, true, false);
bullet.setTextureMode(TextureMode.Projected);
bulletCollisionShape = new SphereCollisionShape(1.0f);
// bulletAppState.getPhysicsSpace().enableDebug(assetManager);
PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
setupLight();
model = (Node) assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
// model.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_X));
//debug view
AnimControl control = model.getControl(AnimControl.class);
SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
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());
//Note: PhysicsRagdollControl is still TODO, constructor will change
ragdoll = new KinematicRagdollControl(0.5f);
setupSinbad(ragdoll);
ragdoll.addCollisionListener(this);
model.addControl(ragdoll);
float eighth_pi = FastMath.PI * 0.125f;
ragdoll.setJointLimit("Waist", eighth_pi, eighth_pi, eighth_pi, eighth_pi, eighth_pi, eighth_pi);
ragdoll.setJointLimit("Chest", eighth_pi, eighth_pi, 0, 0, eighth_pi, eighth_pi);
//Oto's head is almost rigid
// ragdoll.setJointLimit("head", 0, 0, eighth_pi, -eighth_pi, 0, 0);
getPhysicsSpace().add(ragdoll);
speed = 1.3f;
rootNode.attachChild(model);
// rootNode.attachChild(skeletonDebug);
flyCam.setMoveSpeed(50);
animChannel = control.createChannel();
animChannel.setAnim("Dance");
control.addListener(this);
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("toggle") && isPressed) {
Vector3f v = new Vector3f();
v.set(model.getLocalTranslation());
v.y = 0;
model.setLocalTranslation(v);
Quaternion q = new Quaternion();
float[] angles = new float[3];
model.getLocalRotation().toAngles(angles);
q.fromAngleAxis(angles[1], Vector3f.UNIT_Y);
model.setLocalRotation(q);
if (angles[0] < 0) {
animChannel.setAnim("StandUpBack");
ragdoll.blendToKinematicMode(0.5f);
} else {
animChannel.setAnim("StandUpFront");
ragdoll.blendToKinematicMode(0.5f);
}
}
if (name.equals("bullet+") && isPressed) {
bulletSize += 0.1f;
}
if (name.equals("bullet-") && isPressed) {
bulletSize -= 0.1f;
}
if (name.equals("stop") && isPressed) {
ragdoll.setEnabled(!ragdoll.isEnabled());
ragdoll.setRagdollMode();
}
if (name.equals("shoot") && !isPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(matBullet);
bulletg.setLocalTranslation(cam.getLocation());
bulletg.setLocalScale(bulletSize);
bulletCollisionShape = new SphereCollisionShape(bulletSize);
RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, bulletSize * 10);
bulletNode.setCcdMotionThreshold(0.001f);
bulletNode.setLinearVelocity(cam.getDirection().mult(80));
bulletg.addControl(bulletNode);
rootNode.attachChild(bulletg);
getPhysicsSpace().add(bulletNode);
}
if (name.equals("boom") && !isPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(matBullet);
bulletg.setLocalTranslation(cam.getLocation());
bulletg.setLocalScale(bulletSize);
bulletCollisionShape = new SphereCollisionShape(bulletSize);
BombControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);
bulletNode.setForceFactor(8);
bulletNode.setExplosionRadius(20);
bulletNode.setCcdMotionThreshold(0.001f);
bulletNode.setLinearVelocity(cam.getDirection().mult(180));
bulletg.addControl(bulletNode);
rootNode.attachChild(bulletg);
getPhysicsSpace().add(bulletNode);
}
}
}, "toggle", "shoot", "stop", "bullet+", "bullet-", "boom");
inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("boom", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("stop", new KeyTrigger(KeyInput.KEY_H));
inputManager.addMapping("bullet-", new KeyTrigger(KeyInput.KEY_COMMA));
inputManager.addMapping("bullet+", new KeyTrigger(KeyInput.KEY_PERIOD));
}
Aggregations