www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: AGP on November 12, 2014, 07:12:02 pm

Title: Restricting the Mouse
Post by: AGP 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
     }
Title: Re: Restricting the Mouse
Post by: EgonOlsen 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 (http://www.jpct.net/wiki/index.php/FPS-like_camera_controls)
Title: Re: Restricting the Mouse
Post by: AGP 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?
Title: Re: Restricting the Mouse
Post by: EgonOlsen on November 12, 2014, 08:04:21 pm
Code: [Select]
Mouse.setGrabbed(true);
Title: Re: Restricting the Mouse
Post by: AGP 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?
Title: Re: Restricting the Mouse
Post by: EgonOlsen 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.
Title: Re: Restricting the Mouse
Post by: AGP 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.
Title: Re: Restricting the Mouse
Post by: EgonOlsen 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... ???
Title: Re: Restricting the Mouse
Post by: AGP 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?
Title: Re: Restricting the Mouse
Post by: AGP on November 14, 2014, 05:03:53 pm
Egon? Was that too broad a question for your taste?
Title: Re: Restricting the Mouse
Post by: Irony 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/ (http://www.permadi.com/tutorial/raycast/)
Title: Re: Restricting the Mouse
Post by: AGP 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.
Title: Re: Restricting the Mouse
Post by: EgonOlsen 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) (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!?
Title: Re: Restricting the Mouse
Post by: AGP 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)?
Title: Re: Restricting the Mouse
Post by: EgonOlsen 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.
Title: Re: Restricting the Mouse
Post by: AGP on November 15, 2014, 09:43:08 pm
Hmmm. And there's nothing in Config to cause that? It's worth noting that I've set the zombie to Object3D.COLLISION_CHECK_OTHERS and the bullet to Object3D.COLLISION_CHECK_SELF | Object3D.COLLISION_CHECK_OTHERS.
Title: Re: Restricting the Mouse
Post by: EgonOlsen on November 15, 2014, 09:52:27 pm
No, there's nothing in Config about that. It's basic ray-polygon collision detection. It's proven, it doesn't go wrong until one uses it wrong...or not at all...maybe you can post the code snippet that uses it to see if you actually do.. ???
Title: Re: Restricting the Mouse
Post by: AGP on November 16, 2014, 06:56:45 am
Code: [Select]
     if (bullet != null && bullet.isAlive()) {
if (bullet.rayCollision(theWorld, deltaTime*60f)) {
     zombie1.triggerDeath(heroSphere);
}
bullet.translate(bullet.forward.x*deltaTime*60f, bullet.forward.y*deltaTime*60f, bullet.forward.z*deltaTime*60f);
     }

In bullet (Bullet is an Object3D subclass):
Code: [Select]
     public boolean rayCollision(World theWorld, float range) {
if (theWorld.checkCollision(getTransformedCenter(), forward, range*.4f) == Object3D.NO_OBJECT)
     return false;
return true;
     }
Title: Re: Restricting the Mouse
Post by: EgonOlsen on November 16, 2014, 09:52:19 am
Looks fine. Try to remove the -OTHERS from the bullet. Maybe it collides with itself this way and there's no point in setting it up this way anyway, because nothing else collides with a bullet, or does it.
Title: Re: Restricting the Mouse
Post by: AGP on November 25, 2014, 05:14:32 pm
It doesn't collide with anything else (no superheroes on this one! :- ). I fixed the mouse completely, but I'm still getting the issue in which the bullet sometimes goes through the zombies, and sometimes hits it when it shouldn't. Is there any chance that it could be a bones issue (maybe the collision mesh is one place while the mesh being rendered is somewhere else)?
Title: Re: Restricting the Mouse
Post by: EgonOlsen on November 25, 2014, 06:25:41 pm
Not unless you are doing one of these tasks in another thread then the other one. If all operations (i.e. animation, collision detection, rendering) happen in the same thread, it shouldn't be the reason.
Btw: I don't understand how you are even able to notice this flaw. How much off are the results? Or is the bullet really slow?
Title: Re: Restricting the Mouse
Post by: AGP on November 25, 2014, 06:35:44 pm
It's big enough and slow enough to notice. I'll speed it up when it's perfect.

BTW, watch the Jurassic World trailer. It's awesome.