hello.
today i have sort of an interesting problem. Seen from the picture below, I am trying to get a box to move to world coordinates derived from mouse coordinates on the screen. basically, where ever the mouse is, i want the box to be. The camera is top down.
what happens is, I can move the box around, but only inside the pink lines drawn below. outside of the actual screen bounds of the camera, the box will not go. the reason this is not working is because it is getting the world coordinates of the camera's screen. I am confused how to get the actual representation of these coordinates on the "ground". the green thing is the camera, which is 200 units above the ground, and the blue lines are the view which the camera can actually see.
here is the code with which i am doing this
Vector3f target;
Ray mouseRay = null;
Vector2f mousePos = new Vector2f();
mousePos.x = MouseInput.get().getXAbsolute();
mousePos.y = MouseInput.get().getYAbsolute();
System.out.println(mousePos);
mouseRay = DisplaySystem.getDisplaySystem().getPickRay(mousePos, false, mouseRay);
target = mouseRay.getOrigin();
target.y = 0f;
System.out.println("player2:" + player2.getLocalTranslation() + " Ray:" + target);
player2.setLocalTranslation(target);
player2.updateGeometricState(tpf, true);
thanks. sorry if this is kind of weird! haha.
my end goal is to have an object moved by the camera, which the players character will always be facing, and this was the easiest way i saw to do this.
well, heres my code:
statemanager
package MouseWork;
import com.jmex.game.StandardGame;
import com.jmex.game.state.GameStateManager;
public class statemanager{
public static StandardGame manager;
public static ingame ingamestate;
public statemanager(){
// initiate the main game, which is running off standard game
manager = new StandardGame("JMonkey Game ");
manager.getSettings().setSFX(false);
manager.getSettings().setMusic(false);
manager.getSettings().setWidth(800);
manager.getSettings().setHeight(600);
manager.getSettings().setFullscreen(false);
manager.getSettings().setStencilBits(4);
manager.start();
// initiate the different states
ingamestate = new ingame("Main Menu");
// Activate the game state
ingamestate.setActive(true);
// Add it to the manager
GameStateManager.getInstance().attachChild(ingamestate);
}
public static void main(String[] args) throws Exception {
new statemanager();
}
}
game
package MouseWork;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import com.jme.bounding.BoundingBox;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.light.DirectionalLight;
import com.jme.math.Plane;
import com.jme.math.Ray;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.Timer;
import com.jmex.bui.BButton;
import com.jmex.bui.BComponent;
import com.jmex.bui.BContainer;
import com.jmex.bui.BDecoratedWindow;
import com.jmex.bui.BWindow;
import com.jmex.bui.BuiSystem;
import com.jmex.bui.PolledRootNode;
import com.jmex.bui.event.ActionEvent;
import com.jmex.bui.event.ActionListener;
import com.jmex.bui.layout.GroupLayout;
import com.jmex.bui.layout.TableLayout;
import com.jmex.game.state.BasicGameStateNode;
import com.jmex.game.state.GameStateManager;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.StaticPhysicsNode;
import java.lang.reflect.Field;
public class ingame extends BasicGameStateNode{
public CameraNode camNode;
public PhysicsSpace pSpace = PhysicsSpace.create();
public DynamicPhysicsNode player;
public Node player2;
public Node scene;
public Node camHolder;
protected float tpf;
public Vector3f camLoc;
public InputHandler input = new InputHandler();
Node rootNode = super.rootNode;
//extras
Vector3f target;
float zoomValue = 3;
Ray mouseRay = null;
Vector2f mousePos = new Vector2f();
public ingame(String name){
super(name);
initSystem();
initGame();
}
public void update(float tpf) {
super.update(tpf);
pSpace.update(tpf);
if (KeyBindingManager.getKeyBindingManager().isValidCommand("Exit"))
{
System.exit(0);
}
input.update(tpf);
camHolder.setLocalTranslation(player.getLocalTranslation());
camLoc = player.getLocalTranslation();
if(camLoc.y <= 0){
player.setLocalTranslation(0,6,1);
player.clearDynamics();
}
mousePos.x = MouseInput.get().getXAbsolute();
mousePos.y = MouseInput.get().getYAbsolute();
System.out.println(mousePos);
mouseRay = DisplaySystem.getDisplaySystem().getPickRay(mousePos, false, mouseRay);
target = mouseRay.getOrigin();
target.y = 0f;
System.out.println("player2:" + player2.getLocalTranslation() + " Ray:" + target);
player2.setLocalTranslation(target);
player2.updateGeometricState(tpf, true);
}
public void render(float tpf) {
super.render(tpf);
}
public void initSystem(){
buildInput();
}
public void initGame(){
buildZone();
buildPlayers();
buildCamera();
color( player, new ColorRGBA( 0, 1, 0, 1 ) );
color( player2, new ColorRGBA( 0, 0, 1, 1 ) );
rootNode.updateGeometricState(0.0f, true);
rootNode.updateRenderState();
}
public void buildInput(){
// show mouse cursor
MouseInput.get().setCursorVisible(true);
KeyBindingManager.getKeyBindingManager().set("exit", KeyInput.KEY_ESCAPE);
//controls for player -- physics
input.addAction( new up(),
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_W, InputHandler.AXIS_NONE, true );
input.addAction( new left(),
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_A, InputHandler.AXIS_NONE, true );
input.addAction( new down(),
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_S, InputHandler.AXIS_NONE, true );
input.addAction( new right(),
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_D, InputHandler.AXIS_NONE, true );
}
public void buildZone(){
scene = new Node("scene");
StaticPhysicsNode staticNode = pSpace.createStaticNode();
scene.attachChild( staticNode );
final Box visualFloorBox = new Box( "floor", new Vector3f(0,0,0), 100, 1 ,100);
staticNode.attachChild( visualFloorBox );
staticNode.generatePhysicsGeometry();
scene.setLocalTranslation(0,0,1);
scene.updateGeometricState(tpf, true);
rootNode.attachChild(scene);
/** Set up a basic, default light. */
DirectionalLight light = new DirectionalLight();
light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
light.setDirection(new Vector3f(1,-1,0));
light.setEnabled(true);
/** Attach the light to a lightState and the lightState to rootNode. */
LightState lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
lightState.setEnabled(true);
lightState.attach(light);
rootNode.setRenderState(lightState);
}
public void buildPlayers(){
//build physics sphere
player = pSpace.createDynamicNode();
final Sphere visualFallingBox = new Sphere("Sphere", 20, 20, 5);
player.attachChild( visualFallingBox );
player.generatePhysicsGeometry();
player.setLocalTranslation( 50, 10, 1 );
player.setModelBound(new BoundingBox());
player.updateModelBound();
player.updateGeometricState(tpf, true);
System.out.println("changing sphere location");
rootNode.attachChild(player);
//build player box
player2 = new Node("scene");
Box s = new Box( "floor", new Vector3f(0,0,0), 5, 5 ,5);
s.setLocalTranslation(new Vector3f(0,0,0));
s.setModelBound(new BoundingBox());
s.updateModelBound();
player2.attachChild(s);
player2.setLocalTranslation(0,5,1);
player2.updateGeometricState(tpf, true);
rootNode.attachChild(player2);
}
public void buildCamera(){
// attach the CameraNode to the player node
camHolder = new Node("camera holder");
camNode = new CameraNode("cam node", statemanager.manager.getCamera());
camNode.setCamera(statemanager.manager.getCamera());
// moves the camera x,y,z coordinates currently 0,220,1 *edited need fix
camNode.setLocalTranslation(0,200,1);
camHolder.attachChild(camNode);
camNode.lookAt(camHolder.getLocalTranslation(), scene.getLocalTranslation());
rootNode.attachChild(camHolder);
}
public class left extends InputAction {
public void performAction( InputActionEvent evt ) {
player.addForce(new Vector3f( 2, 0, 0 ));
}
}
public class right extends InputAction {
public void performAction( InputActionEvent evt ) {
player.addForce(new Vector3f( -2, 0, 0 ));
}
}
public class down extends InputAction {
public void performAction( InputActionEvent evt ) {
player.addForce(new Vector3f( 0, 0, -2 ));
}
}
public class up extends InputAction {
public void performAction( InputActionEvent evt ) {
player.addForce(new Vector3f( 0, 0, 2 ));
}
}
void color( Spatial spatial, ColorRGBA color ) {
final MaterialState materialState = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
materialState.setDiffuse( color );
if ( color.a < 1 ) {
final BlendState blendState = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
blendState.setEnabled( true );
blendState.setBlendEnabled( true );
blendState.setSourceFunction( BlendState.SourceFunction.SourceAlpha );
blendState.setDestinationFunction( BlendState.DestinationFunction.OneMinusSourceAlpha );
spatial.setRenderState( blendState );
spatial.setRenderQueueMode( Renderer.QUEUE_TRANSPARENT );
}
spatial.setRenderState( materialState );
}
}