Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Make any object explode

Author
Message
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 9th Aug 2010 20:11 Edited at: 16th Nov 2010 18:08
This is a refined version of Dr Tank's exploding sphere using vertexdata. I have converted all the arrays into memblocks and put the whole thing into a set of functions so you can now make any number of objects explode all over the place .

Version 3 (11/12/10)

- This version has 3-axis floors, which should have been added with 3-axis gravity
- This version allows you to specify where the center of the explosion will be at explode-time. Because of this, some of the setup functions needed to be moved to BOOM_ExplodeObject, so expect that to be a little slower.
- BOOM_SetupObject now preserves the object's position and rotation

BOOM_SetupObject(ObjectNo,FloorX,FloorY,FloorZ) gets the object ready to blow up. The Floors are the global positions where the fragments will stop moving.
BOOM_Update() should be called each loop. It moves all the pieces.
BOOM_ExplodeObject(ObjectNo,Violence,vx,vy,vz,ox,oy,oz) explodes the specified object. Violence is how forceful the explosion is. The vx-z values are initial velocities on each axis. The o values specify the center of the explosion relative to the center of the object.
BOOM_UnsetObject(ObjectNo) removes the object from the explosion system but does not delete it or reset its vertexdata.



Version 2 (9/12/10)

- This version has 3-axis gravity, and the way gravity is handled has been changed slightly, so you may need to redo your gravity values.
- This version does not use the BOOMID system to identify the objects, it just uses the object number.

BOOM_SetupObject(ObjectNo,FloorPos) gets the object ready to blow up. The FloorPos is the global y position where the fragments will stop moving downward. Note that calling this command resets the object's position and rotation.
BOOM_Update() should be called each loop. It moves all the pieces.
BOOM_ExplodeObject(ObjectNo,Violence,vx,vy,vz) explodes the specified object. Violence is how forceful the explosion is. The vx-z values are initial velocities on each axis.
BOOM_UnsetObject(ObjectNo) removes the object from the explosion system but does not delete it or reset its vertexdata.



Version 1

ID=BOOM_SetupObject(ObjectNo,FloorPos) gets the object ready to blow up. The FloorPos is the global y position where the fragments will stop moving downward.
BOOM_Update() should be called each loop. It moves all the pieces.
BOOM_ExplodeObject(ID,Violence) explodes the specified object (ID is the value that was returned from BOOM_SetupObject). Violence is just how forceful the explosion is.

In this demo, left-click to explode the sphere, and right-click to explode the cylinder.



Latest progress: Fog of War implemented; frustrated by pathfinding >.>
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 9th Aug 2010 20:41
BMacZero ,Brilliant now we can all make awesome shooting games, something like breakout but now you have avoid the fallout.

still looking at how you've done it but wondering would it go backwards, bang it blows up, then it replay backwards and gets rebuilt as a whole, to be blown up again.

Dark Physics makes any hot drink go cold.
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 9th Aug 2010 23:15
The functionality of the code isn't really mine, so I'm not completely sure how to go about doing that. I do think this could be rewritten to use a lot less memory storing angles and vectors and whatnot, so I might at some point re-write it so it can do that.

Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 10th Aug 2010 03:42
Nice one dude. With the change from arrays to memblocks it's easy to have a bunch of different objects with different numbers of polygons, without wasting loads of memory by making the arrays too large.

It's sweet that this uses functions and stuff. Hopefully people will find it easy to use this in their projects. The original version was less user friendly, and I'm glad you went to the trouble of figuring it out.

About the reverse time thing. Yes it's doable, but would require a bit of coding. Notice how the particles' velocities are multiplied by a simple constant each cycle. This means the positions have exponential time dependence decaying towards a final position. The vertical position has a linear (in time) component on top. The angle obviously just has linear time dependence. If the particle is in the ground, you can just figure out the time for it to hit the ground, and use the positions, angles at that time. Easier said than done of course.

Anyway top job.
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 10th Aug 2010 05:18
Quote: "About the reverse time thing. Yes it's doable, but would require a bit of coding. Notice how the particles' velocities are multiplied by a simple constant each cycle. This means the positions have exponential time dependence decaying towards a final position. The vertical position has a linear (in time) component on topout. The angle obviously just has linear time dependence. If the particle is in the ground, you can just figure out the time for it to hit the ground, and use the positions, angles at that time. Easier said than done of course."

Thanks, Dr. Tank. That actually doesn't even sound that easy, though . Perhaps it's possible to just have a calculation that figures out where any given fragment would be at any given time, then you can just put in the times backwards.

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 10th Aug 2010 05:46
Actually, the reverse time thing doesn't sound too hard. It would be very easy if the particles didn't interact with the ground. Just solve for the parametric equations of x, y, and z over time. For the sphere, it should be as easy as filling in the variables:
x=at+b
y=-ct^2+dt+e
z=ft+g
and then instead of increasing t, decrease t.
Or, if you don't want to do that, just step the particles back in time.

With the particles interacting with the ground... meh.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Bursar
15
Years of Service
User Offline
Joined: 17th Sep 2008
Location:
Posted: 10th Aug 2010 10:50
Excellent, thank you to both Tank for the original code, BMac for this update.
gwheycs62egydws
14
Years of Service
User Offline
Joined: 17th Aug 2009
Location: The World
Posted: 3rd Sep 2010 06:47
HI

i Just found your code

if the direction can be controlled and how much detail

it brings me one step closer to real world damage

Resourceful@shaw.ca

If a thought is Just a thought ~ so whats the main thought ?
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 3rd Sep 2010 06:57
It would take some coding to change the direction, noted but I don't know when I'll get around to it.

This works on a polygon-by-polygon level, so to increase the detail you need to use more polygons on the area you want greater detail on.


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
MSon
19
Years of Service
User Offline
Joined: 13th Jul 2004
Location: Earth, (I Think).
Posted: 10th Sep 2010 19:20
nice snippet, ability to shoot crates, ect to bits is always nice.

Everyone Be Cool, You, Be Cool
Life Isn't fair, Its Just fairer Than Death
Death is doing nothing, life is doing what others tell you
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 10th Sep 2010 21:22
While you couldn't necessarily use this on a building to say, blow out a wall. This would work great for breaking a window and having it shatter. You would just have to model it with uneven polys to make it look more realistic.

gwheycs62egydws
14
Years of Service
User Offline
Joined: 17th Aug 2009
Location: The World
Posted: 10th Sep 2010 21:36
so build a hole model break it down it's it's limbs and
and get the program to go after that limb and do the deed ;o)

it would be getting the direction set
that would be the only thing to be worked out next

so anther 2 function would need to be created

taking apart a 3d object
and direction of item being broken

which means eventually for speed purposes
these 2 could be built in a dll for speed and giving
the user a quick command set to achieve intended goal

If a thought is Just a thought ~ so whats the main thought ?
gwheycs62egydws
14
Years of Service
User Offline
Joined: 17th Aug 2009
Location: The World
Posted: 12th Sep 2010 07:38 Edited at: 12th Sep 2010 07:39
I found a the Portuguese version of this
forum which lead me to a useful link

the originally link
http://www.darkbasic.es/foros/viewtopic.php?t=266

well back to this one
http://forum.thegamecreators.com/?m=forum_view&t=90484&b=6

the examples posted a useful command I did not pay attention
basically you can make holes in objects and or even give the appearance of a dent with a bit more work into an object to show damage dun

so with the code that's here and the commands I did not know about
an object can be changed as things happen to it

one question I did have about the code originally listed in here

how many much info could be handled by it before
it over loads the computer ?

for some of the games and simulation I got in mined
there are going to a lot of 3d model and a lot of different
environments

Things to think about ;o)

If a thought is Just a thought ~ so whats the main thought ?
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 12th Sep 2010 18:03 Edited at: 12th Sep 2010 18:29
That would be cool to rig up something to use CSG to take a piece out of a wall, then blow it up. Would take some doing though.

Quote: "how many much info could be handled by it before
it over loads the computer ?"

I think each object requires about 40 floats of data per face = 160 bytes per face. So a 1000 polygon object will take up 160000 bytes or 160 kb in memory. If you're having problems with running out of memory, or think you might, you should just wait until right before you need to explode the object to set it up.

By the way, I'm posting a new version in a few minutes . This version lets you set the direction of the explosion!


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
gwheycs62egydws
14
Years of Service
User Offline
Joined: 17th Aug 2009
Location: The World
Posted: 12th Sep 2010 21:54
I know one way to speed things up would be to keep long
distance objects detail down low then that keeps memory
more open and if the computers take anther jump in speed
that is going to help too

CSG command I can defiantly see using a lot
one I learn to to use it better

I noticed the ball was a slower squish
and crack up lol

if exploding out what both examples do now
exploding in might be anther thing to think about

check out
http://www.worldsapartonline.net
free to play

the guy uses DarkNet in making this online game
it's still a work in progress but it runs gooood

If a thought is Just a thought ~ so whats the main thought ?
Adrian
20
Years of Service
User Offline
Joined: 11th Nov 2003
Location: My Living Room
Posted: 1st Nov 2010 22:54
This code is brilliant!
I am definitely going to use this.
One thing I found is when you explode an object, it appears to move to location 0,0,0 from wherever it is when you blow it up so you have to store the x,y and z coordinates first, then start the explode routine and then place the object back to where it originally came from. Other than that, this gets my vote as best bit of coding this year!
Well done
Have a on me
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 3rd Nov 2010 08:15
Hmm, thanks much. Easily enough worked around I suppose, if I make another version with other significant changes I'll have the function take care of that itself.

I'll make a note in the first post.


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 10th Nov 2010 18:39 Edited at: 10th Nov 2010 18:41
Great coding

I'd like to contribute with the DGDK version:



It has some minor differences.
I have included an individual violence per face, based on the overall violence.
Also the struct array for obejct Ids is fixed in size (not so crucial), while keeping the dynamic allocation for the vertex and face arrays.

I've noticed that fragments (faces) dissapear when the exploded object is not on the screen. To keep on displaying fragments when the object coordinates are not in screen, I added:


inside of the BOOM_Update function, object 'for' loop.

@BMacZero, @Dr Tank, Many thanks for the contribution to the community. I will be crediting you, so let me know your real name if you want it to appear

As an improvement suggestion, I think that, apart from the explosion direction, I would be nice to be able to offset the center of the explosion.
Right now objects always explode from the center. To have an offset for this center could simulate the bullet hit impact position more realistically.
Any ideas about how this can be achieved?

DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 11th Nov 2010 17:37
Looks very complicated, but works really well! Perfect for any sort of game that involves destruction! I wonder how it would cope with a more complex modeled object? I may well try one out and see lol! If I can get my head round it this will be very useful for my latest project.

http://s6.bitefight.org/c.php?uid=103081
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 11th Nov 2010 20:55
Nice, Morcilla! Should make the function even more useful.

As for exploding the object from a position offset from its center, I can think of an easy way. I'll see if I can write it in today.


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 12th Nov 2010 09:04
Quote: "I wonder how it would cope with a more complex modeled object?"

I've tested it with more than 10k polygons and complex meshes, it works perfectly, and doesn't go below 30 fps.

Quote: "As for exploding the object from a position offset from its center, I can think of an easy way. I'll see if I can write it in today."

Hey that's great, glad you liked the idea

BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 12th Nov 2010 18:44 Edited at: 12th Nov 2010 18:44
It's up now .

As a side note, I just discovered this makes a really weird effect when you set the violence of the explosion to 0 .


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 13th Nov 2010 18:34
Hey that's great. I'll have a closer look at it soon

Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 14th Nov 2010 03:20
Quote: "@BMacZero, @Dr Tank, Many thanks for the contribution to the community. I will be crediting you, so let me know your real name if you want it to appear"

Awesome! It's great that people keep improving this. My name is
.

Quote: "As an improvement suggestion, I think that, apart from the explosion direction, I would be nice to be able to offset the center of the explosion.
Right now objects always explode from the center. To have an offset for this center could simulate the bullet hit impact position more realistically.
Any ideas about how this can be achieved?"

Yep.

Quote: "As for exploding the object from a position offset from its center, I can think of an easy way. I'll see if I can write it in today."

You beat me to it! I thought to put the velocity proportional to the distance from the origin, plus the displacement vector. You thought to displace the object centre position. They pretty much do the same thing though. Nice work.

I think you could speed up the code significantly by losing the recalculation of object bounds. The motion of any particle is quite deterministic, with simple linear damping. The particle centres are in the same distribution as they are when they were "together", just "blown up", until they hit the wall. Therefore it's possible to calculate the size of a box/sphere that will contain all the particles, given the object bounds when unexploded, then add a little for the maximum size of an individual fragment. It's complicated by the wall too. Not trivial but it's doable.

Since you introduced "violence per face", you'd simply need to do this calculation for the maximum face violence.

Can you manually set object bounds? If not, set object radius may do. Is object bounds for a box or sphere?

Maybe the bounds calculation command is fast relative to the other stuff anyway.
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 15th Nov 2010 12:06 Edited at: 15th Nov 2010 12:08
Well, I've tried version 3, and it doesn't seem to be working fine

I cannot see objects exploding if I change the boom gravity, from:



to



I have had a look at the code but I haven't found any clues about what's wrong now

Quote: "Version 3 (11/12/10)

- This version has 3-axis floors, which should have been added with 3-axis gravity"

That seems a logical addition, nice work
I've seen some others overall improvements here and there.

Quote: "- This version allows you to specify where the center of the explosion will be at explode-time"

Quote: "I thought to put the velocity proportional to the distance from the origin, plus the displacement vector. You thought to displace the object centre position."

Well, I think the current approach gives some good results, but it is not exactly what I was thinking of.
It is only the direction of each face the one that should be changed, not the velocity.
Besides the velocity is greater now for the faces that are further away from the explosion center, and lower for the faces near the center of the explosion, shouldn't it be the opposite way?

As said, I was thinking about changing just the direction, like this:



The current method seems to give this result, or very similar, but I think that the velocities should be equal for each face, based on the violence parameter, or if to be changed, the velocities should be higher the shorter the distance of the face from the explosion center.
Current result is more like a push or like beating the object with a baseball bat.

Quote: "I think you could speed up the code significantly by losing the recalculation of object bounds."

Yes, thanks for the suggestions, it doesn't slow down very much in the DGDK version. Also I must say that this program seems to stress DBPro version, while the performance is much better with DGDK. I talk above about 30fps with a 10k poly mesh, but that was using DGDK, DBPro goes down to 10 fps...

Attachments

Login to view attachments
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 16th Nov 2010 18:07
Quote: "Well, I've tried version 3, and it doesn't seem to be working fine "

Found the problem. The direction of the gravity is used to determine which way the floors "face", so when you set the gravity to zero the program was deciding that the floors faced in the negative Y direction. That's fixed now.

Quote: "The current method seems to give this result, or very similar, but I think that the velocities should be equal for each face, based on the violence parameter, or if to be changed, the velocities should be higher the shorter the distance of the face from the explosion center."

That does make more sense...I might play around with that.


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 17th Nov 2010 11:15 Edited at: 17th Nov 2010 11:15
Quote: "Found the problem. The direction of the gravity is used to determine which way the floors "face", so when you set the gravity to zero the program was deciding that the floors faced in the negative Y direction. That's fixed now."

Yes that works now, great

Quote: "
[quote]... the velocities should be higher the shorter the distance of the face from the explosion center."

That does make more sense...I might play around with that.[/quote]
Yes I think it does, I hope it results more realistic and spectacular

Adrian
20
Years of Service
User Offline
Joined: 11th Nov 2003
Location: My Living Room
Posted: 29th Nov 2010 13:03
I'm using V2 of the command and noticed something that may have been changed in V3. If I set the gravity to negative so when I blow something up, all the pieces fall upwards, I would like them to hit a ceiling. If I set the floor above the object in question, it doesn't explode (as it is burried under the level of the floor I guess.)
It would be nice to have a "ceiling" as well as/instead of a floor.

Quote: "
The direction of the gravity is used to determine which way the floors "face"
"

I guess this might be the same thing.

I also noticed that there is a maximum number of objects that you can't exceed (50 ish if I remember correctly.) I think this has something to do with the number of memblocks being used but am not sure. When I want to blow something up I therefore have to set the object up immediately before I blow it up rather than setting everything up at the start of the program and just explode them when I need to. I had to change the way my program works to accomodate this (which I did willingly.) Just a tip so others don't make the same mistake I did.

As you can have control over each piece of an object, could it be possible to morph one object into the shape of another? So if you explode a cyclinder, it could reform as a sphere? I'm not asking for this, just thinking out loud.


I must say that this code is rather fine and I will definitely be using it in most if not all of my programs to come. You should feel a warm fuzzy glow all over. Very well done.
And Dr. Tank too. Whenever I see a really cool piece of code on this forum, your name is often mentioned also. (Spherical and plane portals for example - I will probably use those too )

A for you both then

Login to post a reply

Server time is: 2024-03-28 22:57:05
Your offset time is: 2024-03-28 22:57:05