Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (574) Games in Android Showcase (154) games submitted by our members Games in WIP (620) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1] 2
 ignore  |  Print
 Getting distance of a point in a 2d triangle without calculating perpendicular..  (Read 7282 times) 0 Members and 1 Guest are viewing this topic.
CyanPrime
 « Posted 2012-05-29 21:55:17 »

Alright, so I'm trying to move a ball to it's closest location along a line that it hits. But my dist.y is saying it's extremely far away even when I'm right next to the line. Can anyone tell me whats going on?

Here is the code:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29 public Vector2f LineVsCircleCollision(Vector2f lineA, Vector2f lineB, Vector2f circle, float rad)   {      Vector2f line = new Vector2f(0,0);     // the line vector      Vector2f.sub(lineB, lineA, line);       Vector2f circ = new Vector2f(0,0);     // the vector of the origin line point to the circle center       Vector2f.sub(circle, lineA, circ);              // project the circle vector onto the line vector       float proj = Vector2f.dot(line,circ) / Vector2f.dot(line,line);       if(proj < 0)   proj = 0;       else if(proj > 1)   proj = 1;       // apply the projected position to our vector, and offset it by our origin       Vector2f nearest = new Vector2f(0,0);       line.scale(proj);       Vector2f.add(line, lineA, nearest);       // 'nearest' is now the point on the line nearest the circle       //  see if the distance between this point and the circle center is greater than the radius.       //   if the radius is larger, the circle intersects       Vector2f dist = new Vector2f(0,0);       Vector2f.sub(circle, nearest, dist); // the vector between the circle and the nearest point on the line              System.out.println("hit: " + (Vector2f.dot(dist,dist) <= (rad*rad)));       System.out.println("x: " + dist.x + " y: " + dist.y);       return dist;   }

Axeman

Senior Devvie

Medals: 7

 « Reply #1 - Posted 2012-05-30 00:47:25 »

I think your problem with the large y values could be that you are scaling the line, but since the circle radius is perpendicular to the line you should be interested in scaling the normalized normal instead.

I´m wondering about the projection though... It´s not supposed to be

Line DOT circ / Line DOT line, right? Isn´t projection

Line DOT circ / Line^2? Or am I missing something..?
CyanPrime
 « Reply #2 - Posted 2012-05-30 05:06:39 »

Alright, so I got this code, and it doesn't seem to work on all the angles. Like the ball jumps back when touching the top, the sides seem to work fine in that the ball goes right up to the wall of the square , and the bottom it jumps back up to where the top jumps back to. Can someone tell me what I'm doing wrong?

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 public Vector2f GetClosestPoint(Vector2f start, Vector2f end, Vector2f pos){      Vector2f start_to_point = new Vector2f(0,0);      Vector2f.sub(pos,start,start_to_point);            Vector2f start_to_end = new Vector2f(0,0);      Vector2f.sub(end, start, start_to_end);      float t =  Vector2f.dot(start_to_point,  start_to_end) / start_to_end.lengthSquared();            // The normalized "distance" from a to                                            //   your closest point         return new Vector2f(start.x + start_to_end.x*t, start.y + start_to_end.y*t );                                            // Add the distance to A, moving                                            //   towards B   }
actual

JGO Coder

Medals: 24

 « Reply #3 - Posted 2012-05-30 05:22:32 »

I assume this is looking at a line that goes from start to end and is finding the closest point to pos? What library is your Vector2f coming from? The ones I googled (jME, slick, and java3d) don't have 3 parameter static sub methods. Also can you describe a bit more about where it fails? I'm finding it a bit hard to follow.
CyanPrime
 « Reply #4 - Posted 2012-05-30 05:25:00 »

it's 4 lines, making up a square, actually. The lib is LWJGL, and it fails when the ball touches the top, and bottom lines. The side lines work just as they should.
CyanPrime
 « Reply #5 - Posted 2012-05-30 07:48:05 »

Okay, I got it to work right as intended, but now I'm having trouble with the next step, making it so it pushes the ball slightly out the the line based on the direction it came from (so it'll slide on the wall if it keeps trying to go the same directional vector) IE:

 1  2 ball -> || <- wall       / |

This is the working code I have  that gets the player right on the edge of any angle:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 public Vector2f GetClosestPoint(Vector2f start, Vector2f end, Vector2f p){      Vector2f start_to_point = new Vector2f(0,0);      Vector2f.sub(p,start,start_to_point);            //Storing vector A->P      Vector2f start_to_end = new Vector2f(0,0);      Vector2f.sub(end, start, start_to_end);            //Storing vector A->B      float t =  Vector2f.dot(start_to_point,  start_to_end) / start_to_end.lengthSquared();            // The normalized "distance" from a to                                            //   your closest point      System.out.println("t: " + t );               start_to_end.scale(t);      Vector2f fin = new Vector2f(0,0);      Vector2f.add(start, start_to_end, fin);      System.out.println("fin.x: " + fin.x + " fin.y: " + fin.y);            return new Vector2f(fin.x, Math.abs(fin.y));   }

Also, keep in mind that I do have the ball's velocity vector stored, so if I need that I can easily get it.
Roquen
 « Reply #6 - Posted 2012-05-30 10:42:54 »

theagentd

« JGO Bitwise Duke »

Medals: 428
Projects: 2
Exp: 8 years

 « Reply #7 - Posted 2012-05-30 16:29:52 »

Line DOT Line = x*x + y*y = Line^2, right?

Myomyomyo.
CyanPrime
 « Reply #8 - Posted 2012-05-30 18:36:40 »

Okay, I got it. Thank you all for your help

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33 public Vector2f GetClosestPoint(Vector2f start, Vector2f end, Vector2f one, Vector2f two, Vector2f p, Vector2f m){      Vector2f start_to_point = new Vector2f(0,0);      Vector2f.sub(p,start,start_to_point);            //Storing vector A->P      //start_to_point.normalise();      Vector2f start_to_end = new Vector2f(0,0);      Vector2f.sub(end, start, start_to_end);            //Storing vector A->B      //start_to_end.normalise();      float t =  Vector2f.dot(start_to_point,  start_to_end) / start_to_end.lengthSquared();            // The normalized "distance" from a to                                            //   your closest point      System.out.println("t: " + t );            start_to_end.scale(t);      Vector2f fin = new Vector2f(0,0);      Vector2f.add(start, start_to_end, fin);            System.out.println("fin.x: " + fin.x + " fin.y: " + fin.y);            Vector2f one_to_two = new Vector2f(0,0);      Vector2f.sub(two,  one, one_to_two);            Vector2f normal = new Vector2f(0,0);      normal = new Vector2f(-one_to_two.y, one_to_two.x);      normal.normalise();      System.out.println("n.x: " + normal.x + " n.y: " + normal.y);            normal.scale(movementSpeed * 10);            Vector2f.add(fin, normal, fin);                  return new Vector2f(fin.x, Math.abs(fin.y));   }
CyanPrime
 « Reply #9 - Posted 2012-05-30 20:00:55 »

Looks like I spoke too soon. whenever a line isn't in the center it messes up, and I for the life of me can't figure out how to fix it. Code is in my post above, can anyone tell me why it's acting so strange?
CyanPrime
 « Reply #10 - Posted 2012-05-30 23:25:15 »

Okay, it now works no matter where you are:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41 public Vector2f GetClosestPoint(Vector2f start, Vector2f end, Vector2f one, Vector2f two, Vector2f p, Vector2f m){         Vector2f start_to_point = new Vector2f(0,0);         Vector2f.sub(p,one,start_to_point);            //Storing vector A->P         //start_to_point.normalise();         Vector2f start_to_end = new Vector2f(0,0);         Vector2f.sub(two, one, start_to_end);            //Storing vector A->B         //start_to_end.normalise();         float t =  Vector2f.dot(start_to_point,  start_to_end) / start_to_end.lengthSquared();            // The normalized "distance" from a to                                               //   your closest point         System.out.println("t: " + t );                  start_to_end.scale(t);         Vector2f fin = new Vector2f(0,0);         Vector2f.add(one, start_to_end, fin);                  System.out.println("fin.x: " + fin.x + " fin.y: " + fin.y);                  Vector2f one_to_two = new Vector2f(0,0);         Vector2f.sub(two,  one, one_to_two);         one_to_two.scale(0.5f);         Vector2f normal = new Vector2f(0,0);         normal = new Vector2f(-one_to_two.y, one_to_two.x);         if(p.x < 0 && p.y < 0){            if(Math.abs(p.x) < Math.abs(two.x)) normal = new Vector2f(one_to_two.y, -one_to_two.x);         }                  if(p.x > 0 && p.y > 0){            if(p.x < one.x && p.x < two.x) normal = new Vector2f(one_to_two.y, -one_to_two.x);         }                 normal.normalise();         System.out.println("n.x: " + normal.x + " n.y: " + normal.y);                  normal.scale(movementSpeed * 30);                  Vector2f.add(fin, normal, fin);                  if(p.y > 0.0f) return new Vector2f(fin.x, Math.abs(fin.y));         return new Vector2f(fin.x, -Math.abs(fin.y));      }

Thank you all for your help. :>
Axeman

Senior Devvie

Medals: 7

 « Reply #11 - Posted 2012-05-31 11:57:40 »

Line DOT Line = x*x + y*y = Line^2, right?

That´s right of course! Just got me a bit confused.
CyanPrime
 « Reply #12 - Posted 2012-06-17 19:43:40 »

Nevermind, I fixed it.
CyanPrime
 « Reply #13 - Posted 2012-06-19 03:49:35 »

Alright, so I got this code that is suppose to find me the nearest point on a quad, but it seems to not calculate the quad properly. It seems to think it's tilted when it's not. or rather it's calculating a line between the quads min.x,min.y and max.x, max.y points, but not the min.x, max.y or max.x, min.y points.

My question is how do I make this code calculate the quad as a whole?

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 public Vector3f GetClosestPoint(Vector3f start, Vector3f end, Vector3f point){          Vector3f fin = new Vector3f(0,0,0);          Vector3f start_to_point = new Vector3f(0,0,0);          Vector3f.sub(point,start,start_to_point);            //Storing vector A->P          Vector3f start_to_end = new Vector3f(0,0,0);          Vector3f.sub(end, start, start_to_end);            //Storing vector A->B          float t =  Vector3f.dot(start_to_point,  start_to_end) / start_to_end.lengthSquared();            // The normalized "distance" from a to          start_to_end.scale(t);           Vector3f.add(start, start_to_end, fin);          return new Vector3f(fin);    }

Riven
« League of Dukes »

« JGO Overlord »

Medals: 953
Projects: 4
Exp: 16 years

 « Reply #14 - Posted 2012-06-19 11:32:44 »

There is no such thing as 'closest point to quad' , just like there is no direct calculation for 'closest point to car'.

A quad is comprised of two triangles, for which you can actually calculate the closest point on the triangle, to any other point.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
CyanPrime
 « Reply #15 - Posted 2012-06-21 19:25:35 »

The code I'm using in my collision detection code is this:

(note: Vector3f is part of the LWJGL library, so if you want the src to that you'll have to find it on their site, but it's self explaining, and error free.)

(Note2:Tri is a class composed of three of LWJGL's Vector3fs. v1, v2, and v3.)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26 public Vector getTrisTouching(Vector3f pos, float radius){          Vector tempVec = new Vector();          for(int i = 0; i < tris.size(); i++){             Tri t = tris.get(i);                 Vector3f one_to_point = new Vector3f(0,0,0);             Vector3f.sub(pos,t.v1,one_to_point);            //Storing vector A->P                         Vector3f one_to_two = new Vector3f(0,0,0);             Vector3f.sub(t.v2,t.v1, one_to_two);            //Storing vector A->B                          Vector3f one_to_three = new Vector3f(0,0,0);             Vector3f.sub(t.v3, t.v1, one_to_three);            //Storing vector A->C                          float q1 =  Vector3f.dot(one_to_point,  one_to_two) / one_to_two.lengthSquared();            // The normalized "distance" from a to             float q2 =  Vector3f.dot(one_to_point,  one_to_three) / one_to_three.lengthSquared();            // The normalized "distance" from a to                                                    if (q1 > 0 && q2 > 0 && q1 + q2 < 1){                tempVec.add(t);             }          }                       return tempVec;       }

My question is how do I correctly see if a point in space is touching one of my triangles?
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #16 - Posted 2012-06-21 19:46:59 »

The easiest way is to create vectors from each triangle vertex to your point (so the differences between the vertices and the point). Then you can use the dot product to get the angles between these vectors, sum all the angles, and if they're < 360 then it's inside the triangle. That's not very fast, but it should be fine for most situations.If I recall you actually need to use the cross product too to see if the points is on the correct side of a side. I think this is called the "same side" technique.

You can also use barycentric coordinates to do it, which involves extending each side of the triangle into its own plane, and seeing if the point lies in any of those planes (if it does it's outside). You should be able to Google that one, it's faster but more complicated to implement.

See my work:
OTC Software
CyanPrime
 « Reply #17 - Posted 2012-06-21 20:06:17 »

Like this, right?
 1  2  3  4  5  6  7  8  9  10  11  12 Vector3f one_to_point = new Vector3f(0,0,0);         Vector3f.sub(pos,t.v1,one_to_point);            //Storing vector A->P                  Vector3f two_to_point = new Vector3f(0,0,0);         Vector3f.sub(pos,t.v2,two_to_point);            //Storing vector B->P                  Vector3f three_to_point = new Vector3f(0,0,0);         Vector3f.sub(pos,t.v3,three_to_point);            //Storing vector C->P                  float angle = Vector3f.dot(one_to_point, two_to_point);         angle += Vector3f.dot(two_to_point, three_to_point);         angle += Vector3f.dot(three_to_point, one_to_point);

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #18 - Posted 2012-06-21 20:23:17 »

Yeah, but you also need to check the cross product to make sure it's on the right side. Like this (broken into two functions to preserve code).

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 public boolean isPointInTriangle( Vector3f p, Vector3f a, Vector3f b, Vector3f c ){    return ( pointsAreOnSameSide(p, a, b, c) && pointsAreOnSameSide(p, b, a, c) && pointsAreOnSameSide(p, c, a, b) );}public boolean pointsAreOnSameSide( Vector3f p1, Vector3f p2, Vector3f a, Vector3f b ){    Vector3f diffba = new Vector3f(0,0,0);    Vector3f.sub( b, a, diffba );    Vector3f diffp1a = new Vector3f(0,0,0);    Vector3f.sub( p1, a, diffp1a );    Vector3f diffp2a = new Vector3f(0,0,0);    Vector3f.sub( p2, a, diffp2a );    Vector3f cross1 = Vector3f.cross(diffba, diffp1a);    Vector3f cross2 = Vector3f.cross(diffba, diffp2a);    return ( Vector3f.dot( cross1, cross2 ) >= 0 );}

Holy god LWJGL vectors are lame? Why no myVec.sub(otherVec) etc. You really need to pass everything into static functions? Stupid. Why not put both in there?

See my work:
OTC Software
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #19 - Posted 2012-06-21 20:34:21 »

You can try the barycentric version too, which looks like it's what you were trying. I just took this code from the internets and made it LWJGL-ready, so I don't know if it works.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 public boolean isPointInTriangle( Vector3f p, Vector3f a, Vector3f b, Vector3f c ){    Vector3f v0 = new Vector3f(0,0,0);    Vector3f.sub( c, a, v0 );    Vector3f v1 = new Vector3f(0,0,0);    Vector3f.sub( b, a, v1);    Vector3f v2 = new Vector3f(0,0,0);    Vector3f.sub( p, a, v2 );    float dot00 = Vector3f.dot(v0, v0);    float dot01 = Vector3f.dot(v0, v1);    float dot02 = Vector3f.dot(v0, v2);    float dot11 = Vector3f.dot(v1, v1);    float dot12 = Vector3f.dot(v1, v2);    float inverse = 1.0f / (dot00 * dot11 - dot01 * dot01);    float u = (dot11 * dot02 - dot01 * dot12) * inverse;    float v = (dot00 * dot12 - dot01 * dot02) * inverse;    return (u >= 0) && (v >= 0) && (u + v < 1);}

Looks like in your original version you missed a step of dot producting (note I'm not whiz at this way of doing it either). Honestly looking at the two versions side-by-side the speed difference looks negligible. So I'd say do it the way you want.

See my work:
OTC Software
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #20 - Posted 2012-06-21 21:31:11 »

I made this for the lolz. Both methods appear to work just fine. Note the mouse position is slightly off because positions in the frame and panel don't match exactly due to the menu at the top. Just look at where the cyan dot is.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164 import java.awt.*;import javax.swing.*;import java.awt.event.*;public class TrianglePointContains extends JFrame implements MouseMotionListener{   private static Vector3f mouseLocation;   private static Vector3f vertexA;   private static Vector3f vertexB;   private static Vector3f vertexC;   private static TrianglePanel sameSidePanel;   private static TrianglePanel barycentricPanel;      public TrianglePointContains()   {      super("Triangle Point Contains");            //totally arbitrary points      mouseLocation = new Vector3f(0,0,0);      vertexA = new Vector3f(200, 50, 0);      vertexB = new Vector3f(50, 350, 0);      vertexC = new Vector3f(350, 350, 0);            setLayout(new GridLayout(1,2));      sameSidePanel = new TrianglePanel( false );      barycentricPanel = new TrianglePanel( true );      sameSidePanel.setSize(400,400);      barycentricPanel.setSize(400,400);      add(sameSidePanel);      add(barycentricPanel);            addMouseMotionListener( this );            setSize(800,400);      setVisible(true);   }      public static void main(String[] args)   {      new TrianglePointContains();   }      public static boolean isPointInTriangle( boolean barycentric, Vector3f p, Vector3f a, Vector3f b, Vector3f c )   {      if ( barycentric )      {         Vector3f v0 = c.subtract(a);         Vector3f v1 = b.subtract(a);         Vector3f v2 = p.subtract(a);         float dot00 = v0.dot(v0);         float dot01 = v0.dot(v1);         float dot02 = v0.dot(v2);         float dot11 = v1.dot(v1);         float dot12 = v1.dot(v2);         float inverse = 1.0f / (dot00 * dot11 - dot01 * dot01);         float u = (dot11 * dot02 - dot01 * dot12) * inverse;         float v = (dot00 * dot12 - dot01 * dot02) * inverse;         return (u >= 0) && (v >= 0) && (u + v < 1);      }             return ( pointsAreOnSameSide(p, a, b, c) && pointsAreOnSameSide(p, b, a, c) && pointsAreOnSameSide(p, c, a, b) );   }   private static boolean pointsAreOnSameSide( Vector3f p1, Vector3f p2, Vector3f a, Vector3f b )   {       Vector3f diffba = b.subtract(a);       Vector3f diffp1a = p1.subtract(a);       Vector3f diffp2a = p2.subtract(a);       Vector3f cross1 = diffba.cross(diffp1a);       Vector3f cross2 = diffba.cross(diffp2a);       return ( cross1.dot( cross2 ) >= 0 );   }      private static Vector3f getAdjustedMousePosition( boolean barycentric )   {      if ( mouseLocation.x >= sameSidePanel.getWidth() )      {         return new Vector3f( mouseLocation.x - sameSidePanel.getWidth(), mouseLocation.y, mouseLocation.z );      }      return mouseLocation;   }      public void mouseMoved( MouseEvent e )   {      mouseLocation.x = e.getX();      mouseLocation.y = e.getY();      repaint();   }      public void mouseDragged( MouseEvent e )   {      mouseLocation.x = e.getX();      mouseLocation.y = e.getY();      repaint();   }      private class TrianglePanel extends JPanel   {      private boolean isBarycentric;            public TrianglePanel( boolean barycentric )      {         isBarycentric = barycentric;      }            public void paintComponent( Graphics g )      {         super.paintComponent(g);                  Vector3f mousePos = getAdjustedMousePosition( isBarycentric );         boolean containsPoint = isPointInTriangle( isBarycentric, mousePos, vertexA, vertexB, vertexC );         Polygon p = new Polygon(new int[]{ (int)vertexA.x, (int)vertexB.x, (int)vertexC.x }, new int[] { (int)vertexA.y, (int)vertexB.y, (int)vertexC.y }, 3);         g.setColor( containsPoint ? Color.RED : Color.GREEN );         ((Graphics2D)g).fill( p );         g.setColor(Color.BLACK);         ((Graphics2D)g).draw( p );                  g.setColor(Color.CYAN);         g.fillOval( (int)mousePos.x-2, (int)mousePos.y-2, 4, 4 );      }   }      private static class Vector3f   {      public float x, y, z;            public Vector3f( float setX, float setY, float setZ )      {         x = setX;         y = setY;         z = setZ;      }            public float dot( Vector3f other )      {         return x * other.x + y * other.y + z * other.z;      }            public Vector3f cross( Vector3f other )      {         return new Vector3f         (            (y * other.z) - (other.y * z),            (z * other.x) - (other.z * x),            (x * other.y) - (other.x * y)         );      }            public Vector3f subtract( Vector3f other )      {         return new Vector3f         (            x - other.x,            y - other.y,            z - other.z         );      }   }}

See my work:
OTC Software
CyanPrime
 « Reply #21 - Posted 2012-06-22 01:20:08 »

Alright so this all started when I asked a question on here, which got answered, but I dunno if the answer is correct yet (though I'm assuming it is, in all honesty) So I haven't said thank you to Eli yet (You will get it, or at least more replies once I figure out if it works or not).

anyway, I digress. I asked a question on StackOverflow about Getting the distance  of a point between two lines and got a answer that said to: "find the straight line that links A and D and get its equation in the form: y = m_1 * x + c_1" So I got no idea how to do this, and the person who gave the answer has stopped replying, so I come to you guys. How do I do that?
ra4king

JGO Kernel

Medals: 374
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #22 - Posted 2012-06-22 02:28:29 »

Well if you took basic geometry...it's the usual y=mx+b formula. Just plug it in

m = slope
b = offset

Geemili

Senior Devvie

Medals: 9
Projects: 1
Exp: 2 years

No Games Finished

 « Reply #23 - Posted 2012-06-22 02:54:08 »

Isn't the distance formula this?
 1 distance = sqrt( (x2-x1)^2 + (y2-y1)^2)

Because I'm pretty sure that y=mx+b is a linear equation. (not sure on exact terminology)
CyanPrime
 « Reply #24 - Posted 2012-06-22 02:56:06 »

Okay, I think I got that down, thanks ra4king.
 1  2  3  4  5  6  7  8  9  10  11 public float GetDist(Vector3f one, Vector3f two, Vector3f three, Vector3f p){                     Vector3f one_to_point = new Vector3f(0,0,0);         Vector3f.sub(p,one,one_to_point);            //Storing vector A->P                  Vector3f oneToPNorm = new Vector3f(one_to_point);         oneToPNorm.normalise();                 float m = oneToPNorm.y/oneToPNorm.x;         float b = oneToPNorm.x/oneToPNorm.y;

Okay,so now the guy just kinda says I can "solve this pair of simultaneous equations to get the point (x,y) that lies at the green circle on your diagram" But doesn't say how I can do that...
JESTERRRRRR
 « Reply #25 - Posted 2012-06-22 03:31:30 »

What Geemili said is right, go with it.
loom_weaver

JGO Coder

Medals: 17

 « Reply #26 - Posted 2012-06-22 03:47:09 »

If you know two distinct points that are on a line you can calculate the equation of the line.  Here's a site that explains how:

http://www.webmath.com/equline1.html

Once you have the equation of your two lines then you can calculate where they intersect and from there you can solve your original problem.
CyanPrime
 « Reply #27 - Posted 2012-06-22 13:48:11 »

Alright, I'm trying to make a player walk on a slope. It's not working, well it is, but there seems to be a shitton of jittering when you walk on the beginning of the slope.

And this is getDist

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 public float getDist( Vector3f p, Vector3f a, Vector3f b, Vector3f c )   {       Vector3f v0 = new Vector3f(0,0,0);       Vector3f.sub( c, a, v0 );       Vector3f v1 = new Vector3f(0,0,0);       Vector3f.sub( b, a, v1);       Vector3f v2 = new Vector3f(0,0,0);       Vector3f.sub( p, a, v2 );       float dot00 = Vector3f.dot(v0, v0);       float dot01 = Vector3f.dot(v0, v1);       float dot02 = Vector3f.dot(v0, v2);       float dot11 = Vector3f.dot(v1, v1);       float dot12 = Vector3f.dot(v1, v2);       float inverse = 1.0f / (dot00 * dot11 - dot01 * dot01);       float u = (dot11 * dot02 - dot01 * dot12) * inverse;       float v = (dot00 * dot12 - dot01 * dot02) * inverse;       if((u >= 0) && (v >= 0) && (u + v <= 1)) return (float) (Math.sin(u) * Math.cos(v));       else return 0;   }

This is my code for it. Are there any other parts I'll need to post?
CyanPrime
 « Reply #28 - Posted 2012-06-22 13:55:34 »

Oh, and here's the download so you can see how it jitters.
https://dl.dropbox.com/u/28109593/jitter-test.zip
CyanPrime
 « Reply #29 - Posted 2012-06-22 18:51:48 »

So yeah, this is what I'm trying to do:

Alright, so I'm trying to achieve whats in this image: https://dl.dropbox.com/u/28109593/dotsexample.png

I believe this would be a barycentric coord system, but where the X always equals 1? Basically I need it to only increase/decrease when I move towards/away from the highest point in my triangle. This is the code I got for it so far (Note I'm using the LWJGL library in java).

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 public float getDist( Vector3f p, Vector3f a, Vector3f b, Vector3f c )   {       Vector3f v0 = new Vector3f(0,0,0);       Vector3f.sub( c, a, v0 );       Vector3f v1 = new Vector3f(0,0,0);       Vector3f.sub( b, a, v1);       Vector3f v2 = new Vector3f(0,0,0);       Vector3f.sub( p, a, v2 );       float dot00 = Vector3f.dot(v0, v0);       float dot01 = Vector3f.dot(v0, v1);       float dot02 = Vector3f.dot(v0, v2);       float dot11 = Vector3f.dot(v1, v1);       float dot12 = Vector3f.dot(v1, v2);       float inverse = 1.0f / (dot00 * dot11 - dot01 * dot01);       float u = (dot11 * dot02 - dot01 * dot12) * inverse;       float v = (dot00 * dot12 - dot01 * dot02) * inverse;       if((u >= 0) && (v >= 0) && (u + v <= 1)) return (float) (Math.sin(u) * Math.cos(v));       else return 0;   }

I guess what I'm asking is: Is there a way to get the distance that a point inside a triangle has travailed from the lowest point the triangle has in space where 1 would be the highest point on the triangle (mosty far away from the lowist) without taking it's deviating vector into account? I.E. notice that the two red dots on the image have the same coords even though they have different dists from the top's x?
Pages: [1] 2
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (27 views) 2015-04-16 10:48:47 Duke0200 (42 views) 2015-04-16 01:59:01 Fairy Tailz (32 views) 2015-04-14 20:13:12 Riven (33 views) 2015-04-12 21:36:37 bus hotdog (49 views) 2015-04-10 02:39:32 CopyableCougar4 (51 views) 2015-04-10 00:51:04 BurntPizza (51 views) 2015-04-06 22:06:58 ags1 (53 views) 2015-04-02 10:58:48 Riven (52 views) 2015-04-01 18:27:05 ags1 (69 views) 2015-03-31 10:55:12
 theagentd 26x BurntPizza 18x wessles 15x 65K 11x kingroka123 11x Rayvolution 11x alwex 10x KevinWorkman 9x kevglass 8x phu004 8x Hanksha 7x SHC 7x ra4king 7x Olo 7x Roquen 7x chrislo27 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org