Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Simple question  (Read 1096 times)
0 Members and 1 Guest are viewing this topic.
Offline Gorgorath

Junior Member




Java games rock!


« Posted 2006-11-23 09:35:54 »

I have 4 points definiing a simple quad(2D), but i need to order them in a counter clockwise format in order to render them properly

Paul
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #1 - Posted 2006-11-23 10:15:40 »

so, what's the question?

Offline CommanderKeith
« Reply #2 - Posted 2006-11-23 12:14:53 »

I have 4 points definiing a simple quad(2D), but i need to order them in a counter clockwise format in order to render them properly

Paul
I made the following code to make sure a Polygon has its array of points listed in an anti-clockwise direction.  Basically it uses the fact that any polygon of n-points should have an inner angle sum of (n - 2)*Math.PI.

It uses three methods.  I made this when i was in yr 10 school so it really isn't fast, but it works  Smiley.  If you want to see the full code see the net.phys2d.raw.shapes.KPolygon class in the source here: http://www.freewebs.com/commanderkeith/PolygonPhysicsSource.zip
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  
public Point2D.Float[] points;

public void ensureAntiClockwisePath()
 {
  // to establish whether or not the polygon path is anticlockwise or not.
 // proper angle sum
 float properAngleSum = (float)((points.length - 2)*Math.PI);

  // angle sum assuming the points are listed in anti-clockwise direction
 float angleSum = 0;
  Point2D.Float currentPoint;

  for (int i = 0; i < points.length; i++)
  {
   angleSum += findAngle(i, points);
  }

  // reverses the order of the polygon so that the points go anti-clockwise
 // if they weren't doing that already.

  // System.out.println("properAngleSum = " + properAngleSum);
 // System.out.println("angleSum = " + angleSum);

  if (!(angleSum > (properAngleSum - 0.001f) && angleSum < (properAngleSum + 0.001f)))
  {
   // System.out.println("reversing");
  Point2D.Float[] reversed = new Point2D.Float[points.length];
   int countDown = points.length-1;
   for (int i = 0; i < points.length; i++)
   {
    reversed[countDown] = points[i];
   countDown--;
   }
   points = reversed;
  }
 }

 public float findAngle(int i, Point2D.Float[] decreasingPoints)
 {
  // this only works where the polygon has an anti-clockwise path.
 // returns angle in radians, not degrees.

  float currentAngle;
  float nextPointAngle;
  float prevPointAngle;

  prevPointAngle = findAngle(i, (i - 1 < 0 ? decreasingPoints.length-1 : i-1), decreasingPoints);
  nextPointAngle = findAngle(i, (i + 1 > decreasingPoints.length-1 ? 0 : i+1), decreasingPoints);

  currentAngle = prevPointAngle - nextPointAngle;
  if (currentAngle < 0)
  {
   currentAngle += (float)(2*Math.PI);
  }
  return currentAngle;
 }

public float findAngle(int start, int dest, Point2D.Float[] decreasingPoints)
 {
  // returns angle that dest is relative to start, measured anti-clockwise from the x-axis
 // returns angle in radians, not degrees.
 float angle;
  float x;
  float y;

  x = decreasingPoints[dest].x - decreasingPoints[start].x;
  y = decreasingPoints[dest].y - decreasingPoints[start].y;

  if (x == 0.0f)
  {
   return (y > 0 ? (float)(Math.PI/2) : (float)(Math.PI*3/2));
  }

  angle = (float)(Math.atan(y/x) + (x < 0 ? Math.PI : 0));

  if (angle < 0)
  {
   angle += (float)2*Math.PI;
  }
  return angle;
 }


Keith

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gorgorath

Junior Member




Java games rock!


« Reply #3 - Posted 2006-11-23 16:39:46 »

pretty funny jar:) been playing with it for a half hour
Offline CommanderKeith
« Reply #4 - Posted 2006-11-23 23:20:44 »

glad u like it!  Cheesy  Its part of the 2D Physics System that Kev knocked up... http://www.java-gaming.org/forums/index.php?topic=14461.90

Its under development, but slowly...

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.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (19 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (45 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (100 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!