Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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  
  Calculating rotated points of a rectangle  (Read 1299 times)
0 Members and 1 Guest are viewing this topic.
Offline PRW56

Senior Newbie





« Posted 2013-10-31 17:01:30 »

Hello, I am using vertex buffers to draw this at the moment, but for some reason after the program is launched, the thing that is being drawn appears in the top left corner (close to 0,0) when it is supposed to be at (100,100). The sprite is being rotated around a point Center (a Vector 2), and the Center is being set to (X + (width of sprite)/2) and (Y + (height of sprite/2)) every time the x or y of the sprite is being changed. When I made it so that the rotation (which up until this point had been set at 0) increased every frame, the sprite seemed to rotate around a point close to (0,0). Is there a way I am calculating the new position wrong? Ty for any help!

Position is a Vector 3 by the way and although a Z is in there I am only doing 2D things right now

here is code for calculating verticies:
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  
float x, y;
         float cosRot = FMath.Cos(Rotation);
         float sinRot = FMath.Sin(Rotation);
         
         // Point 1 - top left
         x = Position.X - Center.X;
         y = Position.Y - Center.Y;
         vertices[0]=(float)(x*cosRot - y*sinRot);   // x0
         vertices[1]=(float)(x*sinRot + y*cosRot);   // y0
         vertices[2]=Position.Z;                  // z0
         
         // Point 2 - bottom left
         x = Position.X - Center.X;
         y = (Position.Y + height) - Center.Y;
         vertices[3]=(float)(x*cosRot - y*sinRot);   // x1
         vertices[4]=(float)(x*sinRot + y*cosRot);// y1
         vertices[5]=Position.Z;                  // z1
         
         // Point 3 - top right
         x = (Position.X + width) - Center.X;
         y = Position.Y - Center.Y;
         vertices[6]=(float)(x*cosRot - y*sinRot);   // x2
         vertices[7]=(float)(x*sinRot + y*cosRot);      // y2
         vertices[8]=Position.Z;                     // z2
         
         // Point 4 - bottom right
         x = (Position.X + width) - Center.X;
         y = (Position.Y + height) - Center.Y;
         vertices[9]=(float)(x*cosRot - y*sinRot);   // x3
         vertices[10]=(float)(x*sinRot + y*cosRot);// y3
         vertices[11]=Position.Z;               // z3
Offline Rakiayn

Senior Newbie





« Reply #1 - Posted 2013-10-31 19:58:44 »

you might want to check out this post
http://www.java-gaming.org/topics/rotation-matrix-example/30834/view.html
you can use the code in the second to last post by drzoidberg.
the rotation matrix is for 3d. but you can use it for 2d too.
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2013-10-31 21:27:38 »

I made a function like this a while ago, maybe you could use it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
    public static Vector2f[] rotate(Vector4f src, float rotation){
        Vector2f[] dst = new Vector2f[4];

        float sin = FastMath.sinf(rotation);
        float cos = FastMath.cosf(rotation);
        float wsin = sin * (src.z / 2);
        float wcos = cos * (src.z / 2);
        float hsin = sin * (src.w / 2);
        float hcos = cos * (src.w / 2);
        //x' = x*cos(t) - y*sin(t)
        //y' = x*sin(t) + y*cos(t)

        dst[0] = new Vector2f(src.x + wcos - hsin, src.y + hcos + wsin);
        dst[1] = new Vector2f(src.x - wcos - hsin, src.y + hcos - wsin);
        dst[2] = new Vector2f(src.x + wcos + hsin, src.y - hcos + wsin);
        dst[3] = new Vector2f(src.x - wcos + hsin, src.y - hcos - wsin);

        return dst;
    }
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.

DarkCart (7 views)
2014-10-31 21:44:48

DarkCart (7 views)
2014-10-31 21:43:57

TehJavaDev (38 views)
2014-10-27 03:28:38

TehJavaDev (29 views)
2014-10-27 03:27:51

DarkCart (43 views)
2014-10-26 19:37:11

Luminem (24 views)
2014-10-26 10:17:50

Luminem (29 views)
2014-10-26 10:14:04

theagentd (35 views)
2014-10-25 15:46:29

Longarmx (63 views)
2014-10-17 03:59:02

Norakomi (61 views)
2014-10-16 15:22:06
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!