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
}
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)
How do you prevent the mouse from going out of the frame, for starters? Or does it always have to play in full screen?
Mouse.setGrabbed(true);
Ah, so I should only use lwjgl's Mouse instead of AWT's? So, does that prevent me from using the software renderer?
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.
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.
I never had any issues with it. What can i say...that's what i did and it worked fine for me... ???
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?
Egon? Was that too broad a question for your taste?
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/)
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.
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,%20com.threed.jpct.SimpleVector,%20float)). Or maybe i go the actual question wrong!?
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)?
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.
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.
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.. ???
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):
public boolean rayCollision(World theWorld, float range) {
if (theWorld.checkCollision(getTransformedCenter(), forward, range*.4f) == Object3D.NO_OBJECT)
return false;
return true;
}
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.
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)?
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?
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.