Author Topic: How to attach an oject to the camera  (Read 4864 times)

Offline DougFane

  • byte
  • *
  • Posts: 8
    • View Profile
How to attach an oject to the camera
« 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?

Offline AGP

  • quad
  • ******
  • Posts: 1638
    • View Profile
Re: How to attach an oject to the camera
« Reply #1 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).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12011
    • View Profile
    • http://www.jpct.net
Re: How to attach an oject to the camera
« Reply #2 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.

Offline AGP

  • quad
  • ******
  • Posts: 1638
    • View Profile
Re: How to attach an oject to the camera
« Reply #3 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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12011
    • View Profile
    • http://www.jpct.net
Re: How to attach an oject to the camera
« Reply #4 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.

Offline DougFane

  • byte
  • *
  • Posts: 8
    • View Profile
Re: How to attach an oject to the camera
« Reply #5 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.

Offline AGP

  • quad
  • ******
  • Posts: 1638
    • View Profile
Re: How to attach an oject to the camera
« Reply #6 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

Offline DougFane

  • byte
  • *
  • Posts: 8
    • View Profile
Re: How to attach an oject to the camera
« Reply #7 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.

Offline AGP

  • quad
  • ******
  • Posts: 1638
    • View Profile
Re: How to attach an oject to the camera
« Reply #8 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.

Offline DougFane

  • byte
  • *
  • Posts: 8
    • View Profile
Re: How to attach an oject to the camera
« Reply #9 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.
« Last Edit: October 16, 2011, 02:37:06 am by DougFane »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12011
    • View Profile
    • http://www.jpct.net
Re: How to attach an oject to the camera
« Reply #10 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.

Offline DougFane

  • byte
  • *
  • Posts: 8
    • View Profile
Re: How to attach an oject to the camera
« Reply #11 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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12011
    • View Profile
    • http://www.jpct.net
Re: How to attach an oject to the camera
« Reply #12 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?

Offline DougFane

  • byte
  • *
  • Posts: 8
    • View Profile
Re: How to attach an oject to the camera
« Reply #13 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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12011
    • View Profile
    • http://www.jpct.net
Re: How to attach an oject to the camera
« Reply #14 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.