How would I make a mouse controlled physics object in Box2D / AS3?

Posted by Marty Wallace on Stack Overflow See other posts from Stack Overflow or by Marty Wallace
Published on 2012-12-17T02:35:10Z Indexed on 2012/12/19 17:03 UTC
Read the original article Hit count: 332

Filed under:
|

I recently created this tennis game using my own basic physics: http://martywallace.com/sandbox/tennis/

enter image description here

Basically a tennis racquet sticks to your mouse and you can hit the tennis balls upward.

The physics aren't that great, and I want to make a more interesting version of this game with milestones and levels in Flash. I am planning to use Box2D because I have moderate experience with it.

I'm not sure how to go about creating the racquet - as far as I understand Box2D, the racquet needs a velocity to influence the velocities of the balls when you hit them (so that you can hit them harder or softer upward to keep them up). With that said, I'm assuming I can't just have a kinematic body that will have its position set to the mouse, because it won't affect the velocities of the balls as expected.

I've also thought about setting the velocity to the difference between the racquet position and the mouse each frame, but I am concerned that won't provide accurate positioning and am also thinking that the velocity could end up really large if you move the mouse quickly.

What is the correct way to have a physics object locked to the mouse but also to have its displacement in the last frame (from where it was to the mouse) affect the balls?

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about box2d