Box2D Forums

It is currently Wed May 22, 2013 6:48 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Mon Jan 30, 2012 3:01 pm 
Offline

Joined: Mon Jan 30, 2012 2:37 pm
Posts: 3
Quick question:

My game has a ground shape (let's say a big rectangle), and I want to draw holes in it every couple of seconds. Is it possible? Any tips?




Thanks!


Top
 Profile  
 
PostPosted: Mon Jan 30, 2012 7:23 pm 
Offline

Joined: Wed Nov 16, 2011 6:18 pm
Posts: 28
Not totally sure what you mean. Box2d is a physics engine so you wouldn't actually DRAW holes in it. Thats for your rendering code (sfml,sdl,gl,etc...)

But if you wanted to actually punch holes in the solid body you would probably have to delete the solid rectangle fixture first.
Then define a new shape and create a whole new fixture with holes in it.
Keep in mind that you would actually have to break it into multiple fixtures:
because in box2d shapes must be convex(they cant have angles that point in..)

I'm still new myself to box2d myself but maybe you could create a bunch of tiles that are essentially shapes/fixtures and just destroy them every few seconds. Then it would appear as one solid ground body with holes being punched in it, when it is really just many squares being destroyed.


Top
 Profile  
 
PostPosted: Tue Jan 31, 2012 4:12 pm 
Offline

Joined: Mon Jan 30, 2012 2:37 pm
Posts: 3
I see... sounds reasonable, however the problem with tiles is I think it will either look bad or cost too much in performance. (Wouldn't it?)


I can maybe imagine another solution:

Let's say I have one big rectangle and two circles (representing 'holes' in the rectangle). Would it be possible to modify Box2d to then limit the rectangle's collisions only to where the circles do not collide? If so, how hard would it be?
kinda of "negating shapes" feature, so to speak...

Image


Top
 Profile  
 
PostPosted: Tue Jan 31, 2012 7:25 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
You mean, in the image, only collide to the part of the edge of the circle that is overlapping the rectangle?

No, it wouldn't be possible to do perfectly like that, even with modification, simply because Box2D can only handle convex polygons and perfect full (non-hollow) circles for simple one-shape bodies. The thing you wanted to do is a concave edge, and a curved one at that. You can make a multiple-shape (complex) body to make a concave shape, and doing so almost has no effect on performance.

So if you want to have a concave curved edge, you need to make a body that has a lot of polygons to make approximate edge. It won't be perfect, obviously, but that's the best Box2D can do in terms of concave curved edges.

The WCK port of Box2D makes use of a polygon deconstruction algorithm to generate the required polygons to approximate curved concave edges and even non-perfect circles. It uses lots of polygons, but regardless of the number of polygons used, the performance is still high and provides a good, accurate collision.


Top
 Profile  
 
PostPosted: Wed Feb 01, 2012 2:11 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1516
Location: Tokyo
No, you can't cut parts out of other fixtures like that.
One method to consider is making the ground out of a chain shape, so that you only need to define the outline of it. That way, cutting out a section is only a matter of moving the vertices that make up the chain. Here is a good example, this was made by the user r_bewick on these forums, so maybe you could ask him about the details.

http://www.youtube.com/watch?v=mr3aozeFHxQ


Top
 Profile  
 
PostPosted: Wed Feb 01, 2012 3:53 pm 
Offline

Joined: Mon Jan 30, 2012 2:37 pm
Posts: 3
irresistible force wrote:
No, you can't cut parts out of other fixtures like that.
One method to consider is making the ground out of a chain shape, so that you only need to define the outline of it. That way, cutting out a section is only a matter of moving the vertices that make up the chain. Here is a good example, this was made by the user r_bewick on these forums, so maybe you could ask him about the details.

http://www.youtube.com/watch?v=mr3aozeFHxQ


Thanks, looks impressive, but the problem is it will only support a solid shape while I also need to support holes inside the shape... :/


jayther wrote:
You mean, in the image, only collide to the part of the edge of the circle that is overlapping the rectangle?

No, it wouldn't be possible to do perfectly like that, even with modification, simply because Box2D can only handle convex polygons and perfect full (non-hollow) circles for simple one-shape bodies. The thing you wanted to do is a concave edge, and a curved one at that. You can make a multiple-shape (complex) body to make a concave shape, and doing so almost has no effect on performance.

So if you want to have a concave curved edge, you need to make a body that has a lot of polygons to make approximate edge. It won't be perfect, obviously, but that's the best Box2D can do in terms of concave curved edges.

The WCK port of Box2D makes use of a polygon deconstruction algorithm to generate the required polygons to approximate curved concave edges and even non-perfect circles. It uses lots of polygons, but regardless of the number of polygons used, the performance is still high and provides a good, accurate collision.

Looks like it's worth a shot, but implementing a polygon deconstruction algorithm from scratch sounds too difficult for me.
Is there some known implementation I can rely on?

(BTW I'm developing for iOS, currently with cocos2d, so related solutions would be even better)


Top
 Profile  
 
PostPosted: Wed Feb 01, 2012 6:04 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
I think the term is actually convex decomposition, not polygon deconstruction. Sorry x] You may be able to get more related results from Google with that.

But yeah, it is a rather complicated algorithm to make from scratch, even if you're already familiar with geometric stuff like Delaney triangulation. There is a Convex Decomposition thing in Box2D's contribution folder in Google Code ( http://code.google.com/p/box2d/source/browse/#svn%2Ftrunk%2FContributions%2FUtilities%2FConvexDecomposition ), but it's in C++. The syntax would be very similar with Objective-C, but not too much in class structure (I think).

ANYWAY, the chain shape solution is a great one, actually. With this, you won't need one body with lots of polygons (not that it's a bad thing, just complicated); just a few chain shapes will do the job. There are a couple of ways to do it (in my mind):

Image
Click here for larger image

Both solutions involve at least two chain shapes to create a hole. The first solution (the top bit in the image) is pretty straight forward; two big chains shapes with an approximately curved concave edge to make a hole.

If there are two holes, there would be three big chain shapes:
- a left chain shape that has its right side made for the left half of Hole 1;
- a middle chain shape that has its left side made for the right half of Hole 1 AND its right side made for the left half of Hole 2;
- and a right chain shape that has its left side made for the right half of Hole 2
(Sorry if that was confusing). The algorithm would be relatively simple for that until you have holes that are horizontally close together.

The second solution (which isn't very apparent in the small image) is to have two small chain shapes to make the hole, and two big chain shapes to take up the rest of the rectangle. This wouldn't have the problem the first solution had for horizontally-close holes. Just make sure to fill up the rest of the rectangle with big chain shapes (more than 2 if needed).

Both solutions are relatively simple to make an algorithm for separate holes until you have two holes together that are not touching the edge of the big rectangle. I'll leave that one up to you. Perhaps ask r_bewick (the guy irresistible force mentioned with the link) on how to at least connect a hole with the rest of the damaged terrain (in the video).


Top
 Profile  
 
PostPosted: Thu Feb 02, 2012 6:53 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1516
Location: Tokyo
I don't see why you can't have holes inside the main shape with chains. Just pretend that the outer chain defines an exterior surface and the inside chain defines an interior surface. As long as you draw the regions appropriately and don't let any game entities get into a region which is supposed to be solid, the player won't know any better. It will take some clever arranging to merge and break chains when things change though.

For convex decomposition you might want to look at http://code.google.com/p/poly2tri/ which supports interior holes. I used this recently to get polygons for drawing ground regions very much like what you're talking about (but not with holes or changing on the fly). If it helps, you could dig around the source code here -> http://www.ludumdare.com/compo/ludum-da ... w&uid=7091


Top
 Profile  
 
PostPosted: Thu Feb 02, 2012 7:58 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
irresistible force wrote:
I don't see why you can't have holes inside the main shape with chains. Just pretend that the outer chain defines an exterior surface and the inside chain defines an interior surface. As long as you draw the regions appropriately and don't let any game entities get into a region which is supposed to be solid, the player won't know any better. It will take some clever arranging to merge and break chains when things change though.
Oh yeah, hahaha. That's a lot simpler solution. For some reason, I was thinking chain shapes are filled. x]

As for things changing, you could have an "image" or "snapshot" of the rectangle being filled with holes, and construct the chain shapes that way. Every time a new hole is made into the ground, update the snapshot, destroy the existing chain shapes, and create new chain shapes based on the updated snapshot. It still requires a little bit of clever programming to make the chain shapes, but at least it will be mostly edge tracing and line intersections.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group