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);
Related
I know do a horizontal and vertical scroller game (like Mario), in this game type, the character is always in the same distance from user. The character only moves to left and right in horizontal scroller and to down and up in vertical scroller.
But there are games in 2D that the character can move freely in the scene, like for example graphic adventures.
So how can I do that ground in order to move the character freely on the ground with depth sense?
An example can see in this video: http://youtu.be/DbZZVbF8fZY?t=4m17s
Thanks you.
This is how I would do that:
First imagine that you are looking at your scene from the top to the ground. Set your coordinate system like that. So all object on your scene will have X and Y coordinates. All your object movements and checking (when character bumps into a wall or something), calculations do in that 2D world.
Now, to draw your world, if you want simpler thing, without some isometric perspective 3D look you just to draw your background image first, and then order all your objects far to near and draw them that way. Devide your Y coords to squeeze movement area a bit. Add some constant to Y to move that area down. If you characters can jump or fly (move trough Y axe) just move Y coord to for some amount.
But if you want it to be more 3D you'll have to make some kind of perspective transformation - multiply your X coordinate with Y and some constant (start with value 1 for constant and tune it up until optimal value). You can do similar thing with Y coord too, but don't think it's needed for adventure games like this.
This is probably hard to understand without the image, but it's actually very simple transformation.
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.
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.
I am developing a first person shooter in Java and I want to implement controls in which movement of the mouse rotates the player. However in Java, I can only get mouse coordinates by using MouseListener events, so the coordinates will stop changing once the mouse cursor leaves the monitor edge and I will be unable to turn the player's view.
Any tips/suggestions on how to do that? Thanks.
I tried using a java.awt.Robot as AerandiR suggests, but there were a couple of problems I ran into, and it's possible other people will run into them as well, so I will elaborate.
If your goal is to keep the cursor in one position (preferably the center of the screen), then you will want to call something like robot.mouseMove(width/2, height/2); at the end of your mouseMoved() method. With this implementation, every time the mouse is moved off center, the Robot will move it back to the center.
However, when the Robot re-centers the mouse, the player will turn back to where it was. In effect, the player will stutter between the original position and a turned position.
To fix this, instead of defining how far your player turns on the difference between where the mouse is now and where it was, define it as the distance from the center.
Like so: turnAmountX += e.getX() - width/2;
Now, if the Robot re-centers the mouse, e.getX() - width/2 will always yield zero.
Recap:
void mouseMoved(MouseEvent e) {
turnAmountX += e.getX() - width/2;
turnAmountY += e.getY() - height/2;
robot.mouseMove(this.getLocationOnScreen().x + width/2,
this.getLocationOnScreen().y + height/2;
}
In some games, on every mouse movement event the cursor is moved back to the middle of the screen, and the view moves with the corresponding magnitude and direction of the mouse event. You can get that vector by calculating the offset of the cursor position to the center of the screen prior to centering the cursor. To move the cursor back to the center of the screen you can try using the java.awt.Robot class.
Since you're building a first person shooter, you'll probably want to hide the center locked cursor, and draw your own crosshair where the player is intending to aim. That will also involve keeping track of where the cursor should be based on the running total of previous mouse movement events.
If you want to achieve behaviour where the view will continue moving relative to the starting position of the mouse (even once the mouse has stopped moving), you could keep a moving sum of all the previous mouse movement vectors, and move the view correspondingly once every frame. However, this probably applies more to something like a flight simulator than a first person shooter.
Alright, I know HOW to do but It's doing something weird.
I am trying to get the correct X Y coords for a JMenu to appear. I've found a way but it's only a hack. All the right-clicking takes place on the JList, for now until I add character panel, so let's say you right-click near the top-left. You expect the Y coord to be around 40~ pixels and the Y coord to be around 100~ pixels right? Because you're clicking on the near left where the JList is. Wrong. The x y coords count from the top left of the JList when I want it to count from the top left of the WHOLE application. :S
So, what did I do? Since the Y coord is correct, I added 512 pixels to the X coord so it's always in the JList. Like so:
int newMouseX = 512+e.getX();
popup.show(tileOffline.this, newMouseX, e.getY()); // show item at mouse
However, as you can tell I will not be right-clicking in the JList forever.
How can I make it so I get the exact X Y coords of the mouse from the WHOLE applet?
Here's a pic to describe the situation WITHOUT the 512 hack:
I wonder if you're making this harder than it needs to be. The popup menu will position itself relative to the component you pass into the show method (JPopupMenu API). You should just be able to show the popupmenu doing something like this in order to get it to show at the correct location:
popup.show(myJList, e.getX(), e.getY());
SwingUtilities has methods suitable for converting to and from screen coordinates, as well as among Components.
You can use convertPointToScreen in javax.swing.SwingUtilities to convert to screen coordinates - just pass in the component that you're clicking!
convertPointToScreen(Point p, Component c)
http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html
Use the MouseEvent.getXOnScreen() and MouseEvent.getYOnScreen() methods.
The MouseEvent that gets generated when you move or click a mouse contains the absolute coordinates on the screen. Use these two methods to get them.