Advertisement Jump to content
Sign in to follow this  

help with this algebra.

This topic is 3874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can slide against a plane with this algebra, but it has some strange behaviour, no mather of what i do with the z variables in my segment the distance changes. it seems like the segment always appears at z=0. This is ALL code for this. Vector3 A = new Vector3(38, 1, -72); Vector3 B = new Vector3(25, 1, -85); float lengthToPlane = Vector3.Dot(utility.GetNormalVector(A - B), objCamera.mPos); if ((lengthToPlane < 0) && PlayerInsideSegment(objCamera.mPos)) { move_v += utility.GetNormalVector(A - B) * (0 - (lengthToPlane)); } my plan is to decide the length to the plane/segment and if player is behind then push him infront of the plane (and sliding occurs automatically) Here is the code for my utility.GetNormalVector: public static Vector3 GetNormalVector(Vector3 v) { Vector3 w = Vector3.Normalize(v); Vector3 n = new Vector3(0, 0, 0); n.X = -w.Z; n.Z = w.X; n.Y = w.Y; return n; }

Share this post

Link to post
Share on other sites
I'd like to clarify what I think I understand or don't understand:

1) You say plane, but you are really solving a 2D problem.
2) A and B are the endpoints of a line segment...your 2D "plane"
3) Your GetNormalVector uses 2D logic to find the vector perpendicular to the segment, e.g., you are finding the 2D normal
4) I do not know how your PlayerInsideSegment code works, so I can't tell you whether that works or not.
5) I have no idea what you mean by "It seems like the segment always appears at z=0."

If 1-3 are correct then here are sone conclusions, assuming your PlayerInsideSegment works correctly (which I cannot tell):

A) Your lengthToPlane calculation is wrong.

Try this:

float lengthToPlane = Vector3.Dot(utility.GetNormalVector(A-B), objCamera.mPos - A);

(It is necessary to subtract either A or B away from objCamera.mPos. Otherwise, you are getting the distance from the camera to a line throught the origin (0,0,0) that is parallel to the segment from A to B.)

B) Your move_v += code looks correct, depending on what you do with it later and what you have done with it before getting to this line.

If you simply replace "move_v +=" with "objCamera.mPos +=", then the camera should snap to the segment.

If you apply move_v as a velocity, whether this does what you want will depend on how you apply the velocity. And, actually, if you do this, then that lengthToPlane might not be exactly what you want, though its sign (+/-) is applied correctly here.

Share this post

Link to post
Share on other sites

Im sorry for my bad explanation of my problem, but ALL your statements are correct and i solved my problem just by adding:

float lengthToPlane = Vector3.Dot(utility.GetNormalVector(A - B), objCamera.mPos-A);

instead of

float lengthToPlane = Vector3.Dot(utility.GetNormalVector(A - B), objCamera.mPos);

This was the main problem, i think i can work it out from here, thank for all other advise, im from sweden and my english is bad, so i am very impressed that you could solve my problem and most of all Understanding my porblem! =)

it really is kind of 2D instead of 3D because as you say it is only 2 points within a line segement, not a real 3D plane. =)

Share this post

Link to post
Share on other sites
I'm happy that you solved the main problem!

Your English is far better than my Swedish! I do speak/write French somewhat well, but I've found that Europeans are always better at speaking and writing English than Americans are at speaking and writing foreign languages. I've never been to Sweden, but hope to visit someday.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!