Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 1146 times)
0 Members and 1 Guest are viewing this topic.
Offline Gorgorath

Junior Devvie




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 Spiffy Duke »


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 Devvie




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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!