How to attach an oject to the camera

Started by DougFane, October 14, 2011, 03:07:33 PM

Previous topic - Next topic

DougFane

I am trying to make a first-person shooter type program. I have gotten gravity, movement, and looking to work. But I am struggling to get the weapon to stay with the camera. I want the weapon to stay on the right side of the screen and move with the camera like you see in most first person shooters. But I can't get it to work. Does anybody have any ideas?

AGP

Have a look at the fps demo that comes with jpct. Maybe Egon could tell you what he did there (I recall not really getting it: he translates the camera and weapon, rotates the weapon and resets both its matrices and in no way attaches the camera to the weapon, or vice-versa).

EgonOlsen

Yes, the fps example contains some code for this. It's pretty simple:


weapon.getTranslationMatrix().setIdentity();
weapon.translate(camera.getPosition());
weapon.align(camera);
//weapon.rotateAxis(camera.getDirection(), (float) Math.sin(timerTicks/6f)/20f); // Makes the weapon move slightly


This places the weapon in the center of the camera. If you don't want that, add a translation to the left or right by getting the x-axis from the camera and do a translation along that vector.

AGP

I assume the code that does it is:

weapon.align(camera);


So what does weapon.getTranslationMatrix().setIdentity() do in this context?

EgonOlsen

The same thing as weapon.clearTranslation();. The example is old and doesn't use some methods that have been added afterwards.

DougFane

how could you explain a little further what each of those lines does. I'm not entirely sure I get it.

AGP

weapon.clearTranslation();//CLEARS ANY TRANSLATION OFF OF THE OBJECT'S TRANSLATION MATRIX (YOU USUALLY USE THIS AFTER CALLING translateMesh() SO THAT THE MATRIX IS CLEARED BUT THE OBJECT STAYS WHERE YOU PUT IT)
weapon.translate(camera.getPosition());//WITH THE TRANSLATION MATRIX AT ORIGIN, translate(camera.getPosition()) PLACES WEAPON WHERE THE CAMERA IS
weapon.align(camera);//"ATTACHES" THE OBJECT'S ROTATION TO THE CAMERA

DougFane

Ok, I got the weapon to stay with the camera, but it doesn't rotate properly. I can get it to either rotate with the camera, but it is pointed straight down, or have it pointing the right way, but it is only viewable when you are looking south in the game.

AGP

I expect that by "south" you mean you're looking at the floor. Is that right? Anyway, do weapon.rotateX(Math.PI*.5) (or negative PI*.5) and then call weapon.rotateMesh() and weapon.clearRotation(). Do that right after importing weapon and before calling build(). Tell me if that does it.

DougFane

#9
Thank you, that helped quite a bit. I actually had to rotate it  by -PI * 2. Now I have a new problem. When the camera spins, the gun kind of slides. I have attached a series of screen shots to show what I mean. When taking these I just rotated the camera for about 1/4 of a second and then took  a screen shot until I came back to the origin. NOTE: I can't figure out how to upload the rar file. So I will just describe the issue. Basically, the object starts on the left side of the screen, and then gradually slides to the middle, then to the right, then back to the middle, then back to the right.

EgonOlsen

Make sure that you call build() after the rotateMesh or otherwise, your rotation pivot might be off.

DougFane

I am calling build, and the weapon is rotated correctly. It just moves in a circular track around a point just in front of the camera.

EgonOlsen

I'm not sure that your pre-rotation of the weapon is actually correct. You wrote that you had to rotate it -2*PI...that doesn't make much sense to me as this is a rotation of 360°, which means that it should end up where it started regardless of the rotation pivot. What AGP wrote seems actually like the right way to solve the "looking straight down" issue. Can you post a simple test case?

DougFane

Well I got the 2 * pi idea from the fps example included with the jars. I am loading 3ds objects that are rotated the same way in the file as the example items. Also, I have narrowed down the problem to the line: weapon.align(camera);

Without this line, the item doesn't rotate properly with the camera, but it doesn't move. With this line, it rotates with the camera, but also moves along a circular track.

EgonOlsen

That's because this line sets the rotation matrix. Your rotation pivot is obviously off. You can set it after calling build. Maybe you can try something like the origin or any other reasonable value for your case and see if that helps.