I’ve been trying to create the controls to turn an avatar using the rotate method in com.jme3.scene.geometry (which inherits from spatial)
but I cannot get it to respond at all. I’m feeding it a floats in radians. I don’t get an error, just no response.
like so - player.rotate(0, .1, 0);
Anyone have an idea of what I’m doing wrong?
Thanks
that should work fine, you must be using it wrongly (bar the wrong syntax, 0.1f or (float) 0.1). Can you post a testcase?
In this, player is a geometry. rotateCharacter receives a string saying either “left” or “right”. I’ve tried 2 different things.
The first was using the Spatial.rotate methd
[java] //rotates the player left or right
public void rotateCharacter(String dir)
{
if(dir.equals(“left”))
player.rotate(0, 0, .1f);
else if(dir.equals(“right”))
player.rotate(0, 0, -.1f);
else;
}[/java]
The second version I tried was using the setLocalTranlsation as another attempt to turn the character
[java] //rotates the player left or right
public void rotateCharacter(String dir)
{
if(dir.equals(“left”))
player.setLocalTranslation(0, 0, .1f);
else if(dir.equals(“right”))
player.setLocalTranslation(0, 0, -.1f);
else;
}[/java]
I get no compile problems and see nothing come up in use - just no response to arrow keys. At the bottom is a copy of the entire class
The way we had been doing movement is using the setWalkDirection method from the CharacterControl class to go forwards and backwards or strafing. Then were going to turn the avatar with rotate method from Spatial class.
Any light you could shed would be great.
Thanks
[java]package com.benkyou.util;
import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
/**
*
-
@author Student
*/
public class WorldPhysics {
/**
- For each Spatial that you want to be physical:
Create a CollisionShape.
Create a PhysicsControl by supplying the CollisionShape and mass.
E.g. com.jme3.bullet.control.RigidBodyControl
Add the PhysicsControl to the Spatial.
Add the PhysicsControl to the physicsSpace object.
Attach the Spatial to the rootNode, as usual.
(Optional) Implement the PhysicsCollisionListener interface to respond to PhysicsCollisionEvents if desired
*/
private BulletAppState bulletAppState;
private PhysicsSpace physicsSpace;
private CapsuleCollisionShape capsuleShape;
private CharacterControl character_phys;
private Spatial player;
public WorldPhysics(AppStateManager stateManager, Spatial WorldRoot, Geometry player){
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
this.player = player;
//Sets up the physics space for this bulletappstate
physicsSpace = bulletAppState.getPhysicsSpace();
physicsSpace.setGravity(new Vector3f(0f,-2f,0f));
physicsSpace.setAccuracy(0.005f);
//gives the world floor colision with 0 gravity so it doesent fall
WorldRoot.addControl(new RigidBodyControl(0.0f));
//attaches the world floor to the bulletAppState
physicsSpace.add(WorldRoot);
//This is a temporary Player physics to get a feel of working with physics
capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
character_phys = new CharacterControl(capsuleShape, 0.01f);
player.addControl(character_phys);
physicsSpace.add(player);
}
/**
- returns the current physics implementation, should attach to the statemanager
- in the main world class.
- E.X
- WorldPhysics physics = new WorldPhysics();
- stateManager.attach(physics.getPhysics);
-
@return BulletAppState
*/
public BulletAppState getPhysics(){
return bulletAppState;
}
public void moveCharacter(Vector3f direction){
character_phys.setWalkDirection(direction);
}
//rotates the player left or right
public void rotateCharacter(String dir)
{
if(dir.equals("left"))
player.setLocalTranslation(0, .1f, 0);
else if(dir.equals("right"))
player.setLocalTranslation(0, -.1f, 0);
else;
}
}[/java]
you sure rotateCharacter is being called? Printing dir should confirm that. I can’t see the context where its being called, could you provide a testcase, i.e something we can run to see the problem.
Yeah, I guess the problem is with the input listener rather than the rotating, maybe you forgot to register as listener to all names of mappings you defined.