1

I'm making a 3D-game using C++ and Irrlicht. It is a FPS-style game, so player should be able to carry weapons. But I've been struggling with calculating the position relative to the camera. If camera wouldn't rotate, calculation would be easy:

// node is camera's child
vector3df modifier = vector3df(2, 0, 2);
node->setPosition(node->getPosition() + modifier);

But however, camera isn't a static but a rotating object, so things are a bit more complicated. Here's an image which hopefully crearifies what I'm trying to say:

A picture tells more than a thousand words

This should work in all dimensions, X, Y and Z. I think there's only two trigonometric functions for this kind of purpose, sine and cosine, which are for calculating X and Y coordinates. Am I at the wrong path or can they be applied to this? Or is there a solution in Irrlicht itself? There's the code which I've tried to use (found it from SO):

vector3df obj = vector3df(2, 0, 2);
vector3df n = vector3df(0, 0, 0);
n.X = obj.X * cos(60 * DEGTORAD) - obj.Z * sin(60 * DEGTORAD);
n.Z = obj.Z * cos(60 * DEGTORAD) + obj.X * sin(60 * DEGTORAD);
node->setPosition(node->getPosition() + n);

But the weapon just flies forward.

I would be glad for any kind of help or guidance.

P.S. Hopefully this question is clearer than the previous one

Elias Kosunen
  • 433
  • 7
  • 20
  • 1
    This isn't a programming question (per se), this is more of a mathematics question. – Cory Kramer Apr 27 '15 at 17:25
  • Yeah, that's actually true. Shold I ask this again there? – Elias Kosunen Apr 27 '15 at 17:27
  • I would suggest so. You'll have users more familiar with this type of geometry there (not that you couldn't get lucky and find some here too). Once you are able to write down the equations on paper, you can ask here if you get stuck implementing it in code. – Cory Kramer Apr 27 '15 at 17:29
  • It would be worth taking the time to clarify this question first. You have a camera that pans around horizontally, but not tilt up or down. You have an object somewhere. You know the (x,y) coordinates of the camera and the object, and also the angle of rotation of the camera, and you are trying to calculate... what? – Beta Apr 27 '15 at 17:30
  • Actually, tags are clear enough I think. Both mathematicians and programmers can look at it. – erol yeniaras Apr 27 '15 at 17:30
  • @erolyeniaras But that is implying that mathematicians are perusing Stack Overflow, and aren't hanging out in [Math.SE](http://math.stackexchange.com/) – Cory Kramer Apr 27 '15 at 17:31
  • @Cyber: This is in the grey zone in my opinion.I would certainly agree with you if the question was a purely mathematical.. However, it is still a programming question with a mathematical issue involved. Math and 3d tags make it pretty clear I believe. – erol yeniaras Apr 27 '15 at 17:36
  • This is a standard issue in graphics. I think it belongs here, but the question is unclear about what you mean by "calculating the position relative to the camera" – QuestionC Apr 27 '15 at 17:37
  • Do you mean you have an object whose coordinates are defined relative to the camera, but you need to translate those to real-world coordinates? Like the hand holding a gun in a fps? – QuestionC Apr 27 '15 at 17:39
  • @QuestionC That's kinda what I meant, I just wasn't able to verbalise it. I'll edit my question. – Elias Kosunen Apr 27 '15 at 17:56
  • My guess is that the second line `obj.Y = obj.Y * cos(60 * DEGTORAD) + obj.X * sin(60 * DEGTORAD);` is wrong because `obj.X` has just been modifed. Store `obj.X` before modifying it ! `temp=obj.X;obj.X = obj.X * cos(60 * DEGTORAD) - obj.Y * sin(60 * DEGTORAD); obj.Y = obj.Y * cos(60 * DEGTORAD) + temp * sin(60 * DEGTORAD);` – francis Apr 27 '15 at 17:56
  • @user3725053: I have an answer for you. Hopefully it might help with your code. – erol yeniaras Apr 27 '15 at 19:00

1 Answers1

1

The problem with your code is that the rotation is performed around the origin, not around the camera.

What you want to do is to rotate the object (weapon) around the center of the camera by the angle that the camera rotates:

In order to do that you need to perform the following steps:

1 - Translate all the points so that the center of the camera is at the origin,

2 - Apply the rotation matrix (angle is alpha):

[cos (alpha)   -sin (alpha)]  
[sin (alpha)    cos (alpha)] 

3 - Undo the step 1 on the rotated point.

Sample algorithm:

Position of the weapon: (xObject, yObject)
Position of the camera: (xCamera, yCamera)
Turning angle: alpha    

  //step 1: 
  xObject -= xCamera;
  yObject -= yCamera;

  // step 2
  xRot = xObject  * cos(alpha) - yObject  * sin(alpha);
  yRot = xObject * sin(alpha) + yObject * cos(alpha);

  // step 3:
  xObject = xRot + xCamera;
  yObject = yRot + yCamera;

This algorithm is on XY plane but can be modified for XZ plane. Assuming that in your code obj represent the position of the weapon. Your code can be something like:

...
// Step 1
obj.X-=cam.X;
obj.Z-=cam.Z;

//Step 2
n.X = obj.X * cos(60 * DEGTORAD) - obj.Z * sin(60 * DEGTORAD);
n.Z = obj.Z * cos(60 * DEGTORAD) + obj.X * sin(60 * DEGTORAD);

// Step 3
obj.X = n.X + cam.X;
obj.Z = n.Z + cam.Z;
...

Hope that helps!

erol yeniaras
  • 3,701
  • 2
  • 22
  • 40