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]
  ignore  |  Print  
  Help finding the Y axis angle of a plane?  (Read 2454 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2012-05-28 01:42:05 »

Alright, so I need to know the y axis angle of a plane made by 4 points in 3d space. basically I want code to where if it's _ (flat) I can walk on it and if its | (a wall) I run into it. So could anyone explain to me a good way to figure this out?
Offline theagentd
« Reply #1 - Posted 2012-05-28 01:51:09 »

Calculate the normal using 3 of the vertices (a triangle) and check the value of y?

Myomyomyo.
Offline CyanPrime
« Reply #2 - Posted 2012-05-28 07:45:47 »

Okay, this is what I got so far. Assume quadToCheck is 4 Vector3f's with random values.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public float getNormal(Vector3f[] quadToCheck){
      Vector3f ab = new Vector3f(0,0,0);
      Vector3f.sub(quadToCheck[1], quadToCheck[0], ab);
     
      Vector3f ac = new Vector3f(0,0,0);
      Vector3f.sub(quadToCheck[2], quadToCheck[0], ac);
     
      Vector3f crossed = new Vector3f(0,0,0);
      Vector3f.cross(ac, ab, crossed);
      Vector3f backup = new Vector3f(crossed);
      try{
         crossed.normalise();
      }
     
      catch(Exception e){
         //e.printStackTrace();
        crossed = backup;
      }
      return crossed.y;
   }


Am I doing this right? Cause I'm not seeing good results, and it tends to hit that catch like every time, or every other time :<
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-05-28 11:29:24 »

This is some GLSL code I have to calculate the normal of a triangle in a geometry shader:
1  
2  
3  
4  
5  
6  
vec3 calculateNormal(){
    vec3 u = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
    vec3 v = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
   
    return normalize(cross(u, v));
}


Shouldn't it be Vector3f.cross(ab, ac, crossed)? Mine works as it should.

EDIT: I love how clean GLSL code looks for this kind of math. =>

Myomyomyo.
Offline Roquen
« Reply #4 - Posted 2012-05-28 11:58:38 »

There's no need to normalize unless you really want the normal.  The original question only needs the direction.
Offline theagentd
« Reply #5 - Posted 2012-05-28 15:08:29 »

There's no need to normalize unless you really want the normal.  The original question only needs the direction.
But to calculate the angle to y+ you need to do a dot product with a vector pointing up --> you need normalize it and just look at y. Maybe that's not the best way of doing it...

Myomyomyo.
Offline Roquen
« Reply #6 - Posted 2012-05-28 15:17:26 »

Really this just isn't the way to handle the problem.  If polys are always in XZ (floor/ceiling) and X^Z=0 (walls), then just have three lists floors, ceilings and walls and be done with it...which I assume is the case since only the Y component is being returned.  But if you don't need distance from test point, then no normalization is needed...the sign is the answer.
Offline Danny02
« Reply #7 - Posted 2012-05-28 15:35:24 »

ofcourse you can always check if some plane/triangle/quad is walkable with the normal(angle between the normal and the 90° flat plane). Another possibility is to generate a navigation mesh(something in the direction of Roquens idea).

Just generate a list of areas/triangles ... which are wakable. By hand or an algorithm, there are also nice opensource tools for that see recast
Offline theagentd
« Reply #8 - Posted 2012-05-28 16:01:39 »

I was assuming that OP wanted something similar to most shooters/RPGs, where you can walk on everything that isn't too steep. My idea was to look at the y value of the normal and check if it was above a certain threshold...

Myomyomyo.
Offline Danny02
« Reply #9 - Posted 2012-05-28 16:10:44 »

I was assuming that OP wanted something similar to most shooters/RPGs, where you can walk on everything that isn't too steep. My idea was to look at the y value of the normal and check if it was above a certain threshold...

sure that works, the y value of a normalized plane normal should be the cosinus angle between the plane and the ground, right?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #10 - Posted 2012-05-28 18:38:32 »

Doesn't work.  For arbitrary surfaces you need the full direction and not a single component.  Consider rotating the surface about 'y'.  You can find the slope but not the maximum direction of change.
Offline Icecore

Senior Member


Medals: 5



« Reply #11 - Posted 2012-05-28 19:25:19 »

Check this out
Where Y and X 2 axis distance
Let sta you have 1.3 and 2.4
X = 2 – 1
Y = 4 – 3
Play with dif axis to find what you need Wink

1  
2  
3  
4  
5  
static public float Angle360(float y, float x){
      float ret = (float) (Math.atan2(y, x) * 180 / Math.PI);
      if(ret < 0)ret += 360;
      return ret;
}

try google Atan2
http://polygeek.com/1819_flex_exploring-math-atan2
Offline Danny02
« Reply #12 - Posted 2012-05-28 20:02:38 »

Doesn't work.  For arbitrary surfaces you need the full direction and not a single component.  Consider rotating the surface about 'y'.  You can find the slope but not the maximum direction of change.

sry, I don't get what you are saying. A first thought how to handle player movement would be to check if the surface the player is standing on has a slope below a given threshold. When the slope is high the player starts sliding according to the slope.
Offline Roquen
« Reply #13 - Posted 2012-05-28 20:20:51 »

In what direction does the player start to slide?
Offline Danny02
« Reply #14 - Posted 2012-05-28 20:36:41 »

to get direction where to slide you need of course the normal, but to check if you slide is another calculation(the one above)

to get the direction:
slideDir = normal x (normal x up)

slope:
cos(a) = up * normalize(normal)
cos(a) = (normalize(normal)).y // because up = (0, 1, 0)

Offline Roquen
« Reply #15 - Posted 2012-05-28 21:28:04 »

That was a rhetorical question...you require all three components to know the direction.  cross gives direction of plane, project back into plane is direction of max slope..normalize if needed.
Offline theagentd
« Reply #16 - Posted 2012-05-29 14:37:07 »

That was a rhetorical question...you require all three components to know the direction.  cross gives direction of plane, project back into plane is direction of max slope..normalize if needed.
I don't understand the math behind that, so I'll just tell you how I would do it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
vec3 t0, t1, t2; //The triangle (three sides of a quad)

Vec3 u = t1 - t0;
Vec3 v = t2 - t0;

Vec3 n = cross(u, v); //Unnormalized normal of the plane

float cosSlopeAngle = n.y / sqrt(n.x*n.x + n.y*n.y + n.z*n.z);

if(cosSlopeAngle > cos(maximumAngle){
    vec2 slidingDirection = vec2(n.x, n.z);
    slidingDirection.normalize();
    //Use sliding direction to modify the objects velocity...
}

Excuse my psuedo-GLSL...

Myomyomyo.
Pages: [1]
  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 (25 views)
2014-07-23 16:59:36

Riven (25 views)
2014-07-23 12:16:32

Riven (19 views)
2014-07-23 12:07:15

Riven (22 views)
2014-07-23 11:56:16

ctomni231 (51 views)
2014-07-17 21:55:21

Zero Volt (46 views)
2014-07-17 14:47:54

danieldean (37 views)
2014-07-17 14:41:23

MustardPeter (40 views)
2014-07-16 14:30:00

Cero (56 views)
2014-07-15 15:42:17

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

Java and Game Development Tutorials
by SwordsMiner
2014-06-13 15:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-13 15:47:22

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

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

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

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

Experimental Toys
by Roquen
2014-04-28 04: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!