简体   繁体   中英

How to create a polygon on a texture libGDX

I want to create a polygon by cutting the transparent area from a texture. I wrote a program which detects collision between 2 objects. But the collision is detected before they are in collision. That's how it looks: http://imgur.com/a/dFmXI

This is the code: https://hastebin.com/bebusadiku.java I thought to cut the texture and create a polygon based on the cutted texture and then detect the collision using an Intersector method but I have no idea how to specify the vertexes of the polygon.

I tried to follow this but there's no explain how to detect the vertexes : https://gamedev.stackexchange.com/questions/114228/how-to-detect-collision-only-in-non-transparent-texture

Thanks for help

I used this polygonal shape editor to create more complex shapes for checking collisions. I believe with Box2D it works out of the "box" but Box2D takes a lot of space and that is unnecessary if you don't use the actual physics. However, this tool generates JSON formatted polygonal coordinates and it's not that hard to deserialize that back into Polygon objects and do collision with the Intersector implementation . So in steps you:

  • Create a shape for your images using the shape editor tool .
  • Export that to JSON with the tool.
  • Deserialize that data into a LibGDX Polygon
  • Move, scale and attach it properly to your image so it's synchronized.
  • First check collision on a basic rectangle vs rectangle.
  • If there is a basic collision do a more detailed collision detection with Intersector .

The tool exports a lot of redundant data, if you get yourself familiar with the format it exports you can get rid of all data except the polygon/vertices data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM