PDA

View Full Version : Erase part of an object


gatzke
01-23-2010, 10:09 PM
Hi, just wondering how I can erase part of object "A" using object "B". For example if object A was a square and object B was a circle, object A would have an arc cut out of it. My plan is to have a block of terrain that can be affected by explosions. After the explosion occurs, other objects should be able to roll down the crater created by the explosion. I'm guessing bitmapdata would have something to do with it?

Cheers.

lotsofsloths
01-24-2010, 12:27 AM
I think what you need is masks.
Basically lets say you have a sprite called mySquare which, as the name indicates, is a graphic of a square. Lets say you have have another graphic called myCircle whose radius is double the size of a side on mySqaure(it fits exactly inside mySquare). Now, if you say
mySquare.mask = myCircle;
all you are going to see is a circle with the color of mySquare. What happened was you told actionscript to cover up every bit of mySquare except for the shape of myCircle, the mask.

Here is some basic code:

var myMask:Sprite = new Sprite;
myMask.graphics.beginFill(0x000000);
myMask.graphics.lineStyle(0,0x000000);
myMask.graphics.drawCircle(50,50,50);
myMask.graphics.endFill();
//no need to addChild(myMask) because we want to use it as a
//mask, not as a visible object onstage,

var mySprite:Sprite = new Sprite;
mySprite.graphics.beginFill(0x000FF00);
mySprite.graphics.lineStyle(0,0x000000);
mySprite.graphics.drawRect(0,0,100,100);
mySprite.graphics.endFill();
addChild(mySprite);

mySprite.mask = myMask;

gatzke
01-24-2010, 01:00 AM
Hmm, this will take care of the appearance of a crater, but will it create a physical crater that another object could roll down? Think of it as a real world situation: a car is driving over the road, then a bomb goes off on the road in front of the car, then the car goes into the crater made by the bomb. I'm trying to create that crater.

lotsofsloths
01-24-2010, 01:58 AM
I am not exactly sure what you are trying to say..
You can make one mask, and assign it to different graphics, it can be used more than once:
graphicOne.mask = crater;
graphicTwo.mask = crater;

gatzke
01-24-2010, 04:27 AM
I think you're not getting me - what you're doing is adding something to make it look like there's a crater. I want to physically remove pixels from the original object. Think of it as an apple - you take a bite from the apple - now there's a crater left in it - you've removed particles of apple.

AdityaGameProgrammer
01-24-2010, 07:29 AM
can i suggest a very simple alternative if you haven't already thought of it before? .
just make the crater into a movie clip with the frames containing the crater in deformation in stages. a general deformation.
think of crater as a player and it getting affected by explosion as a walk cycle animation.
you would call it as and when needed.
this is the trick used for most destructible environment objects in flash games.
besides the obvious exploding barrels. deforming cars. benches ,trees, etc....

or you could add patches of small craters to spots at random. like in the bullet holes on the walls in some flash shooter games.

have you thought of this?

gatzke
01-24-2010, 06:01 PM
I really don't know how else to explain what I want so I'll provide a link to a game below. When you try the game, notice how the explosion makes a hole in the ground and that you can then drive into the hole with your tank. This is exactly what I'm looking for. http://www.addictinggames.com/tankwars.html

ASWC
01-24-2010, 07:00 PM
I think they understand perfectly what you want but only in term of code. You can't have a hole make in a bitmapdata and suddenly object magically falling through, that has to be a code taking care of that. The way the object looks (with a hole let's say) and the code making object go through the hole are two different things.

gatzke
01-24-2010, 07:32 PM
I think they understand perfectly what you want but only in term of code. You can't have a hole make in a bitmapdata and suddenly object magically falling through, that has to be a code taking care of that. The way the object looks (with a hole let's say) and the code making object go through the hole are two different things.

Yea.. I know it's code - exactly what I'm looking for. Do you know how I can achieve that effect?

ASWC
01-24-2010, 07:47 PM
Here you go! Now your question is quite different and people might be able to answer it. BTW I'm moving this to the game section ...

gatzke
01-24-2010, 08:01 PM
Here you go! Now your question is quite different and people might be able to answer it. BTW I'm moving this to the game section ...

If you look at my original question I clearly mention bitmapdata. It was the others who ultra-skimmed my post and went in the other direction. And.. I just used that game as an example of what I was looking for - I think this post is still actionscript 3.0 related.
But I'd rather not get a bad name with the admins.. So do what you have to do. :D

cutter
01-24-2010, 08:04 PM
The approach you should use depends on how your game is represented both logically and visually.

If you are using tiles, others have already shown how to appropriately mask images to get the visual effects of craters. With this approach, you'd need to separate your collision logic from the tiles themselves. An approach I've used is connected line segments -- on an explosion, you can apply the visual mask and then subdivide the appropriate line segment(s) to provide a concave shape (that would ideally match your crater visual.)

The game you linked to does not take this approach -- they are using procedural terrain, by manually filling the ground pixels or perhaps polys. In that game, the visuals and collisions are automatically linked together, as it seems they are simply using pixel level collision.

To achieve that, you simply need to have a bitmap of your terrain pixels being drawn over your background and removing pixels (i.e. set them to transparent) appropriately on impact. Thus you get the visual effect and the collision logic synchronized together.

Edit: Apparently it's not a game you're making, but the above still holds true :) If the explanation above isn't good enough for you to write the code, I might try to write some later, time permitting.

gatzke
01-24-2010, 08:42 PM
Thank you Cutter - very well explained. If only "hitTest" was a little more advanced - would make things alot easier!

cutter
01-24-2010, 09:00 PM
NP! Here is a really nice tutorial for implementing terrain deformation like in the game you linked (and presumably like you want.)

http://blog.xna3.com/2007/12/2d-deformable-level.html

The code is in C#, not AS3, but just looking over it, it seems fairly straightforward and doesn't look too evil to port to AS3. Good luck!