Math Question - Calculate position

Hello Guys,
Given two vector3f , I would like to find the middle point (vector3f) and have the camera look at that point (straight - 90 degrees) from a given distance (see pic below)

My requirement is to calculate the camera position.
I’ll be glad to hear your opinion about the best way to achieve that.

Thank you

1 Like

Hi,

this should be relatively easy thanks to JME’s awesome math and vector libraries:

// Midpoint is half way from vector 1 to 2
Vector3f delta = new Vector3f( vector2.subtract( vector1 ) );
Vector3f midpoint = new Vector3f( vector1.add( delta.mult( 0.5f ) ); // Could also use vector1.interpolateLinear( vector2, 0.5f );

// Cross product of two unit length vectors gives orthogonal result
Vector3f camdir = new Vector3f( delta.normalize() );
camdir.crossLocal( Vector3f.UNIT_Y );

Vector3f campos = new Vector3f( midpoint.add( camdir.mult( distance ) );

cam.setLocation( campos );
cam.lookAt( midpoint, Vector3f.UNIT_Y );

Just typed this off my head so excuse any typos…
Also be aware about when to use “local” methods that change the existing vector vs. methods that create new vectors.

Good luck!

3 Likes

Note that in 3 dimensions there are an infinite number of solutions to the stated problem, since the camera can be located at any point on a circle surrounding the midpoint. Even in 2 dimensions, there are 2 solutions, depending on whether you want point1 to be on the camera’s left or on its right.

3 Likes

Yeah that’s correct!

In my snippet I did the cross product with the UNIT_Y vector, so that effectively limits the possibility space to the XZ plane. You could then switch between the two possible solutions by negating the resulting direction vector.

3 Likes

I was thinking , to do this as in plain math you can first create a prototype

1-Length of the Resultant vector (L)
2-Length(L) = L/2 to get the middle point
3-then cam.lookAt(L/2,L.getY());

so in JME , i think it would be

//get the mid point between 2 vectors pointing in the same direction
Vector3f midVector=v1.add(v2).divide(2f);
//in different directions Vector3f midVector=v1.subtract(v2).divide(2f);
//adjust camera position to look at the point
camera.lookAt(midVector,midVector.getY());

that what i thought of.

Given that the general solution is impossible. Can you tell us what you are actually trying to do so that we can bound the problem appropriately?

Is 2D already a good assumption? (In which case this was relatively trivial so I’m not sure.)

2 Likes

I have two fighters in my game moving in the scene. I keep them looking at each other. I want the camera to have a good optimized side view catching both of them in the middle of the view .
I thought that having the camera look (or chase) the middle point between those fighters will yield best result.

3 Likes

In that case, we probably can reduce the problem to just 2 dimensions (just place the camera the average y position of the two fighters). In that case, just obtain the global positions of the fighters subtract one from the other to get your distance. Cross that with Vector2f.UNIT_Y to get a starting position to your camera. Multiply it by your desired distance. Then, add to that vector first by the half-distanced vector between your two fighters (the one you calculated originally), and then again add the global position of your first fighter. In fact, it will actually look a lot like @Pauli’s code.

1 Like

It took quite a few trial & error to tune the camera and it turned out that my initial question was a little naïve :slight_smile:
Anyway I have managed to create my own kind of “chase camera” with lots of control on its behavior so it will surely fit this game

3 Likes