2

I have two objects in a game, which for this purpose can be considered points on a 2d plane, but I use Vector3s because the game itself is 3d.

I have a game camera which I want to align perpendicularly (also on the plane) to the two objects, so that they are both in view of the camera. Due to the nature of the game, the objects could be in any imaginable configuration of positions, so the directional vector between them could have any direction.

Part1: How do I get the perpendicular angle from the two positional vectors?

I have:

Vector3 object1Position; // x and z are relevant
Vector3 object2Position;

I need:

float cameraEulerAngleY;

Part2: Now, because of the way the game's assets are modelled, I want to only allow the camera to view within a 180 degree 'cone'. So if the camera passes a certain point, it should use the exact opposite position the above math might produce.

An image is attached of what I need, the circles are the objects, the box is the camera.

example cases

I hope this post is clear and you guys won't burn me alive for being total rubbish at vector math :P

greetings, Draknir

svick
  • 236,525
  • 50
  • 385
  • 514
Casper
  • 429
  • 1
  • 6
  • 14
  • 1
    this question is probably OK being here, but you'd actually probably get better results on math.SO – Phillip Schmidt Jul 30 '12 at 14:11
  • 1
    [This](http://stackoverflow.com/questions/1243614/how-do-i-calculate-the-normal-vector-of-a-line-segment) could help you with the first part – Steve Jul 30 '12 at 14:19
  • @PhillipSchmidt I'll give it a try, is there a way to move this post over, or do I need to repost? – Casper Jul 30 '12 at 14:36
  • @Draknir as far as I know only moderators can move posts. I could be wrong, but I'd just post a copy of this over there as well (no rules against duplicate posts across different SO servers) – Phillip Schmidt Jul 30 '12 at 14:39
  • no images allowed on the math SO.. hope this one gets an answer! – Casper Jul 30 '12 at 14:53
  • I don't think math.SO would be interested. Could you draw in the angle you want? – Beta Jul 30 '12 at 15:39

3 Answers3

1

You'll need to specify a distance from the object line, and an up vector:

Vector3 center = 0.5 * (object2position + object2position)
Vector3 vec12 = object2position - object1position

Vector3 normal = Cross(vec12, up)
normal.Normalize()
Vector3 offset = distance * normal

Vector3 cameraA = center + offset
Vector3 cameraB = center - offset
< choose which camera position you want >

Instead of using Euler angles, you should probably use something like LookAt() to orient your camera.

comingstorm
  • 25,557
  • 3
  • 43
  • 67
1

Assuming Y is always 0 (you mentioned "X and Z" are your relevant components), then you can use some 2-d math for this:

1.Find any perpendicular vector (there are two). You can get this by calculating the difference between the two vectors, swapping the components, and negating one of them.

Vector3 difference = (object1Position - object2Position);
Vector3 perpendicular = new Vector3(difference.z, 0, -difference.x);

2.Using your separating plane's normal, flip the direction of your new vector if it's pointing opposite of intended.

Vector3 separatingPlaneNormal = ...; // down?
if(Vector3.Dot(separatingPlaneNormal, perpendicular ) < 0)
{
    perpendicular = -perpendicular ;
}

// done.
DuckMaestro
  • 15,232
  • 11
  • 67
  • 85
0

Well, for the first bit, if you have points (x1, y1) and (x2, y2) describing the positions of your objects, just think of it in terms of triangles. The angle you're looking for ought to be described by

arctan((y2-y1)/(x2-x1))+90

I don't completely understand what you want to do with the second part, though.

bigbenbt
  • 367
  • 2
  • 14