Simulating water resistance in Box2D
Asked Answered
G

2

9

I'm making a game where player drags a boat around using finger. I'm using Box2D for the physics aspects of the game. In particular, I'm using b2MouseJoint to attach the touch event to the boat to let the touch event drag the boat.

But there is one problem, that the way the boat moves while it is being dragged is not realistic; it can simply be dragged at any directions, where as in real world a boat can only move along its length and there will be water resistance that makes it hard to change the direction of the boat movement. Thus, I need a way to simulate water resistance to make the boat movement realistic.

Is it possible to simulate it in Box2D? Linear damping and angular damping simply make the boat hard to move, but I just want the boat to be easy to move in one direction and it should only resist changing of the direction.

P.S. I don't think I need to post my code for this question, but do let me know if you want to see the code excerpt.

Giselagiselbert answered 15/1, 2012 at 1:33 Comment(3)
Have you tried using the drag equation? The cross-sectional area term A will change depending on the orientation of the boat relative to its velocity, which should do what you want. And you can always estimate A with some cheap heuristic.Pax
@Rahul, thanks for the hint. I've added the drag equation into the code with some approximation techniques (like using the velocity vs boat angle to come up with reference area), I can see some water resistance simulated in the latest code but there seems to be some crazy movements when the boat collides with the world border. I'm trying to make sense of what's wrong before asking for further help here. Again, thanks for the hint.Giselagiselbert
A cheeky workaround could be to create lots of very small round bodies with small mass to fill the space around the boat. I haven't tested the viability of this in terms of physics or processing power (yet) which is why I'm not submitting it as a proper answer.Incarcerate
S
2

This is actually not a trivial thing to do, so a 'complete' answer is a little bit much to hope for. Here is a link to an example of a car in box2d. View that gentleman's demoreel (the third demo in the reel is the most relevant), and take a gander at his source code. His simulation is a little more advanced than what you need to accomplish, so if you can understand how it works you'll have no problem creating a simpler version.

There's a lot of code (understandably) in dougk16's box2d extension, so it may be tough to figure out exactly how his car works. So here's a simple starting point: You want to simulate a body that can move forwards and backwards, and also turn. So that's one force that extends forward or behind from your boat body's current facing, and one torque that will turn the body left or right. This should be all you need to get something that is pretty cool. Here's some pseudocode that would be a reasonable place to start:

if( needs_to_turn )
{
   // turn_direction will either be -1 (left) or 1 (right), boat_torque can be a constant to start with, but should probably be controlled by the user
   body.ApplyTorque(turn_direction * boat_torque);
}

if( needs_to_move )
{
   // facing_vector should be a vector pointing in the direction the boat is facing, the boat_force could be a constant, but again should be controlled by the user
   body.ApplyForce(facing_vector * boat_force);
}

I'll end my answer with another link, this time to a relevant tutorial. It's not as cool as dougk16's action script stuff, but it might be more useful to you, since it's in tutorial format.

Shontashoo answered 19/2, 2012 at 4:21 Comment(0)
H
1

I have one suggestion which might be trivial but require lots of particles and therefor perhaps some extra processing power. You could possibly create many many small circles with collision but have them not be drawn. So you would get the simulated effect of water, but not water. The size of the circles would determine how many of them you;d need to fill the screen and therefore also your performance. I know its a bit of a hack solution but it may be worth a shot. You would find a happy medium for the size of the circles that both allows for good performance and fluid like dynamics.

Oops, it appears someone already suggested this... Sorry.

Heisel answered 16/2, 2012 at 22:36 Comment(2)
Unfortunately, IMO, those particles will interfere with other physical objects that exist within the world so I'd rather not use this hackish technique. Thanks for the suggestion though.Giselagiselbert
@Heisel I ended up playing around with it, and it was a massive problem getting the balance right - either the circles were too big and impeded all movement, or the whole thing was too taxing on my processor. Mind you, I was trying it out on the JS port, which I think is normally a bit slower.Incarcerate

© 2022 - 2024 — McMap. All rights reserved.