Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Separating Axis Theorem + OpenGL rotation  (Read 6739 times) 0 Members and 1 Guest are viewing this topic.
Kronos

Junior Devvie

Medals: 1

 « Posted 2013-09-15 12:22:09 »

Hello everybody

I finally got SAT working but only if I leave out the roation part from OpenGL. My Player object rotates around his own axis
 1  2  3 `         GL11.glTranslatef(player.x-player.width/2, player.y-player.height/2, 0f);         GL11.glRotatef(player.angle, 0f, 0f, 1f);         GL11.glTranslatef(-player.x-player.width/2, -player.y-player.width/2, 0f);`

Works great so far. But the collision detection does not recognize that the polygon (trapez) has rotated.
How do I connect my SAT collision detection with the angle my polygon has rotated?

This is how I go forward:

 1  2  3 `            player.y += speed * delta * Math.sin(Math.toRadians(player.angle) - Math.PI/2);player.x += speed * delta * Math.cos(Math.toRadians(player.angle) - Math.PI/2);`

Opiop
 « Reply #1 - Posted 2013-09-15 13:30:24 »

It's frustrating when you don't include the code in question.  Please, I have no idea what your code looks like, so post it.
Kronos

Junior Devvie

Medals: 1

 « Reply #2 - Posted 2013-09-15 13:41:32 »

Oh I am sorry

This methods are in Polygon

 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 `   public boolean collides(Polygon polygon) {      Dot[] axes = new Dot[polygon.dotCount];            for(int i=0; i < polygon.dotCount; i++) {         Dot p1 = polygon.getDots()[i];         Dot p2 = polygon.getDots()[i + 1 == polygon.dotCount ? 0 : i +1];                  Dot edge = p1.subtract(p2);         Dot normal = edge.perp();         axes[i] = normal;      }            for(int i=0; i < axes.length; i++) {         Projection p1 = this.project(axes[i]);         Projection p2 = polygon.project(axes[i]);                  if(!p1.overlap(p2)) {            return false;         }      }            return true;   }   public Projection project(Dot axis) {      float min = axis.dotProd(getDots()[0]);      float max = min;            for(int i=1; i < getDots().length; i++) {         float p = axis.dotProd(getDots()[i]);                  if(p < min) {            min = p;         } else if(p > max) {            max = p;         }      }            return new Projection(min, max);   }`

where Dot is a Vector, I just called it Dot

Projection.class
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `public class Projection {      protected float min;   protected float max;      public Projection(float min, float max) {      this.min = min;      this.max = max;   }      public boolean overlap(Projection proj) {      boolean temp = true;      if(min > proj.max || max < proj.min)         temp = false;            return temp;   }}`

I hope that was all of the relevant code. Sorry again, I thought that was a common problem.
relminator
 « Reply #3 - Posted 2013-09-15 14:34:03 »

Your sat code looks good.  You maybe checking unrotated polygons though.

Btw, of it's just obbs you can ditch the check for 2 axes to make it faster still.
Kronos

Junior Devvie

Medals: 1

 « Reply #4 - Posted 2013-09-15 16:30:05 »

Hey thanks for your answer. Only the Player's polygon is rotated, the other polygons are "normal". I did not understand your last sentence. What do you mean exactly?
Kronos

Junior Devvie

Medals: 1

 « Reply #5 - Posted 2013-09-16 09:36:00 »

I did not found out something new, but I created a screenshot to visualize my problem. I understand that the collision detection is working, but only internally. What I am rendering is not true!?

Here you are:

relminator
 « Reply #6 - Posted 2013-09-16 11:07:58 »

Hey thanks for your answer. Only the Player's polygon is rotated, the other polygons are "normal". I did not understand your last sentence. What do you mean exactly?

What i meant was because you are using an oriented box, you can skip checks for 2 axes since 2 of the edges are parallel to each other.
Kronos

Junior Devvie

Medals: 1

 « Reply #7 - Posted 2013-09-16 17:20:15 »

Okay thanks, but I think I will keep it as it is, I might want to use more than 4 axes later.

Anyone else can help me with the real problem?
Kronos

Junior Devvie

Medals: 1

 « Reply #8 - Posted 2013-09-17 13:19:23 »

Ok I came to the point that rotating the polygons only with openGL is not the right way, because it is just an illusion. The actual verctors aren't rotated at all.

So here is my code (little different now) with the user input and rotating stuff. What do I have to do? Really I do not come further. I need your help. Getting desperate

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `hip = forwardSpeed * delta;               float radAng = (float) Math.toRadians(player.angle);         float cosAngle = (float) Math.cos(radAng);         float sinAngle = (float) Math.sin(radAng);                           if(Keyboard.isKeyDown(Keyboard.KEY_W)) {            player.x += hip * sinAngle;            player.y += hip * cosAngle;         }                  if(Keyboard.isKeyDown(Keyboard.KEY_S)) {            hip = backwardSpeed * delta;                        player.y -= hip * Math.sin(Math.toRadians(player.angle));            player.x -= hip * Math.cos(Math.toRadians(player.angle));         }`
relminator
 « Reply #9 - Posted 2013-09-17 14:28:20 »

You should have another polygon vertex array that is rotated and use that to check for collisions.

you can use:

sin - cos
cos + sin

if I remember correctly.

Or just rotate from origin like what you're doing, translate and check for collisions.  just clear the rotations and translations every frame.  slow but should work.
Kronos

Junior Devvie

Medals: 1

 « Reply #10 - Posted 2013-09-17 17:37:18 »

Hey thanks for the answer, could please show me with code or an example? I tried some thing that didn't work or did the same
relminator
 « Reply #11 - Posted 2013-09-18 00:05:31 »

www.rel.phatcode.net/index.php?action=contents&item=ConvexWars-FB

edit:

I think I have a little demo in c++ here:

www.rel.phatcode.net/junk.php?id=121

/samples
Kronos

Junior Devvie

Medals: 1

 « Reply #12 - Posted 2013-09-18 17:39:19 »

Thanks dude, but the problem was something else my method for texture rendering used the normal x and y coords

I think I got the right rotation Part, but now it has to be translated that it rotates around its center. In openGl I used this

 1  2  3 `GL11.glTranslatef(player.x-player.width/2, player.y-player.height/2, 0f);GL11.glRotatef(player.angle, 0f, 0f, 1f);GL11.glTranslatef(-player.x-player.width/2, -player.y-player.width/2, 0f);`

what would be the equivalent in "plain" java code. I naivle tried this

 1  2  3  4  5  6  7 `before:      x -= width/2;      y -= height/2;after rotation:      x -= -width/2;      y -= -height/2;`

I feel a little bit dumb with this try Pleaseeeee there is only a little step to success and I can sleep again
Kronos

Junior Devvie

Medals: 1

 « Reply #13 - Posted 2013-09-18 20:06:13 »

this is going to be an one man show, anyway hope is there

This is the current rotation at an angle given by the user input (left right key)

this code is doing the rotation...

 1  2  3  4  5 `   tmpDots[0].set(x * cosAngle - y * sinAngle, x * sinAngle + y * cosAngle);      tmpDots[1].set(x * cosAngle - (y + height) * sinAngle, x * sinAngle + (y + height) * cosAngle);      tmpDots[2].set((x + width) * cosAngle - (y + height) * sinAngle, (x + width) * sinAngle + (y + height) * cosAngle);      tmpDots[3].set((x + width) * cosAngle - y * sinAngle, (x + width) * sinAngle + y * cosAngle);`

I saw this behaviour when I only used the glRotatef() method without translating, but in this case I really do not know how to translate it.
relminator
 « Reply #14 - Posted 2013-09-19 00:24:21 »

I don't think I understand what you're saying correctly.  Are you tring to orbit your sprite from an arbitrary point or rotate your sprite from its own center.
Opiop
 « Reply #15 - Posted 2013-09-19 00:44:08 »

I think he's saying the center. Although I have no idea...
Kronos

Junior Devvie

Medals: 1

 « Reply #16 - Posted 2013-09-19 12:17:45 »

Hello,
Yes I want to rotate the pumpkin around it's center and move it in its new facing direction. I Think it is the same as in GTA I + II. I think it is called Bird's-eye view. however, there is only missing the translation because it has now a too big rotation radius, and thats completely wrong. I hope you guys understand me better now
relminator
 « Reply #17 - Posted 2013-09-19 15:18:21 »

www.rel.phatcode.net/junk.php?id=140

There's a sprite rotate scale function in there you could modify.  Uses lwjgl.
Kronos

Junior Devvie

Medals: 1

 « Reply #18 - Posted 2013-09-19 18:04:34 »

Hello,

that helped a bit. But I'm stucking again.
The pumpkin is either in the left bottom corner, but rotates the right way. If I add x and y coords it runs faster then the particle effect, which uses only the x and y coords from the pumpkin. It has also the 4 Verctors for the polygon.

 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 `      float radAng = (float) Math.toRadians(this.angle);      float cz = (float) Math.cos(radAng);      float sz = (float) Math.sin(radAng);            float hx = (width/2);      float hy = (height/2);            float hx1 = -hx;    // top left      float hy1 = -hy;      float hx2 = -hx;   // bottom left      float hy2 =  hy;      float hx3 =  hx;   // bottom right      float hy3 =  hy;      float hx4 =  hx;   // top right      float hy4 = -hy;      // I added the x and y coords. looked good at first      float x1 = cz * hx1 - sz * hy1;      float y1 = sz * hx1 + cz * hy1;      float x2 = cz * hx2 - sz * hy2;      float y2 = sz * hx2 + cz * hy2;      float x3 = cz * hx3 - sz * hy3;      float y3 = sz * hx3 + cz * hy3;      float x4 = cz * hx4 - sz * hy4;      float y4 = sz * hx4 + cz * hy4;      tmpDots[0].set(x1, y1);      tmpDots[1].set(x2, y2);      tmpDots[2].set(x3, y3);      tmpDots[3].set(x4, y4);`

the pumpkin should be where the particle effect is.

any idea? I think I messed up everything by now
relminator
 « Reply #19 - Posted 2013-09-20 01:48:53 »

You forgot to add the translation to you final coordinates. (x,y)
Kronos

Junior Devvie

Medals: 1

 « Reply #20 - Posted 2013-09-21 19:58:56 »

Yeah you got it. Thanks for your patience
In addition that I forgot to add the coords I had a messed up Camera class.... I fixed it and everything is fine now. Many thanks!
Pages: [1]
 ignore  |  Print

 nelsongames (19 views) 2018-04-24 18:15:36 nelsongames (17 views) 2018-04-24 18:14:32 ivj94 (608 views) 2018-03-24 14:47:39 ivj94 (54 views) 2018-03-24 14:46:31 ivj94 (402 views) 2018-03-24 14:43:53 Solater (66 views) 2018-03-17 05:04:08 nelsongames (111 views) 2018-03-05 17:56:34 Gornova (177 views) 2018-03-02 22:15:33 buddyBro (747 views) 2018-02-28 16:59:18 buddyBro (94 views) 2018-02-28 16:45:17
 ByerN 12x KaiHH 12x SHC 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 10x Damocles 6x VaTTeRGeR 5x orangepascal 4x philfrei 4x ndnwarrior15 3x mesterh 3x ags1 3x Phased 2x CommanderKeith 2x delt0r 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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