Backspin & topspin

Hi,

I'm trying to get a good backspin and topspin effect on a 2d circle (pretending to be a ball) in a top down view. At the moment, I am firing my circle body using a linearvelocity and then applying an impulse to the circle body in my tick method, which is a factor of the original velocity. The impulse factor and velocity of the circle reduce over time to slow it to a stop. The impulse is applied in the direction of the initial velocity for topspin and in the opposite direction for backspin.

Apply the initial velocity:
Code:
b2Vec2 initialVelocity;
initialVelocity.x = 5.0;
initialVelocity.y = 5.0;
circleBody->SetLinearVelocity(initialVelocity);


Then in my tick method:
Code:
tempImpulse.x = initialVelocity.x * 0.8; //change the figure depending on impulse force required
tempImpulse.y = initialVelocity.y * 0.8;
circleBody->ApplyLinearImpulse(-tempImpulse, circleBody->GetWorldCenter());
circleBody->SetLinearDamping(0.5); //change the figure depending on rate of velocity reduction required


.h
Code:
b2Vec2 tempImpulse;



The result I get for backspin is, with a small impulse factor the circle heads off at the set velocity, the impulses slow it down a bit and as the impulse figure reaches 0, before the velocity figure does, the circle then 'rolls' on a bit further. This seems accurate enough - the backspin is strong enough to slow the circle, but not to reverse it's direction or stop it. With a medium impulse, where the impulse and velocity reach 0 at about the same time, the circle speeds off and stops dead, suddenly, which looks a bit odd and with a high impulse the velocity reaches 0 before the impulse meaning the circle reverses direction. This looks about right, but the problem is, I think that if there is a large velocity and impulse the circle should be able to move quite a long way before it reverses direction, but no matter what the velocity the big impulse kicks in so the circle never moves very far before reversing direction. I could perhaps set the impulse to start after a specific period, but this doesn't seem right.

I was wondering if anyone had any thoughts on how I could get a decent back and top spin effect? I feel I'm perhaps halfway there with what I've done so far.

Thanks for any help.
I like these kinds of problems!

I'd use a vector to keep track of the spin's velocity, somewhat like the "tempImpulse" you're using. You might have missed that your impulse is constant, and I think a more realistic approach is to have the spin correct itself towards the ball's reversed velocity.

We've got two relevant values. The ball's Center of mass velocity and our imagined spin velocity which is the velocity of the ball's surface where it touches the ground.

Let's say, if the ball has no backspin and it rolls compliantly along with with the ball's velocity; the spin velocity would equal that of the ball's velocity negated:
SpinVelVector == -BallVelVector
This is a ball at peace, just rolling along and not worrying about changing direction too much.

If the spin velocity was equal to that of the ball, you'd have a violent backspin:
SpinVelVector == BallVelVector
and if you'd do this SpinVelVector == -2*BallVelVector you'd have a violent topspin!

So what forces do we get and how does the two velocities relate to each other?

The SpinVelVector should slowly correct itself to the peaceful state, the negative BallVelVector. A friction variable could be useful here, aswell as a time variable. Friction would normally be in the 0-1 range.
Something along the lines SpinVelVector += time * friction * (-BallVelVector-SpinVelVector);
So the friction will affect how fast the spin corrects itself, and the time is simply the same as the box2D time step.

Now the spin should affect the ball itself. It's pretty much the same thing, just in a different order. Time is handled automatically by the Box2D forces, so we won't need that variable here.
The spin force when SpinVelVector == -BallVelVector should be zero, so that gives us a starting point.
SpinForce = -friction * -BallVelVector - SpinVelocity;
Pretty much the same as before, with the difference of everything being negated once more. It's simply because the spin is excerpting a force in the opposite direction of it's velocity.

All what's left is to apply the force to the ball and you should have it spinning any direction you want! It will also make the ball start rolling if it got speed but no spin.

Spoiler:
Code:
// Declared in class
b2Vec2 SpinVelVector;
float friction;

// Definition and launch
ball->SetLinearVelocity( b2Vec2( 3,0 ) ); // Send the ball towards the right
SpinVelVector.Set( 0, 3 ); // Side spin!


// Update function
b2Vec2 BallVelVector = ball->GetLinearVelocity();
SpinVelVector += time * friction * (-BallVelVector-SpinVelVector); // You can multiply and add vectors without breaking them up to x and y.
b2Vec2 SpinForce = -friction * (-BallVelVector - SpinVelVector);

ball->ApplyForce( SpinForce, ball->GetWorldCenter() );
 


It's untested, but I'm pretty sure it should work as expected. Hope it's a good start anyway!
Hi,

Sorry for the slow response (I was stuck on something else meaning I couldn't even get my game to work so couldn't test any spin ideas), but thanks a lot Zoomulator for your help. I'm glad someone loves physicsy problems like this!

At the moment I am just focusing on getting the back and top spin working correctly. I have created a method, called spinUpdater, which is called from my tick method:

Code:
-(void)spinUpdater{
   b2Vec2 ballVector = cueballBody->GetLinearVelocity();
   spinVector += (1/100) * ballFriction * (-ballVector-spinVector); //ballFriction currently 0.5
   b2Vec2 spinForce = -ballFriction * (-ballVector - spinVector);
   ballBody->ApplyForce(spinVector, ballBody->GetWorldCenter());
}


and I fire off my ball in my touchesEnded method:

Code:
ballBody->SetLinearVelocity(usersSpeed); //usersSpeed is a user inputted b2Vec2
spinVector = cueballBody->GetLinearVelocity();
// spinFigure is a user inputted figure for amount of spin, between -2.5 (backspin) and +2.5 (topspin) currently
spinVector.x = spinVector.x * spinFigure;
spinVector.y = spinVector.y * spinFigure;


The spin seems to be working but nothing seems to be slowing it down / reducing it to zero. Not sure why at the moment. Any ideas?

Thanks for any help
The spin force will be zero when the spin velocity reaches balance with the ball velocity.
SpinForce == 0 when SpinVel == -BallVel

So the ball wont slow down any more once the spin has done its thing and adapted to the ball's momentum.

I think the solution might be as simple as putting a damping value to act as general friction of the ball against the table.
Easiest method of doing this is to use Box2Ds own damping option, that you already know of
Code:

b2BodyDef
::linearDamping = x; // Some value, usually 0.0-> 1.0
 


Or if you need more control, take the velocity vector of the ball, normalize it, negate it and multiply with what ever friction*ballSpeed.

Hope you get it workin'! Cheers

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