use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class RollingTheMonkey method simpleInitApp.
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
cam.setLocation(new Vector3f(0.0f, 12.0f, 21.0f));
viewPort.setBackgroundColor(new ColorRGBA(0.2118f, 0.0824f, 0.6549f, 1.0f));
// init physics
BulletAppState bulletState = new BulletAppState();
stateManager.attach(bulletState);
space = bulletState.getPhysicsSpace();
space.addCollisionListener(this);
// create light
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.7f, -0.3f, -0.5f)).normalizeLocal());
System.out.println("Here We Go: " + sun.getDirection());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
// create materials
Material materialRed = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialRed.setBoolean("UseMaterialColors", true);
materialRed.setBoolean("HardwareShadows", true);
materialRed.setColor("Diffuse", new ColorRGBA(0.9451f, 0.0078f, 0.0314f, 1.0f));
materialRed.setColor("Specular", ColorRGBA.White);
materialRed.setFloat("Shininess", 64.0f);
Material materialGreen = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialGreen.setBoolean("UseMaterialColors", true);
materialGreen.setBoolean("HardwareShadows", true);
materialGreen.setColor("Diffuse", new ColorRGBA(0.0431f, 0.7725f, 0.0078f, 1.0f));
materialGreen.setColor("Specular", ColorRGBA.White);
materialGreen.setFloat("Shininess", 64.0f);
Material logoMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
logoMaterial.setBoolean("UseMaterialColors", true);
logoMaterial.setBoolean("HardwareShadows", true);
logoMaterial.setTexture("DiffuseMap", assetManager.loadTexture("com/jme3/app/Monkey.png"));
logoMaterial.setColor("Diffuse", ColorRGBA.White);
logoMaterial.setColor("Specular", ColorRGBA.White);
logoMaterial.setFloat("Shininess", 32.0f);
Material materialYellow = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialYellow.setBoolean("UseMaterialColors", true);
materialYellow.setBoolean("HardwareShadows", true);
materialYellow.setColor("Diffuse", new ColorRGBA(0.9529f, 0.7843f, 0.0078f, 1.0f));
materialYellow.setColor("Specular", ColorRGBA.White);
materialYellow.setFloat("Shininess", 64.0f);
// create level spatial
// TODO: create your own level mesh
Node level = new Node("level");
level.setShadowMode(ShadowMode.CastAndReceive);
Geometry floor = new Geometry("floor", new Box(22.0f, 0.5f, 22.0f));
floor.setShadowMode(ShadowMode.Receive);
floor.setLocalTranslation(0.0f, -0.5f, 0.0f);
floor.setMaterial(materialGreen);
Geometry wallNorth = new Geometry("wallNorth", new Box(22.0f, 2.0f, 0.5f));
wallNorth.setLocalTranslation(0.0f, 2.0f, 21.5f);
wallNorth.setMaterial(materialRed);
Geometry wallSouth = new Geometry("wallSouth", new Box(22.0f, 2.0f, 0.5f));
wallSouth.setLocalTranslation(0.0f, 2.0f, -21.5f);
wallSouth.setMaterial(materialRed);
Geometry wallEast = new Geometry("wallEast", new Box(0.5f, 2.0f, 21.0f));
wallEast.setLocalTranslation(-21.5f, 2.0f, 0.0f);
wallEast.setMaterial(materialRed);
Geometry wallWest = new Geometry("wallWest", new Box(0.5f, 2.0f, 21.0f));
wallWest.setLocalTranslation(21.5f, 2.0f, 0.0f);
wallWest.setMaterial(materialRed);
level.attachChild(floor);
level.attachChild(wallNorth);
level.attachChild(wallSouth);
level.attachChild(wallEast);
level.attachChild(wallWest);
// The easy way: level.addControl(new RigidBodyControl(0));
// create level Shape
CompoundCollisionShape levelShape = new CompoundCollisionShape();
BoxCollisionShape floorShape = new BoxCollisionShape(new Vector3f(22.0f, 0.5f, 22.0f));
BoxCollisionShape wallNorthShape = new BoxCollisionShape(new Vector3f(22.0f, 2.0f, 0.5f));
BoxCollisionShape wallSouthShape = new BoxCollisionShape(new Vector3f(22.0f, 2.0f, 0.5f));
BoxCollisionShape wallEastShape = new BoxCollisionShape(new Vector3f(0.5f, 2.0f, 21.0f));
BoxCollisionShape wallWestShape = new BoxCollisionShape(new Vector3f(0.5f, 2.0f, 21.0f));
levelShape.addChildShape(floorShape, new Vector3f(0.0f, -0.5f, 0.0f));
levelShape.addChildShape(wallNorthShape, new Vector3f(0.0f, 2.0f, -21.5f));
levelShape.addChildShape(wallSouthShape, new Vector3f(0.0f, 2.0f, 21.5f));
levelShape.addChildShape(wallEastShape, new Vector3f(-21.5f, 2.0f, 0.0f));
levelShape.addChildShape(wallEastShape, new Vector3f(21.5f, 2.0f, 0.0f));
level.addControl(new RigidBodyControl(levelShape, 0));
rootNode.attachChild(level);
space.addAll(level);
// create Pickups
// TODO: create your own pickUp mesh
// create single mesh for all pickups
// HINT: think particles.
pickUps = new Node("pickups");
Quaternion rotation = new Quaternion();
Vector3f translation = new Vector3f(0.0f, PICKUP_SIZE * 1.5f, -PICKUP_RADIUS);
int index = 0;
float ammount = FastMath.TWO_PI / PICKUP_COUNT;
for (float angle = 0; angle < FastMath.TWO_PI; angle += ammount) {
Geometry pickUp = new Geometry("pickUp" + (index++), new Box(PICKUP_SIZE, PICKUP_SIZE, PICKUP_SIZE));
pickUp.setShadowMode(ShadowMode.CastAndReceive);
pickUp.setMaterial(materialYellow);
pickUp.setLocalRotation(rotation.fromAngles(FastMath.rand.nextFloat() * FastMath.TWO_PI, FastMath.rand.nextFloat() * FastMath.TWO_PI, FastMath.rand.nextFloat() * FastMath.TWO_PI));
rotation.fromAngles(0.0f, angle, 0.0f);
rotation.mult(translation, pickUp.getLocalTranslation());
pickUps.attachChild(pickUp);
pickUp.addControl(new GhostControl(new SphereCollisionShape(PICKUP_SIZE)));
space.addAll(pickUp);
//space.addCollisionListener(pickUpControl);
}
rootNode.attachChild(pickUps);
// Create player
// TODO: create your own player mesh
Geometry playerGeometry = new Geometry("player", new Sphere(16, 32, PLAYER_RADIUS));
playerGeometry.setShadowMode(ShadowMode.CastAndReceive);
playerGeometry.setLocalTranslation(PLAYER_START.clone());
playerGeometry.setMaterial(logoMaterial);
// Store control for applying forces
// TODO: create your own player control
player = new RigidBodyControl(new SphereCollisionShape(PLAYER_RADIUS), PLAYER_MASS);
player.setRestitution(PLAYER_REST);
playerGeometry.addControl(player);
rootNode.attachChild(playerGeometry);
space.addAll(playerGeometry);
inputManager.addMapping(INPUT_MAPPING_FORWARD, new KeyTrigger(KeyInput.KEY_UP), new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(INPUT_MAPPING_BACKWARD, new KeyTrigger(KeyInput.KEY_DOWN), new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(INPUT_MAPPING_LEFT, new KeyTrigger(KeyInput.KEY_LEFT), new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(INPUT_MAPPING_RIGHT, new KeyTrigger(KeyInput.KEY_RIGHT), new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(INPUT_MAPPING_RESET, new KeyTrigger(KeyInput.KEY_R));
inputManager.addListener(this, INPUT_MAPPING_FORWARD, INPUT_MAPPING_BACKWARD, INPUT_MAPPING_LEFT, INPUT_MAPPING_RIGHT, INPUT_MAPPING_RESET);
// init UI
infoText = new BitmapText(guiFont, false);
infoText.setText(INFO_MESSAGE);
guiNode.attachChild(infoText);
scoreText = new BitmapText(guiFont, false);
scoreText.setText("Score: 0");
guiNode.attachChild(scoreText);
messageText = new BitmapText(guiFont, false);
messageText.setText(MESSAGE);
messageText.setLocalScale(0.0f);
guiNode.attachChild(messageText);
infoText.setLocalTranslation(0.0f, cam.getHeight(), 0.0f);
scoreText.setLocalTranslation((cam.getWidth() - scoreText.getLineWidth()) / 2.0f, scoreText.getLineHeight(), 0.0f);
messageText.setLocalTranslation((cam.getWidth() - messageText.getLineWidth()) / 2.0f, (cam.getHeight() - messageText.getLineHeight()) / 2, 0.0f);
// init shadows
FilterPostProcessor processor = new FilterPostProcessor(assetManager);
DirectionalLightShadowFilter filter = new DirectionalLightShadowFilter(assetManager, 2048, 1);
filter.setLight(sun);
processor.addFilter(filter);
viewPort.addProcessor(processor);
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class TestExplosionEffect method simpleInitApp.
@Override
public void simpleInitApp() {
createFlame();
createFlash();
createSpark();
createRoundSpark();
createSmokeTrail();
createDebris();
createShockwave();
explosionEffect.setLocalScale(0.5f);
renderManager.preloadScene(explosionEffect);
cam.setLocation(new Vector3f(0, 3.5135868f, 10));
cam.setRotation(new Quaternion(1.5714673E-4f, 0.98696727f, -0.16091813f, 9.6381607E-4f));
rootNode.attachChild(explosionEffect);
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class TestMousePick method simpleUpdate.
@Override
public void simpleUpdate(float tpf) {
Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
direction.subtractLocal(origin).normalizeLocal();
Ray ray = new Ray(origin, direction);
CollisionResults results = new CollisionResults();
shootables.collideWith(ray, results);
// }
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
mark.setLocalTranslation(closest.getContactPoint());
Quaternion q = new Quaternion();
q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y);
mark.setLocalRotation(q);
rootNode.attachChild(mark);
} else {
rootNode.detachChild(mark);
}
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class FbxNode method fromElement.
@Override
public void fromElement(FbxElement element) {
super.fromElement(element);
Vector3f localTranslation = new Vector3f();
Quaternion localRotation = new Quaternion();
Vector3f localScale = new Vector3f(Vector3f.UNIT_XYZ);
Quaternion preRotation = new Quaternion();
for (FbxElement e2 : element.getFbxProperties()) {
String propName = (String) e2.properties.get(0);
String type = (String) e2.properties.get(3);
if (propName.equals("Lcl Translation")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
//.divideLocal(unitSize);
localTranslation.set((float) x, (float) y, (float) z);
} else if (propName.equals("Lcl Rotation")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
localRotation.fromAngles((float) x * FastMath.DEG_TO_RAD, (float) y * FastMath.DEG_TO_RAD, (float) z * FastMath.DEG_TO_RAD);
} else if (propName.equals("Lcl Scaling")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
//.multLocal(unitSize);
localScale.set((float) x, (float) y, (float) z);
} else if (propName.equals("PreRotation")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
preRotation.set(FbxNodeUtil.quatFromBoneAngles((float) x * FastMath.DEG_TO_RAD, (float) y * FastMath.DEG_TO_RAD, (float) z * FastMath.DEG_TO_RAD));
} else if (propName.equals("InheritType")) {
int inheritType = (Integer) e2.properties.get(4);
inheritMode = InheritMode.values()[inheritType];
} else if (propName.equals("Visibility")) {
visibility = (Double) e2.properties.get(4);
} else if (type.contains("U")) {
String userDataKey = (String) e2.properties.get(0);
String userDataType = (String) e2.properties.get(1);
Object userDataValue;
if (userDataType.equals("KString")) {
userDataValue = (String) e2.properties.get(4);
} else if (userDataType.equals("int")) {
userDataValue = (Integer) e2.properties.get(4);
} else if (userDataType.equals("double")) {
// NOTE: jME3 does not support doubles in UserData.
// Need to convert to float.
userDataValue = ((Double) e2.properties.get(4)).floatValue();
} else if (userDataType.equals("Vector")) {
float x = ((Double) e2.properties.get(4)).floatValue();
float y = ((Double) e2.properties.get(5)).floatValue();
float z = ((Double) e2.properties.get(6)).floatValue();
userDataValue = new Vector3f(x, y, z);
} else {
logger.log(Level.WARNING, "Unsupported user data type: {0}. Ignoring.", userDataType);
continue;
}
userData.put(userDataKey, userDataValue);
}
}
// Create local transform
// TODO: take into account Maya-style transforms (pre / post rotation ..)
jmeLocalNodeTransform.setTranslation(localTranslation);
jmeLocalNodeTransform.setRotation(localRotation);
jmeLocalNodeTransform.setScale(localScale);
if (element.getChildById("Vertices") != null) {
// This is an old-style FBX 6.1
// Meshes could be embedded inside the node..
// Inject the mesh into ourselves..
FbxMesh mesh = new FbxMesh(assetManager, sceneFolderName);
mesh.fromElement(element);
connectObject(mesh);
}
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class FbxNodeUtil method quatFromBoneAngles.
public static Quaternion quatFromBoneAngles(float xAngle, float yAngle, float zAngle) {
float angle;
float sinY, sinZ, sinX, cosY, cosZ, cosX;
angle = zAngle * 0.5f;
sinZ = FastMath.sin(angle);
cosZ = FastMath.cos(angle);
angle = yAngle * 0.5f;
sinY = FastMath.sin(angle);
cosY = FastMath.cos(angle);
angle = xAngle * 0.5f;
sinX = FastMath.sin(angle);
cosX = FastMath.cos(angle);
float cosYXcosZ = cosY * cosZ;
float sinYXsinZ = sinY * sinZ;
float cosYXsinZ = cosY * sinZ;
float sinYXcosZ = sinY * cosZ;
// For some reason bone space is differ, this is modified formulas
float w = (cosYXcosZ * cosX + sinYXsinZ * sinX);
float x = (cosYXcosZ * sinX - sinYXsinZ * cosX);
float y = (sinYXcosZ * cosX + cosYXsinZ * sinX);
float z = (cosYXsinZ * cosX - sinYXcosZ * sinX);
return new Quaternion(x, y, z, w).normalizeLocal();
}
Aggregations