I have a fps "camera", and just recently managed to set up mouse movement to rotate the angle of viewing. The one problem with the camera is that the mouse can leave the window and the angles will not rotate anymore. I know I can use a robot method like mouseMove(), however, I've heard that it makes the camera rotation feel very jerky. Is there any other way to keep the mouse in the window, say like, Minecraft? I'm using Minecraft as an example because my program uses LWJGL too, and I was wondering how Notch does it. Any suggestions?
Mouse.setGrabbed(true) at a start-up moment,
and for every game-loop(frame):
catch mouse movement with Y_Angle += Mouse.getDX()*0.1f,
then rotate your view matrix around Y axis on Y_Angle degrees/radians.
For rotation around X and Z axes use Mouse.getDY() and think on your own how to implement right matrix rotation for those, but this is the main idea.
Related
So I was using this code to get the coordinates of the touch in the screen.
if(Gdx.input.justTouched()){
System.out.println("X= "+Gdx.input.getX()+"Y= "+Gdx.input.getY());
}
but if I have a device with a 1280x960 resolution and I have a 800x600 orthographic camera.
What can I do to obtain the coordinates inside the camera instead the device touch coordinates
"for example if I touch the limit of the screen on the x axis I want to get 800 instead of 1280"
The correct way to do it is to use the camera to give you the world coordinates from the screen coordinates.
For example...
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY());
camera.unproject(mousePos); // mousePos is now in world coordinates
That way, if you change the camera (zoom / rotate / whatever) then everything will still work.
If you're using a Viewport, then you should use the vieport's unproject method instead, as it may have additional issues to handle (e.g. allowing for the letterboxing in a FitViewport).
Note - In the example I gave, it'd be better to reuse a single Vector3 instance but I wanted the example to be as simple as possible.
I am developing one simple game in which i have encountered one small but important issue.
I have implemented absolute rotation in my logic.
When i start rotating an object when the object does not have any rotation , it works fine and i can rotate as in any direction without any problem as shown in the following link.
Initial Rotation Video
Now the problem arise when the object does have some rotation , and why i try to rotate in one of the direction , instead of being rotated in desire direction the rotation always starts from initial rotation as shown in the following link.
Rotation issue when shape has some rotation
I think the video shows everything , still if you have any questions please ask me.
I think the problem is , there should be a relative rotation in the direction of mouse pointer from whatever circle is selected .
Now about My Logic,
in mouse press event i just checked
Mouse Press
Whether the shape is selected on the canvas , if yes
if one of the four circles contains mouse point if yes
then initiateRotation
Mouse Drag
Using Vector Maths
I update the motion according to mouse points ,
calculate rotation angle according to the following method
Math.atan2(rotationVector.getY(), rotationVector.getX());
and apply rotation on this shape.
Rotation Vector i get from this class
Vector Rotation
I called above class startMotion in mouse press and updateMotion in mouse drag event.
What am i missing or doing anything wrong ?
We need to see some code to be able to help you out. It looks like you reset the rotation, whenever you initiateRotation, and then the object quickly rotates in place, according to your mouse position when you drag.
I'm making a game in libGDX and I decided to use box2dlights to render the lights. I did not used cameras so much up to this point, because I already had most of the code done in pure LWJGL. There are two main operations that I need to do with the coordinates of everything.
The first is to translate the screen to the position of the map (the map is bigger than the screen, and the position of the player defines what portion of the map is visible). So for example, if the player is at (50, 30), I translate everything by (-50, -30), so that the player is in the middle.
The second thing is to multiply everything by a constant, that is the conversion from box2d meters to pixels on screen.
However, since I do not have access to box2dlights rendering, I need to pass these two information to the ray handler, and the only way to do that is via Camera. So I created an Orthographic Camera and translate it in deltaS every tick before drawing, instead of manually subtracting deltaS from every coordinate. That part works perfectly. On the other hand, the zoom thingy does not seem to work, because it zooms in and out based in the middle of the screen. For example, if I set zoom = 2, the screen is reduced twice, but it is centered on the screen. The coordinate (0,0) is not (0,0), as I would expect, but instead is screen.width/4.
Is there any way to set the camera so that it multiplies every coordinate by a number, you would assume zoom function should do OR is there any way to do it directly on box2dlights?
I don't know if my problem is very clear or common, but I can't find anything anywhere.
I finally figured it out! The problem was that I needed to set the zoom before I used
camera.setToOrtho(true, SCREEN_WIDTH, SCREEN_HEIGHT);
Because that method uses the current zoom to set its properties. Hope this helps!
Im building something in JOGL and im looking to make the camera move either through using the keyboard or mouse, it doesnt really matter, so long as the camera can pan around the object and possibly zoom in and out. If using the keyboard/mouse is difficult then I also dont mind using some buttons in the applications, e.g. arrows up, down, left and right and a plus and minus button for the zoom but basically whatevers easiest.
Im building something kind of like Lego but its proving to be quite difficult without being able to move the camera.
To move your "camera" you need to apply a glTranslate3f() transform at the beginning of your rendering function. If your camera's location is to be
cameraPos = <cx, cy, cz>
then you should use
glTranslate3f( -cx, -cy, -cz );
This will offset everything that is drawn by that vector.
In order to use the keyboard to make this happen, you will want to use a KeyListener implementation and define the functions specified by the interface.
public void keyPressed( KeyEvent ke ){ // put something intelligent here... }
public void keyReleased( KeyEvent ke ){}
public void keyTyped( KeyEvent ke ){}
Make sure to register this implementing class as a KeyListener to your GLJPanel (or whatever you're using). Then, inside the keyPressed(...) function, check which key is being pressed and increment the appropriate coordinate of the camera.
If you want to get really fancy and allow the mouse to enable you to look around, you can do a similar thing by creating a MouseMotionListener and registering it. The OpenGL transform that is needed to put this into play can vary based on what type of mouse behavior you are looking for. If you just want something simple that will allow you to look around, you can probably get away with tracking mouse motion in the x and y directions and allowing it to modify some offset angles. Moving the mouse in the x direction rotates about the y-axis. Movement in the y direction rotates about the x-axis. As an OpenGL call, as with the glTranslate3f(), you can use glRotatef() to rotate about each axis.
glRotatef( angleX, 1, 0, 0 ); // to rotate about the x-axis
glRotatef( angleY, 0, 1, 0 ); // to rotate about the y-axis
Again, this is just a quick and easy solution. It won't be beautiful, but it will work. If you want to implement something a bit fancier, you can look into computing an arbitrary axis rotation matrix.
http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/
(one of many resources on this topic)
You use a function called "gluLookAt( position of x , position of y , position of z , x of where the camera looks at, y of where the camera looks at, z of where the camera looks at, 0, 1, 0);"
As for the "0, 1, 0" part, leave them as they are as you are just panning around and zooming in. This just affects the tilt of the camera.
What I want to do is make my mouse move in a circular fashion around a point (500px away from said point). I want the mouse to do this anticlockwise.
So what I am trying to say is this.. I want the mouse to move around a point 500px away, using the Robot class method mouseMove. I jsut can't figure out how I would do so in the math side of things.
use the circle equation to get the dependence between the x value and the y value.
now go over every x between -500 to 500 (jumps of 1), calculate the y value, and move the mouse.
I think little bit trigonometry can do the magic. Then you can move continuously by:
Robot r = new Robot();
r.mouseMove(calculatedX,calculatedY);