Water Buoyancy Example

Posted by iNSERT.CODE, Posted on October 11, 2011

4 votes

I am currently working on a game (my first in fact) which unexpectedly has led me down this long, but productive, detour of a road in trying to implement a buoyancy effect that makes physics objects float realistically when dropped into a body of liquid.

A search on YouTube and Google showed some interesting implementations on various platforms (Flash and Java), but I couldn't find anything related to Corona SDK or Lua that I could dissect, learn from and implement myself. I will add here, that I am new to programming in general (about 6 weeks in), and porting from another scripting language, whilst trying to learn the target scripting language is unfortunately beyond my scope at this point.

So, I decided to have a go at creating the buoyancy myself. After all, this best way to learn is by diving in (no pun intended) and doing it yourself.

The maths may not be perfect, but I think that the effect looks good and, more importantly, pretty realistic.

Demo Video Here: http://youtu.be/VI65CXnsG9Q

In this example, I've added a 'Settings' section that allows simple changing of:

- Number Of Boxes

- Size Of Boxes

- Liquid Density

The liquid density allows the user to control how viscous the liquid is. The default setting of 1.0 simulates water, and the higher the number, the more viscous the fluid becomes. A setting of 2.5 here simulates something similar to oil etc.

In a future update, I will be modularising the code to make it more flexible, and support various display objects etc.

You can download the .zip (only 6KB) from here:

http://insertcode.co.uk/2011/10/water-buoyancy-in-corona-sdk/

I'd like to thank Corona SDK forum members 'horacebury' and 'JayantV' for responding to my questions and offering their input and assistance.


Replies

wintermuteai1's picture
wintermuteai1
User offline. Last seen 3 weeks 1 day ago. Offline
Joined: 10 May 2011

Very cool and useful! Good Job man

AlenB's picture
AlenB
User offline. Last seen 4 days 4 hours ago. Offline
Joined: 29 Sep 2010

Thank you very much for sharing ..
Works nice ...

BR,
Alen

nicholasclayg's picture
nicholasclayg
User is online Online
Joined: 16 May 2011

That's great man. How "new" are you to programming? I started brand new in June 2011 (nothing ever before of any language) and I feel your pain.

It's like you get this tidal wave of ideas, then you fire up corona sdk try some things out and realize your "simple concept" requires some COMPLEX logic. Then you search the forums, online, youtube and then you do what you just did "Is there an example of this in another language, maybe action script or flash or java?"

That in itself is a pain in the ass, so for your effort that was very very cool and nicely done.

I first started off on Android as I owned a droid X, and I wanted to make games for android. Then I learned I would have to know Java....then I wanted to make games for iPhone....and we all know that leads to Objective C....

THEN THE MOTHERBEEPING SUN CAME OUT AND SHINED A LIGHT ON MOTHERBEEPING CORONA TO THE S TO THE D TO THE K.

Nice effort, good to see some new people new to coding whipping up useful stuff. I myself am working on a library of "codesets" (that's what I call them anyway) that I will release a while (like Jon Beebe) after my first game is finished :)

ng

mightE
User offline. Last seen 2 weeks 2 days ago. Offline
Joined: 27 Jul 2011

^ hmn. yes.
calm down

nicholasclayg's picture
nicholasclayg
User is online Online
Joined: 16 May 2011

@anddrewscott

Expresso makes me this way, I have learned to accept my fate, and therefore I cannot execute your calm down request at this time.

I'm always excited! Lol.

iNSERT.CODE's picture
iNSERT.CODE
User offline. Last seen 1 day 23 hours ago. Offline
Joined: 7 Jul 2011

Glad you all think it's useful - I'm pretty happy with the effect.

@nicholasclayg

I'm about 6-7 weeks into actively coding with Corona/Lua.

I won't go into my full full experience (or lack thereof) in this thread, but I've posted a little introduction about myself and my short journey this far with Corona. So, if you or anyone else wants to take a look and say 'hi', then head here:

http://insertcode.co.uk/2011/10/my-short-app-development-journey-so-far-an-intro/

MichaelAssadi
User offline. Last seen 1 day 20 hours ago. Offline
Joined: 20 Jan 2011

That was so awesome!!!! Ive been looking for a way to do this!? i was wondering if i could use this in my app, it is really useful. Thanks a lot for this wonderful work.

iNSERT.CODE's picture
iNSERT.CODE
User offline. Last seen 1 day 23 hours ago. Offline
Joined: 7 Jul 2011

@MichaelAssadi

Glad you like it. Feel free to use it without restriction as I've made it MIT Licenced. If you do use it in a project, I'd very much like to see how people are using it and what they can create with it. If you send me a link to your App or web site I'll check it out and maybe blog/publicise it for you with a link back to your web site or AppStore link.

Same goes for everyone. Enjoy.

MichaelAssadi
User offline. Last seen 1 day 20 hours ago. Offline
Joined: 20 Jan 2011

The example is really good, wow only 6-7 weeks?! amazing!

Thanks a lot!

xxxfanta's picture
xxxfanta
User offline. Last seen 6 days 40 min ago. Offline
Joined: 25 Dec 2010

Wow - that was an awesome snippet and I would really love to use it! :)
And the best of it - it's quiet simple and easy to implement!

I was playing with your code and changed line 184 to
local buoyancyForce = (box.mass * gy) * ((box.y-(liquid.y-liquid.height/2))/(liquid.height/1.2))

Why? Because I wanted the Boxes to have a bigger speed when they are deeper in the water (change the 1.2 to 4 and you'll see some torpedos jumping out of the water :D )

Thanks for your effort and your great code!

Keep up the good work! :)

Best regards,
Chris