Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating water resistance in Box2D

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.

like image 607
Lukman Avatar asked Jan 15 '12 01:01

Lukman


2 Answers

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.

like image 30
Fred Avatar answered Oct 23 '22 19:10

Fred


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.

like image 97
Dan O Avatar answered Oct 23 '22 21:10

Dan O