Restricting the Mouse

Started by AGP, November 12, 2014, 07:12:02 PM

Previous topic - Next topic

AGP

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.


     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
     }

EgonOlsen

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

AGP

How do you prevent the mouse from going out of the frame, for starters? Or does it always have to play in full screen?

EgonOlsen


AGP

Ah, so I should only use lwjgl's Mouse instead of AWT's? So, does that prevent me from using the software renderer?

EgonOlsen

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.

AGP

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.

EgonOlsen

I never had any issues with it. What can i say...that's what i did and it worked fine for me... ???

AGP

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?

AGP

Egon? Was that too broad a question for your taste?

Irony

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/

AGP

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.

EgonOlsen

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!?

AGP

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)?

EgonOlsen

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.