|
0 registered members (),
1,012
guests, and 8
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
Math question
#168953
11/22/07 22:03
11/22/07 22:03
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
I have position P which is anywhere in an x,y,z world, but always between the length of point A and B: Code:
P A \ . \ . . . B
What I need is length n between P and the closest point to P on the length of A and B (so I need the lenght of the the \ line). You know the values of all positions, but what's the formula? EDIT: gah nevermind, it's not leading me anywhere lol I better explain my whole plan. My idea is to create a simple to use 3rd person camera system for outside levels. The idea is to place several entities around the level, which function as camera navpoints. The player walks around under the navpoints, and while he does so, the camera moves in a certain "pattern" between the 3 closest camera navpoints to the player. Now I am having a little problem defining this "pattern". I first wanted to move the camera between the two closest points around the player, in a curve where the radius is stretched to the player position. All movement in the image are moving simultaniously and with equal speeds:  But, after thinking a little more, this would cause severe camera bugging when all of a sudden a different camera navpoint gets one of the two closest to the player. So then I was thinking, when every change in a curve would always go through the camera position, there wont be any bugs and the camera should move smoothly to the next navpoint. After drawing and sketching a little, I just got totally stuck. I couldn't imagine anymore how the camera should go through those navpoints while staying near the player and not behave buggy. I still want to think this out, and if anyone wants to throw an idea in the ring, or can bring up a clear explanation, I'd be very happy to listen  . Meanwhile I experiment further and if I got something to work, I'll just post it here until it's ready (if it ever will) for user contributions.
Last edited by Joozey; 11/23/07 00:06.
|
|
|
Re: Math question
[Re: Joozey]
#168954
11/23/07 02:10
11/23/07 02:10
|
Joined: Jul 2004
Posts: 1,205 Greece
LarryLaffer
Serious User
|
Serious User
Joined: Jul 2004
Posts: 1,205
Greece
|
Here's an idea. If you want the camera to move on a certain path, use the camera navpoints as Nodes in a pathfinding algorithm. The camera should strive to be in a certain distance from the player, and always have line of sight with him. To do so, it will need to find a path between the cam Navpoints that will get him there. You can later transform the straight lines between navpoints with spline curves for smoother camera movement. For a working pathfinding algorithm you can use Intense Pathfinding, which is somewhere in the contribution forum but that was just an idea. It may make things more complicated than they're already are. If I were you i'd look the net on how professionals tackle this problem, then copy-cat 
|
|
|
Re: Math question
[Re: LarryLaffer]
#168955
11/25/07 12:26
11/25/07 12:26
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Thats a good idea. I've tried it a little. -The camera faces the player at all times. -The camera switches to the navigationpoint closest to the player. -The second closest navigationpoint is known, and there should be somehow a path from the first to the second. When the player moves, and two different navigationpoints become first and second, there should be a path from first to second. As long as the player moves and those two points remain closest and second the path should be maintained so the camera can move over it. As soon as theres a change in navigationpoints the path should be recalculated. I'm trying with two positions currently, and I can make a path from one to the other, however, to move the camera correctly over it, I need to somehow see how far the player has walked towards the second point in percentages, knowing that the distance between point A and B is 100%. Difficult picture:  So, the camera travels a max of 1200, using vec_lerp will make it easier with 0..100%. Now, how far has the player travelled from beginning to the end, or actually, what IS the begin and the end? I guess the begin would be as if the player would stand under navpoint 1, and the end if it would stand right underneath navpoint 2. There should be a calculation meassuring the distance the player travelled as if it were on a linear line between A and B, no matter where the player stands on the ground, but I couldn't get the calculation to work. Another thing is, that if the player goes to the furthest point, it would stand underneath B... but that means B is the closest, thus A will be the end point... So I guess that wouldn't work at all  $%^&@#$%^& I Guess I need a different approach.
Last edited by Joozey; 11/25/07 12:28.
|
|
|
Re: Math question
[Re: Joozey]
#168956
11/25/07 14:34
11/25/07 14:34
|
Joined: Jul 2004
Posts: 1,205 Greece
LarryLaffer
Serious User
|
Serious User
Joined: Jul 2004
Posts: 1,205
Greece
|
I guess your approach could work.. In your example, Nav1 is the closest navpoint so that would be 0%. Nav2 is the second closest, so it would be 100% like you said. Then at the location that the player is at the picture, the percentage of the camera would be: CamPerc=(playerDistFromClosest/(playerDistFromClosest+playerDistFromSecondClosest))*100; which would be CamPerc=(1400/3400)*100 which is 41 something.. So with some code that you probably already know... Code:
DistanceFromClosestNav=(vec_dist(ClosestNavPoint.x,SecondCloseNavPoint.x)*CamPerc)/100; //In your example, DistanceFromClosestNav would now be 492, since CamPerc is 41 and the vec_dist would return 1200
CalculatePosBetween(ClosestNavPoint.x,SecondCloseNavPoint.x,DistanceFromClosestNav,camera.x);
function is_CalculateMidPoint(var* vStartVec,var* vEndVec,distanceFromStart,var* vResultVec) { var is_CalcMidP_Direction[3];
//compute (StartVec-EndVec)/||(StartVec-EndVec|| //to get normalized vector heading from StartVec to EndVec vec_diff(is_CalcMidP_Direction,vEndVec,vStartVec); vec_normalize(is_CalcMidP_Direction,1); //StartVec+distance*Direction to get MidPoint vec_scale(is_CalcMidP_Direction,distanceFromStart); vec_set(vResultVec,vStartVec); vec_add(vResultVec,is_CalcMidP_Direction); }
This will work. When the player is equadistant with Nav1 and Nav2, anyone could be chosen as the closest. When player gets closer to Nav2, then that would be set as the closest but that doesn't break the algo. In steps, while player walks from Nav1 to Nav2, the CamPerc variable will go like that: 0%, 10%, 20%, 30%, 40%, 50%, 40%, 30%, 20%, 10%, 0% I have to admit though, I haven't thought this through. Usually when I attempt to solve type of program, i do a ton of research first. For Camera scripting, i think there was a good article on Game Programming Gems..
|
|
|
Re: Math question
[Re: Joozey]
#168958
11/26/07 10:24
11/26/07 10:24
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
It didn't work perfect. When the player walked exactly over the line I had the right value, however this is, ofcourse, often not the case. Thanx to my cousin I got this formula for calculating the closest point to the player on the line: Quote:
navpoint1 + (dotproduct((p1 - player) * (p1 - p2)) / dotproduct((p1 - p2) * (p1 - p2)) * (p1 - p2))
which follows into the following calculation: Code:
VECTOR* vec_substract (VECTOR* vect1, VECTOR* vect2) { VECTOR vec1; vec_set (vec1, vect1); return (vec_sub (vec1, vect2)); }
...
VECTOR* navpoint1 = vector (cameraNavpoint_array[closestNavpoint].x, cameraNavpoint_array[closestNavpoint].y, cameraNavpoint_array[closestNavpoint].skill30); VECTOR* navpoint2 = vector (cameraNavpoint_array[scndClosestNavpoint].x, cameraNavpoint_array[scndClosestNavpoint].y, cameraNavpoint_array[scndClosestNavpoint].skill30); VECTOR vec_u; vec_set (vec_u, vec_substract(navpoint2.x, navpoint1.x)); VECTOR vec_y; vec_set (vec_y, vec_substract(player.x, navpoint1.x)); var scale_number = vec_dot (vec_y, vec_u) / vec_dot (vec_u, vec_u); vec_set (tempVec, vec_u); vec_scale (tempVec, scale_number); vec_add (tempVec.x, navpoint1.x);
tempVec now has the correct value on the line, which I place the camera on with a fixed z value. Now I need to smoothly blend it over into a different line.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
|