How would I get this Box2D "Water" example into Corona?

I am thinking about making a game that includes people on a raft and part of the game will be keeping it afloat, so I was looking around for some water code. Here is the best one I found (If its not on it already, hit “Next Demo” until it is the “Animated Buoyancy and Waves” demo) : http://www.sideroller.com/wck/?f=6

I did see the example by Corona Geek (http://bit.ly/13Aqw2y) but it doesn’t look like it has weight distribution which is what I need, and the waves from the Box2D demo would be nice. :slight_smile:

What I need help on is getting the code from the Box2D example into Corona. I really don’t know much about Box2D so I don’t know how I would do this, or if it is even possible. It looks like it might lag in Corona :frowning: Any help will be appreciated!

Hi @AppdojoStudios,

One of the Corona ambassadors, Matt Webster (a.k.a. “horacebury”) put together a cool buoyancy demo in Corona. I’ll e-mail him and ask him to respond to this thread, hopefully with the sample code. I don’t know if the “insert:code” demo is the same or not… and I’m not sure that it does the weight distribution you need.

Also, doing “waves” is going to be very tricky… you’re setting yourself up for a considerable challenge if you need to implement rolling waves + buoyancy.

Best regards,

Brent

Funnily enough, as @Brent was replying, I’ve been working on a buoyancy sample. I’ve just put it on the code exchange…

http://developer.coronalabs.com/code/box2d-water-buoyancy

@Brent Thanks for emailing horacebury to get the sample and the waves would just be a fun thing to add, not necessary at all.

@Horacebury That sample looks perfect except when I make somewhat small rectangles they just bounce around on top of the water. It does look like it’s leveling them out though so that part is awesome.

It’s ok, I really just happened to read your post at the same time.

The buoyancy effect is created by calling applyForce lots of times at equally spaced locations. For small objects the amount of force will be considerable, which is why you can’t really let the user create their own objects. I added the box creation function to demonstrate this.

The sample I create is just that: a sample. It can definitely be changed and certainly needs to be optimised.

One thing which pops into my head is that the sensors at the top 1 or 2 rows could be given a much lower amount of force to apply, meaning that they won’t be battering objects at the top “out of the water.” I had also thought that another layer of sensors could be placed on top which can actually apply a downward force to get the same effect.

Ultimately, you need to play around with the force, frequency it is applied and the number of sensors to figure out what is right for your game.

In the past, I have tried to convert the code from this page, but failed somewhat as the results did not seem to match with his:

http://www.iforce2d.net/b2dtut/buoyancy

Thank you so much for explaining the logic, I totally get how to fix the problem now. Thanks everyone for the help!!!

Hi @AppdojoStudios,

One of the Corona ambassadors, Matt Webster (a.k.a. “horacebury”) put together a cool buoyancy demo in Corona. I’ll e-mail him and ask him to respond to this thread, hopefully with the sample code. I don’t know if the “insert:code” demo is the same or not… and I’m not sure that it does the weight distribution you need.

Also, doing “waves” is going to be very tricky… you’re setting yourself up for a considerable challenge if you need to implement rolling waves + buoyancy.

Best regards,

Brent

Funnily enough, as @Brent was replying, I’ve been working on a buoyancy sample. I’ve just put it on the code exchange…

http://developer.coronalabs.com/code/box2d-water-buoyancy

@Brent Thanks for emailing horacebury to get the sample and the waves would just be a fun thing to add, not necessary at all.

@Horacebury That sample looks perfect except when I make somewhat small rectangles they just bounce around on top of the water. It does look like it’s leveling them out though so that part is awesome.

It’s ok, I really just happened to read your post at the same time.

The buoyancy effect is created by calling applyForce lots of times at equally spaced locations. For small objects the amount of force will be considerable, which is why you can’t really let the user create their own objects. I added the box creation function to demonstrate this.

The sample I create is just that: a sample. It can definitely be changed and certainly needs to be optimised.

One thing which pops into my head is that the sensors at the top 1 or 2 rows could be given a much lower amount of force to apply, meaning that they won’t be battering objects at the top “out of the water.” I had also thought that another layer of sensors could be placed on top which can actually apply a downward force to get the same effect.

Ultimately, you need to play around with the force, frequency it is applied and the number of sensors to figure out what is right for your game.

In the past, I have tried to convert the code from this page, but failed somewhat as the results did not seem to match with his:

http://www.iforce2d.net/b2dtut/buoyancy

Thank you so much for explaining the logic, I totally get how to fix the problem now. Thanks everyone for the help!!!