We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Destructible Terrain (solution and discussion)
Page Index Toggle Pages: 1
Destructible Terrain (solution and discussion) (Read 1631 times)
Destructible Terrain (solution and discussion)
Aug 17th, 2007, 5:55pm
 
Recently I decided to make myself a Scorched Earth clone. The first challenge was destructible terrain. I managed to make a "fake" one, that is, an array of "holes" that are drawn over actual terrain. Its also easier to check for collision that way. Click to make a hole, + and - to adjust radius.
http://turpspages.freehostia.com/Dest_Terrain/

I wanted to know how you guys would do it, what techniques would you use to maybe create "real" one (that is, a shape that holes are actually made into).
Re: Destructible Terrain (solution and discussion)
Reply #1 - Aug 17th, 2007, 7:10pm
 
I believe that Scorched Earth and suchlike games store the landscape as an array of heights. When an explosion happens, te values nearby are changed as required, and posibly temporarily ome "in-the-air" dirt is created, which falls down , and is added to the height values.

More modern games, such as worms et-al probably use a similar method to yours, but may well actually erase/blank parts of a bitmapped/tiled landscape, instead of storing the holes, to make it a one-off calculation, rather than something that has to be continually kept in memory (e.g. if you have a lot of holes, you probbaly have to check many to see if a projectile is travelling through free space, or hitting the landscape, and that could become costly)
Re: Destructible Terrain (solution and discussion)
Reply #2 - Aug 17th, 2007, 7:32pm
 
Yeah, I remember now the odd 1 pixel lines that would sometime appear in Scorched. Gotta try that one...

And yeah, I guess that in the long run it can be better to do the expensive calculations at the whole creation rather than at each step later when we need actual collision detection.
Re: Destructible Terrain (solution and discussion)
Reply #3 - Aug 17th, 2007, 8:42pm
 
One of my bosses regularly uses the scenery as a collision map. Basically you can fall through png transparency, but not solid pixels which support you and push you up and out of the surface. A series of "pixel sniffers" manage this. Which is how she made this game with it's deformable landscape.

Levels were made in a custom, tile based level editor which she made and then when the game loads the XML for a level, it paints all of those tiles into a bitmap.
Re: Destructible Terrain (solution and discussion)
Reply #4 - Aug 18th, 2007, 12:00am
 
If you're looking for a physically realistic dirt explosion, a somewhat involved solution is to actually carry out a physical simulation of the involved area.  A simple simulation of a bunch of moving circles with some highly damped springs between close circles can do wonders.  Basically what you would do is first mark an "active" area, right around where the explosion hit.  Then you'd convert the previously static landscape in the area of interest into a bunch of particles.  Then apply an appropriate explosion force, and mark the particles as at rest (and hence stop simulating them) once they have settled down, and go until all the particles have settled.  You'd need a lot of particles (I'm guessing a thousand or more for a realistic blast), but if you really dig into the methods for physical simulation, this is not out of the question to achieve.  Though I'll warn you, the difficult parts of this are a) tweaking the forces between the particles so that it looks right, b) figuring out how to do the collision detection fast enough to handle all those particles, and c) figuring out how to draw all the particles in Processing without dropping the frame rate (cutting out the transformation pipeline is a good step here, esp. if you're using one of the 3d renderers and only actually drawing in 2d - that's a lot of per-vertex multiplications that you don't actually need!).  Maybe not the best solution for a simple game, but it would be interesting to try...(maybe if I ever finish up the game I'm currently working on I'll try to throw together a demo).
Re: Destructible Terrain (solution and discussion)
Reply #5 - Aug 19th, 2007, 2:15am
 
http://turpspages.freehostia.com/Dest_Terrain2

It looks and works way better than the other one, but there are a few problems:

-The applet is somewhat very laggy (the downloaded version doesnt lag at all)
-There are still a few bugs with the "makeahole"

Click to drop bomb, + - adjusts the size and any other key regenerates the terrain.

Only challenge remaining is "real" one where you remove a shape from a shape...
Re: Destructible Terrain (solution and discussion)
Reply #6 - Aug 19th, 2007, 8:41pm
 
I don't think that springs are necessary - you can get all of the motion just with particles.

I've uploaded a demo to my site of a deformable landscape with flying dirt as well.

To make things extra easy I used a PGraphics instead of a PImage - that means you can get really creative with the sort of craters you leave behind.

Le Demo

You can add bounce to it, but you have to check how far you penetrated into the soil. I have some code that does that but I think verlet integration is going to be heavy going for you as it is. Just remember with the particles to sort out collision corrections AFTER the "verlet" command to keep your physics stable.
Re: Destructible Terrain (solution and discussion)
Reply #7 - Aug 20th, 2007, 3:33pm
 
Yup, that demo is probably more in line with what Xeon is looking for (watch out though, it crashes if you bust through the bottom of the screen, I assume it needs a bounds check in the pixel test).  It really all depends how realistic you want to get - for gameplay purposes, a simple explosion without too much complication is probably quite enough.  You'd need springs and particle/particle (swept circle) collisions if you wanted to do something real serious, but it would be kind of involved.  The complicated algorithm I was envisioning would result in something along the lines of what I did a while ago at http://www.gamefight.org/liquid2/ (source available, though excruciatingly messy), though it would need to be pretty tuned to make something behave like dirt (probably a good bit of sliding friction would need to be added to the collision response to keep things from being "slippery").

Now to veer off topic: st33d, I've noticed several mentions of your using pixel based collision detection with a Verlet simulation.  Have you ever taken this "all the way," as in doing a full rigid body simulation with pixel by pixel detection?  I've always thought of that as sort of the holy grail of simple 2d physics libraries, since most of the libraries out there make you actually define the object in terms of the various pieces of simple geometry.  I was thinking it would be really nice to put together a Processing physics library where you would only need to supply a sprite and a mass and the library would handle the rest.  I've never dug into the pixel by pixel stuff, though, so I'm not sure how tricky it would be to pull off in a fast manner.  The real key would be to automatically set up a collision hierarchy so that you wouldn't have to test every single pixel every frame - I'm thinking that might be somewhat easy to accomplish by pre-caching alpha masks at halved resolutions all the way down to 1x1, but there are some details I haven't worked out.  But I like the pixel approach in that whereas in a normal broadphase collision algorithm you're essentially recreating from scratch a lot of geometrical information that's already been handled in rasterization.  Anyways, have you dealt with any of this stuff before, or do you have any thoughts on it?
Re: Destructible Terrain (solution and discussion)
Reply #8 - Aug 21st, 2007, 1:06am
 
Yes my demo breaks, but I already uploaded it and my internet connection is useless.

Why the hell would you need springs? Why? Why? Why? Why?

I'm using the Bresenham algorithm to resolve pixel collisions in my latest game. I would post the code but it's too long. Generally I just check for being stuck in the landscape and then if I am I then draw a line from where I was to where I am now. Because B-ham walks every pixel to the destination faster than you can say something quite short, I can tell how much x,y penetration there is. So I put the last position of the particle there, place the particle outside of the pixels and I have a collision resolved plus the appropriate bounce.

Tie it all together with springs and you get some sticky but realistic collisions against a pixel map (note that I haven't figured out how to resolve collisions against a pixel wall thinner than your current velocity).
Re: Destructible Terrain (solution and discussion)
Reply #9 - Aug 21st, 2007, 3:05pm
 
>> Yes my demo breaks, but I already uploaded it
>> and my internet connection is useless.

Understood - during college I lived off campus and had nothing but dialup AOL, so I'm no stranger to the crappy connection.  Even just to download a PDF file for a class could take up to an hour, which made things quite difficult...in any case, don't worry about it, proof of concept demos are entirely exempt from any expectation of debugging, in my opinion.

>> Why the hell would you need springs? Why? Why? Why? Why?

True, I never did explain this...you need springs if you want to physically model dirt as opposed to sand.  A pile of sand is very well approximated (for the purposes of game physics, at least) by a whole bunch of spheres undergoing almost completely inelastic collisions (too much jitter if they are elastic).  If you hit a pile of spheres with a big heavy sphere, it will basically just spray everywhere, with no clumping or anything like that, just like real sand does when you kick it.  In contrast, if you play around with some actual dirt, it clumps together, and if you stick an M80 in a pile of it, you'll blow it up and get a lot of reasonably sized chunks flying through the air.  That's why you'd need springs, if you wanted to capture that clumping aspect of a ground explosion.  In reality, though, it's probably easier just to use a bunch of colliding spheres and attach clump-shaped graphics to them, since that will look almost as convincing.

Re: collisions against thin pixel walls.  My latest Verlet project was exclusively points and lines, so I've dealt a lot with "tunneling" issues, though not pixel based.  Since you've already got Bresenham's algorithm running, couldn't you do a rasterization from the last position to the current position and pick out a collision point that way rather than simply testing the current position for penetration distance?  This would require a Bresenham calc each time step rather than at each reported collision, which is slightly more computationally intensive than a single pixel check, but that's the price we pay for avoiding tunneling...anyhow, with today's computers you can do hundreds/thousands of lines with Bresenham without maxing out the processor, so it shouldn't be too much of an issue to do one for each particle (the mere drawing of the particle itself, if you're using circles, will outweigh the cost of the collision check).

Generally speaking a collision detector provides at least two things: 1) a collision point, and 2) a collision normal.  For the collision point, just run Bresenham from last->current and pick out the first occupied pixel.  For the normal things get a bit more complex, because the normal direction is not really specified from a pixel map - more thought would be required there, probably something along the lines of checking the neighborhood of the collision point and massaging that data somehow to come up with a normal direction.  It sounds like you're getting by without using normals anyhow, so you may not need to go there.

BTW, once I finish up the engine for the game I'm working on I'll post the source, where I have a lot of useful (and painfully debugged) code for doing Verlet collision resolution.  You might find some of it useful for your stuff.  At the moment I'm still tinkering, though, so I want to get it right before I put it online.

In any case, am I correct to assume that you've not tried to deal with bodies that have extent in this way? (as in full sprite-on-sprite collision)  At some point I might take a swing at that, P3D and P2D have some nice, fast triangle rasterization routines that could be modded to do overlap queries without too much trouble.  Even swept detection might not be too slow in such a scheme...would be interesting to try, at least.
Re: Destructible Terrain (solution and discussion)
Reply #10 - Aug 21st, 2007, 10:20pm
 
>>Generally speaking a collision detector provides at least two things: 1) a collision point, and 2) a collision normal.  For the collision point, just run Bresenham from last->current and pick out the first occupied pixel.

This is what I'm doing.

However it gets a little more complicated because B-ham will only travel in one specific direction. For 4 of the octants it's business as usual - but for the other 4 you have to swap the start and end points - otherwise the algorithm breaks. Thus half the time you are going from previous to current and the other half from current to previous.

This is simply how B-ham works.

But it's still easy to logically find the point outside the surface. And where does the normal come from? You take the point where you were at inside the surface and you use the x,y difference from where you are now outside and you have your normal.

This means you get some pretty weird bounces (because it's not a line surface vector bounce).

But springs (though overkill they way you were on about it) actually help here. Make a spring box (a group of particles tied together by a mesh of springs) and you get some amazingly realistic collisions off of pixels - this is because that odd bounce actually becomes a harmonious reaction throughout the spring structure.

I had thought I could try to get a localised screen grab for when the springbox and enemies collide and have universal pixel collision - but we're pressed for time - I'm just kicking the ship away from the point the enemies explode from.
Re: Destructible Terrain (solution and discussion)
Reply #11 - Aug 21st, 2007, 10:52pm
 
Hm, I see, so the problem arises when both the beginning and end points are outside the occupied pixel because you have no control over the direction the algorithm goes in, so the algorithm will sometimes actually give you the exit point instead of the entry.  You could probably hack around this by forcing the algorithm to run to completion and return both the first and the last occupied pixel, then outside the algorithm determine which one is the entry pixel, right?

Anyhow, it's a very interesting approach to this problem, I'm very curious to see how it turns out - definitely post a link to the game when it's done!  I'm going to be away for a few days, but when I get back I may start experimenting with this method for rigid bodies, since if it worked for those, it would result in an extremely intuitive and beginner-friendly physics engine.  I'm sure even without rigid bodies built in it speeds level development for you quite considerably, turning any run of the mill paint program into a full featured level editor.  Having just spent three full weeks working on a custom level editor for the stuff I'm working on, I'm starting to wish I'd gone with your type of approach from the beginning!
Page Index Toggle Pages: 1