www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: DougFane on October 14, 2011, 03:07:33 pm

Title: How to attach an oject to the camera
Post by: DougFane on October 14, 2011, 03:07:33 pm
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?
Title: Re: How to attach an oject to the camera
Post by: AGP on October 14, 2011, 07:19:00 pm
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).
Title: Re: How to attach an oject to the camera
Post by: EgonOlsen on October 14, 2011, 09:27:22 pm
Yes, the fps example contains some code for this. It's pretty simple:

Code: [Select]
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.
Title: Re: How to attach an oject to the camera
Post by: AGP on October 14, 2011, 09:32:52 pm
I assume the code that does it is:
Code: [Select]
weapon.align(camera);

So what does weapon.getTranslationMatrix().setIdentity() do in this context?
Title: Re: How to attach an oject to the camera
Post by: EgonOlsen on October 14, 2011, 09:38:55 pm
The same thing as weapon.clearTranslation();. The example is old and doesn't use some methods that have been added afterwards.
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 15, 2011, 11:31:13 pm
how could you explain a little further what each of those lines does. I'm not entirely sure I get it.
Title: Re: How to attach an oject to the camera
Post by: AGP on October 16, 2011, 12:13:39 am
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
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 16, 2011, 12:43:53 am
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.
Title: Re: How to attach an oject to the camera
Post by: AGP on October 16, 2011, 12:53:00 am
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.
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 16, 2011, 01:59:51 am
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.
Title: Re: How to attach an oject to the camera
Post by: EgonOlsen on October 16, 2011, 01:05:58 pm
Make sure that you call build() after the rotateMesh or otherwise, your rotation pivot might be off.
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 16, 2011, 03:35:08 pm
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.
Title: Re: How to attach an oject to the camera
Post by: EgonOlsen on October 16, 2011, 08:37:57 pm
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?
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 17, 2011, 02:11:59 am
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.
Title: Re: How to attach an oject to the camera
Post by: EgonOlsen on October 17, 2011, 07:21:32 am
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.
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 17, 2011, 03:31:18 pm
I think I know what the issue is, and I don't know if this is what you said, but here it is:

_______   is the gun

<  is the camera

faced straight ahead:

_______
       <

faced 90 degrees:
       |
       |
       ^
       |

ans so on...

I think this is what was making me think it was moving in a circular motion, because the gun appears to recede and advance. Is this what you were referring to, and is altering the pivot point going to solve it?
Title: Re: How to attach an oject to the camera
Post by: AGP on October 17, 2011, 04:58:31 pm
Yes and yes. Does simply calling weapon.setRotationPivot(camera.getPosition()) not work?
Title: Re: How to attach an oject to the camera
Post by: EgonOlsen on October 17, 2011, 05:20:15 pm
That would mix camera and object space, which isn't supposed to work very well. Can you post the current value of getRotationPivot()?
Title: Re: How to attach an oject to the camera
Post by: DougFane on October 18, 2011, 08:07:39 pm
Well first I would like to say, thank you for all the help. Unfortunately, I accidentally overwrote the file of the project I was working on, and post the value that you are asking for. the good news is that while writing test code for the FPS project I was working on, I thought of an alternate game type that seemed a more worthy venture. Rather than create another shooter, I am going for more of a puzzle/adventure type game. The basic premise is that you are trying to get to a spinning monkey head (don't ask) on a platform. You can run and jump like usual, but you can also shoot large red balls which ca be used as stepping platforms. I will make a new post with a zip file attached that contains a fully functional (albeit simple) version of the game.

Thank you for the help Egon and AGP