Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Slick texture + Phys2d polygon mapping  (Read 2550 times)
0 Members and 1 Guest are viewing this topic.
Offline marcin_85

Junior Newbie





« Posted 2012-05-14 19:46:24 »

Hello All:)

I have a simple question..

In my game I use Slick for graphic and Phys2d for physic simulation...

In my code part responsible for physic (Phys2d) i have code like this to create my polygon body:

            
1  
2  
3  
4  
5  
6  
7  
             static Vector2f[] nonConvexPoly = {new Vector2f(-60,-70), new Vector2f(20,-100), new Vector2f(10,0), new Vector2f(20,10), new Vector2f(-20,10), new Vector2f(-10,0)};

             polygon = new Polygon(nonConvexPoly); //Phys2d polygon
            nonConvexBody = new Body("poly", polygon, 30.0f);
             nonConvexBody.setPosition(350, 100.0f);
        world.add(nonConvexBody)
           


In my code part responsible for graphic (Slick) I have code like this :

            
1  
2  
3  
4  
5  
6  
7  
            slick_polygon= new Polygon(); // Slick polygon

            for (int i=0;i<nonConvexPoly.length;i++)
            {
               slick_polygon.addPoint(nonConvexPoly[i].getX(), nonConvexPoly[i].getY());  
            }
           


next I render(Slick) this polygon with my texture:
1  
          g.texture(slick_polygon, SlickMyGame.my_texture,1.0f,1.0f, true);


In effect, I have  "nonConvexPoly" shaped Slick texture and independent Phys2d Body :
 


How I can "glue" my Slick texture to Phys2d "nonConvexBody" ? and render this together ?
Offline davedes
« Reply #1 - Posted 2012-05-14 23:08:34 »

Rotate the Slick polygon to match the Phys2D polygon? (Transform class)
Or whenever the Phys2D body changes, update the Slick2D polygon with the new points from Phys2D?

Either way will probably not be very efficient. Slick also does a lot of stuff under the hood -- triangulation -- which could probably be made more efficient by handling it yourself. For example, I doubt Slick's triangulation is as feature complete / efficient as something like poly2tri, or GLU's own tessellation.

Offline marcin_85

Junior Newbie





« Reply #2 - Posted 2012-05-16 19:51:00 »

Thank for Your's help..

Because Im not a professional developer and my game it's just only for me and for fun I did this the simplest way:

1  
poly_set  = nonConvexPoly.getVertices(nonConvexBody.getPosition(), nonConvexBody.getRotation());

this function, I guess is a bit faster than this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public float rotateX(float angle, float x, float y)
    {
   return ((float)Math.cos(angle) * x - (float)Math.sin(angle) * y);
    }
   
    public float rotateY(float angle, float x, float y)
    {
     return ((float)Math.sin(angle) * x + (float)Math.cos(angle) * y);
    }



Now a have another problem..

I want to rotate my texture together with my polygon, but this don't work:

1  
2  
       SlickMyGame.my_texture.rotate(nonConvexBody.getRotation());
       g.texture(slick_polygon, SlickMyGame.my_texture,1.0f,1.0f, true);




The texture is still at 90 degrees Sad
It is possible to rotate texture image ?

I load my texture image with this:
1  
SlickMyGame.my_texture = new Image("data/square.png", false, Image.FILTER_NEAREST);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-05-18 17:59:02 »

That method doesn't take into account Image's rotation (I will try to clarify in docs).

Instead, use the Slick polygon as your renderable shape -- but create it upright and at (0, 0). When it comes time to render the shape, do something like so:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
//use the Phys2D body for x/y
g.translate(bodyX, bodyY);

//use the Slick Polygon for center x/y and the Phys2D body for rotation
g.rotate(polyCenterX, polyCenterY, bodyRotation);

g.texture(..);

//revert the transform
g.rotate(polyCenterX, polyCenterY, -bodyRotation);
g.translate(-bodyX, -bodyY);


Notice that this doesn't actually change the Slick Polygon at all; it remains static and is only used for rendering.

Some things to consider:
- Phys2D expects rotations in radians, Slick expects rotations in degrees
- Phys2D's positions are based on the center of the shape, Slick's positions are based on the top-left of the shape
- If you are using the Slick Polygon for collision, you won't get accurate results since it will always be (0, 0) and upright (zero rotation). So you should only rely on Phys2D for collision.

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.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!