Slick texture + Phys2d polygon mapping

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:


             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 :


            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:


          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 ?

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.

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:

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

this function, I guess is a bit faster than this:



    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:

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

http://www.hostmyjpg.com/thumbs/56c15949d3_example2.jpg

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

I load my texture image with this:

SlickMyGame.my_texture = new Image("data/square.png", false, Image.FILTER_NEAREST);

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:

//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.