3 Simple? questions

Hi,

My first question is not Box2D specific, but... a reply to this may be of benefit to others. So, I will ask it here in the hope of a solution.
1) I am creating a top-down game that has a ball at the main character. The playing field is made using Box2d for collisions and the movement of objects and contains no gravity (as it would be expected in the Z, though there is not need to worry about it). The ball that the player controls modifies the X/Y velocities for movement and uses linear damping to come to a rest. The problem is that I want the ball (a 3d model) to rotate in the direction of movement based on the velocity. The ball has no frictional surface (well, no surface at all) for which to gain any rotational impulses and thus must be manually added. My problem is finding a way to rotate the model along x/y based on the x/y velocity. Nothing I try works. Any help with a formula for this would be wonderful.

2) I have objects in the game world that act as bumpers. Ie. if the player makes contact with them, then the player is repelled. These are simple rectangular bodies and I am manually forcing the player away. Is there any way a body can be given a minimum restitution. ie. if I set a restitution on the bumper and allow box2d to handle the rebound, if the player keeps pushing into this bumper - eventually the ball runs out of steam and stops. What I would like to do is make sure it is repelled at the correct angle at a minimum amount of force set by myself. If there a simple way to do this?

3) What is the simplest way to find the amount of force that that a body collided into another with? Again, I am doing this manually, but know that Box2d has this information. For example, if the player hits another body hard enough, I want that body to crumble.

ps. sorry if I have not explained well enough (sneakily typing whilst at work 😉 )
Perhaps there were not that simple?

Question 2 and 3 would be a great help if anyone knows the answer?
Ok, think 1 and 3 are now sorted.

If anyone has any ideas regarding point 2, please?
I don't have good knowledge about Flash or Actionscipt. I am learning at the moment. It would be more appreciable if anyone answer to the question.
Question 2 and 3 would be a great help ?
Good Knowledge
I haven't ever written code to figure out the 3rd answer, but these are freebees because I happened to have already written the code and its mine to share.

Quote:
1) I am creating a top-down game that has a ball at the main character. The playing field is made using Box2d for collisions and the movement of objects and contains no gravity (as it would be expected in the Z, though there is not need to worry about it). The ball that the player controls modifies the X/Y velocities for movement and uses linear damping to come to a rest. The problem is that I want the ball (a 3d model) to rotate in the direction of movement based on the velocity. The ball has no frictional surface (well, no surface at all) for which to gain any rotational impulses and thus must be manually added. My problem is finding a way to rotate the model along x/y based on the x/y velocity. Nothing I try works. Any help with a formula for this would be wonderful.


What you should do is separate the rendering from the physics. In this case, it is a bit confusing, because a common and useful technique is to make the renderable game object be a userData of a b2Body in a b2World. The game object has a method that I declare in my program like this:

Code:
   virtual void render(TSFloat offsetX, TSFloat offsetY, TSFloat timeStep, GLuint color = 0xFFFFFFFF, bool isOffScreen = false, TSMatrix* matrix = NULL);


The matrix is used for things like stretching or skewing. I sometimes apply a full screen effect programmatically where objects need an animated transition. The C++ "virtual" keyword (only required in the base class in order to work properly!) causes any subclasses of your base game object class to do whatever it wishes.

In my program, the code that I would use for your facing forward algorithm would look like this:

Code:
   sprite->render(
              timeStep,
              body->GetPosition().x * box2D->pixelsPerMeter,
              body->GetPosition().y * box2D->pixelsPerMeter,
              b2Atan2(body->GetLinearVelocity().y, body->GetLinearVelocity().x), //body->GetAngle(),
              color,
              matrix);


Quote:
2) I have objects in the game world that act as bumpers. Ie. if the player makes contact with them, then the player is repelled. These are simple rectangular bodies and I am manually forcing the player away. Is there any way a body can be given a minimum restitution. ie. if I set a restitution on the bumper and allow box2d to handle the rebound, if the player keeps pushing into this bumper - eventually the ball runs out of steam and stops. What I would like to do is make sure it is repelled at the correct angle at a minimum amount of force set by myself. If there a simple way to do this?


I would make the bump impulse proportional to the player velocity into the direction of the
collision normal with the bumper, and just clamp its magnitude. Code:

Code:
b2Vec2 contactNormal;
if(getContactNormal(body, contactNormal)) {
    b2Vec2 bumpImpulse = body->GetLinearVelocity() * contactNormal;
    float32 bumpMagnitude = b2Clamp(bumpImpulse->Normalize(), 0.0, someBumpImpulseConstant);
    bumpImpulse *= bumpMagnitude;
}

bool TSBox2D::getContactNormal(b2Body* body, b2Vec2 &accumulatedNormals)
{
   accumulatedNormals.SetZero();
   bool foundAContact = false;
   
   for(b2ContactEdge* contactEdge = body->GetContactList(); contactEdge; contactEdge = contactEdge->next) {
      b2Contact* contact = contactEdge->contact;
      if (contact != NULL && contact->IsEnabled() && contact->IsTouching() && contact->GetManifold()->pointCount > 0) // && !contact->IsSensor()
      {
         foundAContact = true;
         
         // Get the b2WorldManifold if you want circle-circle contact normal
         b2Vec2 contactNormal = contact->GetManifold()->localNormal;
         
         if (contact->GetFixtureA()->GetBody() == body)
         {
            contactNormal *= -1.0;
         }
         
         accumulatedNormals += contactNormal;
      }
   }
   
   if (!foundAContact)
   {
      return false;
   }
   
   accumulatedNormals.Normalize();
   return true;
}

Original thread: https://box2d.org/forum/viewtopic.php?t=6843