Hello guys , i’m new to Jmonkey , i have alredy red Beginners tutorial but i’m stucked with this problem.
I have a custom Java class called : Player with attributes like : healt , stamina … and a Vector3f field called direction that should store the current direction that the player is facing. My goal is to use right and left arrow to rotate a box and calculate the new direction it is facing.
I made a test case were :
- a player is rappresented by a simple box moving on a surface
- a player direction is rapresented by a red arrow starting from the center of the box
- mapping for arrow click were added
- when pressing arrow key the direction should change and the cube should rotate
this is what i did so far:
class Player { protected final Vector3f position; protected final Vector3f direction;
public Vector3f getPosition(){ return position;
public Vector3f getDirection(){ return direction;
}
public void Move(int i ){ switch(i){ case 0: //Move right case 1: //Move left
}
}
}
public class Test extends SimpleApplication implements ActionListener{
// Position at 0 0 0
// direction is equal to Z_Axis
Player player
@Override public void simpleInitApp() {
Geometry player_geom = new Geometry("player", new Box(1f, 1f, 1f)); Material player_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); player_mat.setColor("Color", ColorRGBA.randomColor()); player_geom.setMaterial(player_mat);
Geometry floor = new Geometry("floor", new Box(60f, 0.1f, 50f)); Material floor_material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); floor_material.setTexture("ColorMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg")); floor.setMaterial(floor_material); floor.setLocalTranslation(60f, -1f, 0f);
Geometry player_direction = new Geometry("arrow",new Arrow(new Vector3f(10, 0f, 0f))); Material arrow_material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); arrow_material.setColor("Color", ColorRGBA.Red); player_direction.setMaterial(arrow_material); player_direction.setLocalTranslation(-1f, 0.15f, 0f);
Node n = new Node("player_node"); n.attachChild(player_geom); n.attachChild(player_direction); n.setLocalTranslation(player.getPosition()); }
@Override public void simpleUpdate(float tpf) {
.
.
.
// Here shoud rotate the box ``if(ROTATING){ rootNode.getChild("player_node").rotate(//Ho do i rotate facing the new direction ?); }
}
@Override public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals("right") ){
ROTATION = isPressed
;
if(isPressed)
player.move(0)
;
}else if (name.equals("left")){ if(isPressed){ p.move(0); } }
`}`
if somebody could help me understeand how to manage the move function ( how to get the new direction )
and how can i rotate the cube i will apreciate a lot