Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Getting distance of a point in a 2d triangle without calculating perpendicular..  (Read 5727 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2012-05-29 23: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;
   }

Offline Axeman

Senior Member


Medals: 7



« Reply #1 - Posted 2012-05-30 02: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..?
Offline CyanPrime
« Reply #2 - Posted 2012-05-30 07: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
  }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline actual

JGO Coder


Medals: 23



« Reply #3 - Posted 2012-05-30 07: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.
Offline CyanPrime
« Reply #4 - Posted 2012-05-30 07: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.
Offline CyanPrime
« Reply #5 - Posted 2012-05-30 09: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.
Offline Roquen
« Reply #6 - Posted 2012-05-30 12:42:54 »

At the bottom of the code snippet in this THREAD are formulations for reflection...read the comments.
Offline theagentd
« Reply #7 - Posted 2012-05-30 18:29:52 »

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

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

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

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));
   }
Offline CyanPrime
« Reply #9 - Posted 2012-05-30 22: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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #10 - Posted 2012-05-31 01: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. :>
Offline Axeman

Senior Member


Medals: 7



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

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

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

Nevermind, I fixed it.
Offline CyanPrime
« Reply #13 - Posted 2012-06-19 05: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);
    }


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2012-06-19 13: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
Offline CyanPrime
« Reply #15 - Posted 2012-06-21 21: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<Tri> getTrisTouching(Vector3f pos, float radius){
          Vector<Tri> tempVec = new Vector<Tri>();
          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?
Online Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #16 - Posted 2012-06-21 21: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
Offline CyanPrime
« Reply #17 - Posted 2012-06-21 22: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);

        
Online Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #18 - Posted 2012-06-21 22: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
Online Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #19 - Posted 2012-06-21 22: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. Smiley

See my work:
OTC Software
Online Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #20 - Posted 2012-06-21 23: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
Offline CyanPrime
« Reply #21 - Posted 2012-06-22 03: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?
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


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

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

m = slope
b = offset

Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #23 - Posted 2012-06-22 04: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)
Offline CyanPrime
« Reply #24 - Posted 2012-06-22 04: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...
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #25 - Posted 2012-06-22 05:31:30 »

What Geemili said is right, go with it.
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #26 - Posted 2012-06-22 05: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.
Offline CyanPrime
« Reply #27 - Posted 2012-06-22 15: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.

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  
onGround = false;
         Vector3f prevPos = new Vector3f(pos);
         
         if(!jumping && !flying && !hovering) pos.y += fallSpeed + height;
         
         boolean b = false;
         Vector<Tri> tris = model.meshes.get(0).getTrisTouching(pos);
         float minY;
         if(tris.size() > 0) minY = Float.POSITIVE_INFINITY;
         else minY = 0;
         
         for(int i = 0; i < tris.size(); i++){
            Tri q = tris.get(i);
            Normal normal = q.cn;
            if(normal.pos.y <= -0.3f){//asdf
              float dist = Vector3f.dot(pos, normal.pos) - Vector3f.dot(q.center, normal.pos);
               radius = 4;
               
               if(dist < 1){
                   Vector3f[] quad = model.meshes.get(0).getTriAsArray(q.index - 1);
   
                   if(prevPos.y + radius <= q.max.y){
                      //closestPoint = GetClosestPoint(quad[0], quad[2], pos, normal.pos, fallSpeed + height, delta, false);
                  //   float quadDist = GetDist(quad[0],quad[2], quad[1], pos);
                     //float quadDist = GetDist2d(new Vector2f(quad[0].x, quad[0].z),new Vector2f(quad[1].x, quad[1].z), new Vector2f(quad[2].x, quad[2].z), new Vector2f(pos.x, pos.z));
                  //   System.out.println("quadDist:\t" + quadDist);
                     
                      if(q.min.y != q.max.y){

                         float quadDist = getDist(pos, q.v1, q.v2, q.v3);
                         float sizeChunk = (q.max.y - q.min.y);
                         float curY = q.min.y + (sizeChunk*quadDist);
                         
                         System.out.println("quadDist: " + quadDist);
                         
                         if(curY < minY) minY = curY;
                      }
                     
                      else
                         minY = q.min.y;
                     
                      b = true;
                     }
               }
            }
         }

         if(b){
            onGround = true;
            hovering = false;
            flying = false;
            pos.y = minY - height;
                         
         }
         
         else{
            pos.y -= height;
         }



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?
Offline CyanPrime
« Reply #28 - Posted 2012-06-22 15:55:34 »

Oh, and here's the download so you can see how it jitters.
https://dl.dropbox.com/u/28109593/jitter-test.zip
Offline CyanPrime
« Reply #29 - Posted 2012-06-22 20: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (23 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!