Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) 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
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Slick texture + Phys2d polygon mapping  (Read 4172 times) 0 Members and 1 Guest are viewing this topic.
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

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

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
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!
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/yg.translate(bodyX, bodyY);//use the Slick Polygon for center x/y and the Phys2D body for rotationg.rotate(polyCenterX, polyCenterY, bodyRotation); g.texture(..);//revert the transformg.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

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (555 views) 2018-03-24 14:43:53 Solater (94 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (338 views) 2018-03-02 22:15:33 buddyBro (998 views) 2018-02-28 16:59:18
 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