2
\$\begingroup\$

Im implementing certain Camera movements in my turn based games. I'm able to place and rotate the camera between two units that attack each other. However it will always move to the same "side". Which isn't necessarily wrong, the rotation of the camera just seems to be sharp when it's on the other side of the half plane ( as of right now it always goes on one half plane). I would like the camera to move to one side relative to the cameras position.

Essentially I just have to flip the Normal which gives me the direction where I have to place my camera:
A and B are units.
C is the current camera position
T is the target position.
enter image description here
Essential code

center =  Vector3.Lerp(attacker.transform.position, target.transform.position, 0.5f);
Vector3 diff = attacker.transform.position - target.transform.position;
Vector3 normal = Vector3.Normalize(diff);
normal = Vector3.Cross(Vector3.up,normal);

//CHECK HALF PLANE HERE      
normal *= -1;

//Center is the target position.
center -=  normal * (diff.magnitude + 1.0f);
\$\endgroup\$
3
  • \$\begingroup\$ Usual way to implement camera transitions is to interpolate between the current and target matrices. Convert the rotations to quaternions and slerp between them, convert the translations to vector and lerp \$\endgroup\$ Jun 13 '13 at 9:45
  • \$\begingroup\$ @gareththegeek That's not the problem. I need to know on which half plane my camera is depending on the X and Z components. The plane should be defined by the difference of the two units. Where y = 0;. It's already tweening to the spot I want. It's just that it always moves to the same half plane, because the normal is always pointing in the same direction. Once I know the plane half the camera is on, I can reverse the direction of my normal. \$\endgroup\$
    – Sidar
    Jun 13 '13 at 9:46
  • \$\begingroup\$ The half planes should be defined* \$\endgroup\$
    – Sidar
    Jun 13 '13 at 10:01
2
\$\begingroup\$

If I have understood your question correctly, as units A and B start attacking each other, you want the camera to move to a target position with a certain elevation and at equal and set distances from both units, thus defining two possible targets, both at opposite sides of the plane defined by A, B and the up vector. You want to determine which side of that plane the camera is on, so you can select the nearest target.

As you have calculated the normal of this plane, the problem has already been solved for the most part. The dot product of the normal vector and the vector from A (or B) to the camera will be positive on the side the normal is pointing towards and negative on the other. If, by definition, the normal is always perpendicular to the z-axis, you can omit that component and simply calculate the two-dimensional dot product, e.g.:

//Center is the target position.
if ( ((camera.x - attacker.x) * normal.x) + (camera.y - attacker.y) * normal.y) < 0 ) normal *= -1;
center -=  normal * (diff.magnitude + 1.0f);

Personally, I would prefer to keep variables like 'normal' and 'center' intact for later use, but I hope this example code gets the point across.

\$\endgroup\$
4
  • \$\begingroup\$ Ok I had to test the z axis not the y one. ( y = 0 ); I knew i had to do something with the dot product, classes been too long ago. Time to refresh my algebra. Thanks! \$\endgroup\$
    – Sidar
    Jun 13 '13 at 13:41
  • \$\begingroup\$ @Sidar: It's arbitrary. It could be perpendicular to the banana-axis if you desire to label your axes so. \$\endgroup\$ Jun 13 '13 at 13:52
  • \$\begingroup\$ Marcks, Banana implies a curve. Can't use that. \$\endgroup\$
    – Sidar
    Jun 13 '13 at 19:33
  • \$\begingroup\$ @Sidar: Sure you can! It is conventional to use the banana, pogostick and beer axes in cylindrical coordinate systems. \$\endgroup\$ Jun 13 '13 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.