Posted: 3rd Oct 2011 9:13
Hi all,

Newbie here so bear with me Coding in Tier 1.

Does anyone know how to increase the velocity of a sprite by the amount of force from another sprite? Basically I have a paddle (1st sprite) controlled by the player via the mouse that hits a ball (2nd sprite). I want the ball to move faster based on how hard the paddle hits it, so the faster the player moves the mouse the faster the ball will move once contact is made. I have this done to the point where the ball will bounce off the paddle but I can only get it to move by the same amount each time, it doesn't matter how "hard" it gets hit.

I'm using damping to slow it down after it's hit and I have mass values set as well as restitution for both sprites. I've tried SetSpritePhysicsVelocity as well as SetSpritePhysicsForce but I can only get the velocity command to move the ball. Otherwise I just push it around with the paddle.

Any advice at all would be greatly appreciated!

Thanks!
Posted: 3rd Oct 2011 19:14
I'm using damping to slow it down after it's hit

This could be your problem. Since you're using physics sprites anyway you don't need to calculate this kind of stuff. Turn the damping off or right down to nothing and you should see a huge improvement.

Just add forces to the paddle or control its position and angle and let the ball get hit by it, it should react as expected.

That would be my advice
Posted: 4th Oct 2011 0:02
Pretty sure you do NOT want to use SetSpritePhysicsVelocity, as this sets the velocity at a specific rate and will not work correctly with Forces or Impulses.

Here is the description in the help for it:

+ Code Snippet
Immediately sets the sprite's velocity to the given direction.
This is an abrupt change that might not look right during the
physics simulation, forces and impulses are the preferred approach
to influencing velocity.


Wish I could help more, but I am still trying to get my head wrapped around the physics commands too.

Thanks,
JHA
Posted: 4th Oct 2011 0:28
You can use the "SetSpritePhysicsVelocity" command as a 'one off' to get something moving and it will still react properly, just don't call it every loop!
Posted: 4th Oct 2011 3:19
He wants something like this effect:

http://www.emanueleferonato.com/2008/11/20/dragging-objects-with-box2d-flash/

Try to use:
CreateMouseJoint( iJointIndex, iSpriteIndex, x, y, maxForce )

This might work for you
Posted: 4th Oct 2011 7:27
Thanks everyone! Now it works for the most part except for two problems:

Using CreateMouseJoint and setting SetJointMouseTarget to GetPointerX and GetPointerY allows the sprite to follow the mouse without using SetSpritePosition fixed my physics problems, but the sprite lags behind the mouse pointer a bit. Is there anyway to make the sprite follow the mouse pointer 1:1?

And the other problem is that the velocity seems capped somehow. The harder the paddle impacts the ball the more velocity it gets but the paddle and ball cap at 600 units, which I see when I Print the values of GetSpritePhysicsVelocityX and Y for both sprites. Basically you can fling the mouse pointer across the screen but you aren't going to go faster than 600 units, no matter how fast you move it. Move the mouse slower though and you get the right velocity up to that 600 max. I'm guessing I am using SetSpritePhysicsForce incorrectly but I am still playing with it
Posted: 4th Oct 2011 11:06
"CreateMouseJoint(iJointIndex, iSpriteIndex, x, y, maxForce)"
What about playing with the "maxForce" value?