Author Topic: Restricting the Mouse  (Read 6068 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Restricting the Mouse
« on: November 12, 2014, 07:12:02 pm »
I'm making a zombie-shooting game and trying to mimic some of the mechanics of The Last of Us, but with a mouse and keyboard. When the player presses S, the camera zooms over the character's shoulder and the mouse can then be used to target a zombie. For the most part, it works. Except that for the life of me I can't seem to restrict the camera's rotation. The following are my mouse event methods. The weird thing is that sometimes the vertical rotation behaves perfectly (and blocks at both ends), and, most of the times it doesn't.

Code: [Select]
     public void mouseMoved(MouseEvent e) {
if (initialX == 100101 && initialY == 100101) {
     initialX = e.getX();
     initialY = e.getY();
}
if (aiming) {
     float deltaX = e.getX()-initialX;
     float deltaY = e.getY()-initialY;
     float addX = deltaX-lastDeltaX;
     float addY = deltaY-lastDeltaY;
     if ((addX > 0.00f && cameraDisplacementY < Math.PI*.3) || (addX < 0.00f && cameraDisplacementY > -Math.PI*.3))
cameraDisplacementY += addX*.02f;//HORIZONTAL ROTATION
     if ((addY > 0.00f && cameraDisplacementX < Math.PI*.1) || (addY < 0.00f && cameraDisplacementX > -Math.PI*.1))
cameraDisplacementX += addY*.02f;//VERTICAL ROTATION
     lastDeltaX = deltaX;
     lastDeltaY = deltaY;

// if (cameraDisplacementY > 1f || cameraDisplacementY < -1f) {
     hero.getRoot().rotateY(cameraDisplacementY);
gun.rotateY(cameraDisplacementY);
heroSphere.rotateY(cameraDisplacementY);
     cameraDisplacementY = 0.00f;
// }
}
     }

     public void mouseDragged(MouseEvent e) {}
     public void mouseExited(MouseEvent e) {
robot.mouseMove(this.getWidth()/2, this.getHeight()/2);
initialX = e.getX();
initialY = e.getY();
     }

     public void mouseClicked(MouseEvent e) {
if (aiming)
     fireWeapon = true;
     }
     private void fire() {
if (bullet != null)
     theWorld.removeObject(bullet);
bullet = new Bullet(gun, theCamera);
bullet.build();
theWorld.addObject(bullet);
if (buffer.usesRenderer(IRenderer.RENDERER_OPENGL))
     bullet.compileAndStrip();
soundQueue = new SoundQueue();
soundQueue.add(new SoundAndTime(shotgunBlast, 0l));
soundQueue.add(new SoundAndTime(shotgunShellsFalling, 700l));
soundQueue.add(new SoundAndTime(shotgunReload, 1100l));
soundQueue.activateQueue();
fireWeapon = false;//JUST FIRED
     }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Restricting the Mouse
« Reply #1 on: November 12, 2014, 07:54:25 pm »
I normally keep track of the rotation and limit it that way. Like in the second part of this page: http://www.jpct.net/wiki/index.php/FPS-like_camera_controls

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #2 on: November 12, 2014, 07:58:11 pm »
How do you prevent the mouse from going out of the frame, for starters? Or does it always have to play in full screen?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Restricting the Mouse
« Reply #3 on: November 12, 2014, 08:04:21 pm »
Code: [Select]
Mouse.setGrabbed(true);

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #4 on: November 13, 2014, 01:14:50 am »
Ah, so I should only use lwjgl's Mouse instead of AWT's? So, does that prevent me from using the software renderer?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Restricting the Mouse
« Reply #5 on: November 13, 2014, 08:53:18 am »
In that case, i used to set the mouse cursor to invisible by using an image for it and reset it each frame using java.awt.Robot. I might have some sources lying around that do this, if that would help. But it's actually pretty simple to do.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #6 on: November 13, 2014, 01:34:55 pm »
I tried that. In fact, I'm doing it in the above code in mouseExited. Everywhere else, Robot seems to create mouse events of its own. And that's no good.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Restricting the Mouse
« Reply #7 on: November 13, 2014, 01:36:53 pm »
I never had any issues with it. What can i say...that's what i did and it worked fine for me... ???

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #8 on: November 13, 2014, 07:00:54 pm »
I'll make a video of this little issue later. For now, it works well enough.

In a separate question, I would like to raycast the bullet towards the zombie. My bullet, too often, is going through the zombie (in slower computers). Mine is just a proximity test and I understand why the problem happens on slower computers. How, then, could I do this raycasting?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #9 on: November 14, 2014, 05:03:53 pm »
Egon? Was that too broad a question for your taste?

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: Restricting the Mouse
« Reply #10 on: November 14, 2014, 08:22:22 pm »
I'd say this is more a general math question rather than something JPCT specific. Maybe you should give Egon a break on this one ;)
http://www.permadi.com/tutorial/raycast/

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #11 on: November 14, 2014, 08:27:39 pm »
Thanks for the link, but I understand the concept of raycasting. I have a sphere, and it has its own direction. How, then, could I use the sphere, whose both position and direction I know, to raycast it into another object? To me, that's a very jpct-specific question.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Restricting the Mouse
« Reply #12 on: November 14, 2014, 09:56:34 pm »
You can just use one of the ray-based collision methods that jPCT provides, like http://www.jpct.net/doc/com/threed/jpct/World.html#checkCollision(com.threed.jpct.SimpleVector, com.threed.jpct.SimpleVector, float). Or maybe i go the actual question wrong!?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Restricting the Mouse
« Reply #13 on: November 15, 2014, 06:54:43 am »
No, that was the question, thanks. Did you take longer than usual because you thought it too obvious, or is it that pesky day job again? :- )

But here's the thing: the result seems to be too "broad." Even when the bullet clearly misses the zombie, the zombie is dying. How could I  make it more accurate (something as precise as a direct line between the center of the sphere and one of the zombie's triangles--and missing everything else--would be ideal)?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Restricting the Mouse
« Reply #14 on: November 15, 2014, 10:14:25 am »
Then you are doing something fishy. The ray-polygon collision detection methods are delivering (in the range of floating point accuracy) perfect results. That's why, for example, picking is sub-pixel perfect in jPCT.