Sorry sorry for bumping and sorry for the long post but i am stuck on this.
I cant get the camera to move on the 2D node view.
I extended FlyByCam to create an Ortho cam (although you could set it as a FlyBy as well) so i could set parallel projection and dragtorotate
I have a 3D node using FlyCam and a “2D” node using my OrthoCam, i enable/disable between switching nodes
the 2d view is for clicking on the map to add new objects in the world
I added WASD and QZ triggers and they fire.
The OrthoCam gets instantiated with a new Camera (but with the build in cam’s width and height)
I made sure i wasnt secretly changing the wrong camera.
I made sure my moveOrthoCamera which initially was a wrapper for moveCamera was working by copying the code from the FlyByCamera.java, peppering it with println, and i can see pos is changing. I also added Q and Z for zooming in and out and at least they fire.
Nothing is moving in the 2D node view (but i can see the objects).
If i use WASDQZ keys in the 3D node view i directly see movement and if i then switch back to 2D node view i see the objects have moved.
I do remember a topic on 2D games and parallel projection where an extra line was addded after setting the camera to parallel projection but i cant find it anywhere and it is not in my code.
As the first reply would be show us your code here it goes, some code still contains debugging println’s and so on.
OrthoCam class
(most of the use of the this pointer is unnecessary here although it does not hurt)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.input.FlyByCamera;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
/**
*
* @author Martien
*/
public class OrthoCam extends FlyByCamera {
public OrthoCam( Camera cam ) {
super( cam );
//the super does:
//this.cam = cam;
//initialUpVec = cam.getUp().clone();
//
this.cam.setParallelProjection( true );
this.setDragToRotate( true );
if ( this.cam.getName() == null ) {
System.out.println( "Creating new Ortho Cam " );
}
else {
System.out.println( "Creating new Ortho Cam " + this.cam.getName() );
}
if ( this.cam.getName() != "cam" ) {
this.cam.setName( "Ortho" );
}
setCanRotate( true );
this.moveSpeed = 100 * this.moveSpeed;
};
public void setLocation( Vector3f location ) {
this.cam.setLocation( location );
};
public Vector3f getLocation() {
return this.cam.getLocation();
};
public void setFrustum ( int i, int j, float a, float b, float c, float d) {
this.cam.setFrustum( i, j, a, b, c, d);
};
public void setParallelProjection( boolean setParallel ) {
this.cam.setParallelProjection( setParallel );
};
public void setDragToRotate( boolean dragToRotate ) {
this.dragToRotate = dragToRotate;
};
public void setCanRotate( boolean setCanRotate ) {
this.canRotate = setCanRotate;
};
public float getFrustumTop() {
return cam.getFrustumTop();
}
public Vector3f getWorldCoordinates( Vector2f screenPos, float projectionZPos ) {
return this.cam.getWorldCoordinates( screenPos, projectionZPos);
};
protected void moveOrthoCamera(float value, boolean sideways){
//moveCamera(value, sideways);
System.out.println( "Moving " + cam.getName() );
Vector3f vel = new Vector3f();
Vector3f pos = this.cam.getLocation().clone();
System.out.println( "from " + pos );
if (sideways){
this.cam.getLeft(vel);
}
else{
this.cam.getDirection(vel);
}
vel.multLocal(value * this.moveSpeed);
if (this.motionAllowed != null)
this.motionAllowed.checkMotionAllowed(pos, vel);
else
pos.addLocal(vel);
System.out.println( "to " + pos );
this.cam.setLocation(pos);
}
}
instantiating my OrthoCam in simpleInitApp, the handle myOrthoCam is in my App
cam.setName( "cam" );
flyCam.setDragToRotate(true);
Camera myCam = null;
try {
myCam = new Camera( cam.getWidth(), cam.getHeight() );
}
catch ( NullPointerException e ) {
System.err.println( "Severe ERROR: Failed to create base Camera for OrthoCam");
};
try {
myOrthoCam = new OrthoCam( myCam ) ;
//we start off in 3D view with FlyCam enabled
}
catch ( NullPointerException e ) {
System.err.println( "Severe ERROR: Failed to create OrthoCam, no base Camera");
};
myOrthoCam.setEnabled( false );
myOrthoCam.setLocation( new Vector3f( 9, 3, 3 ) );
// _> cam.setDragToRotate(false); no such method tho
float aspect = (float) cam.getWidth() / cam.getHeight();
float size = 1f;
myOrthoCam.setFrustum(-1000, 1000, -aspect * size, aspect * size, size, -size);
//myOrthoCam = new OrthoCam( myCam ) ;
View3D = new Node ( "View3D" );
View2D = new Node ( "View2D" );
in ActionListener we switch the views and camera’s
if (name.equals("Edit") && !keyPressed) {
isEditMode = !isEditMode;
if ( isEditMode ) {
rootNode.detachChild( View3D );
rootNode.attachChild( View2D );
flyCam.setEnabled( false );
myOrthoCam.setEnabled( true ) ;
};
if ( !isEditMode ) {
myOrthoCam.setEnabled( false );
flyCam.setEnabled( true );
rootNode.detachChild( View2D );
rootNode.attachChild( View3D );
};
}
in AnalogListener we handle WASD for map movement (along XY was the intention) and QZ for zoom in and out, here a snippet of the handlers
if (name.equals("D")){
if ( isEditMode ) {
//value = 0;
System.out.println("D key was pressed.");
myOrthoCam.moveOrthoCamera( -value, true );
//Vector3f location = myOrthoCam.getLocation( );
//myOrthoCam.setLocation( new Vector3f( location.x - value*speed, location.y, location.z));
dirtyTrackNodes = true;
}
}
if (name.equals("Q")) {
if ( isEditMode ) {
System.out.println("Q key was pressed.");
frustumSize = myOrthoCam.getFrustumTop() + 0.3f * tpf;
float aspect = (float) cam.getWidth() / cam.getHeight();
myOrthoCam.setFrustum(-1000, 1000, -aspect * frustumSize, aspect * frustumSize, frustumSize, -frustumSize);
dirtyTrackNodes = true;
}
}
Output shows messages like below after setting moveSpeed to moveSpeed * 1000:
AD change pos along X-axis, WS along Z-axis i noted. Vusually nothing moves
Constructing aligned Section succeeded with 2 segments.
Constructing extended aligned Section succeeded with 3 segments.
OGL: Throttling update loop.
OGL: Enter showing state.
OGL: Visible. Create strategy.
OGL: Enter showing state.
OGL: Visible. Create strategy.
OGL: Ceased throttling update loop.
A key was pressed.
Moving Ortho
from (9.0, 3.0, 3.0)
to (131.99875, 3.0, 3.0)
A key was pressed.
Moving Ortho
from (131.99875, 3.0, 3.0)
to (279.45422, 3.0, 3.0)
A key was pressed.
Moving Ortho
from (279.45422, 3.0, 3.0)
to (420.2791, 3.0, 3.0)
A key was pressed.
Moving Ortho
from (420.2791, 3.0, 3.0)
to (561.1069, 3.0, 3.0)
Thanks,
Martien