Struggling to make an object always follow the mouse in box2dweb

RespectCS

New Member
I'm trying to learn how to use box2dweb and would like to have an object (a circle in this case) follow the mouse at all times, not just when dragging. The idea being it could be used for an air hockey type game where the circle is your paddle.I currently have the following code set up for a mouse listener:\[code\]canvas.addEventListener('mousemove', function(e){mouseX = (e.clientX - canvas.offsetLeft) / 100;mouseY = (e.clientY - canvas.offsetTop) / 100;if(!mouseJoint){ md = new b2MouseJointDef(); md.bodyA = world.GetGroundBody(); md.bodyB = ball; md.target.Set(mouseX, mouseY); md.maxForce = 300 * ball.GetMass(); md.collideConnected = true; mouseJoint = world.CreateJoint(md);}else{ mouseJoint.SetTarget(new b2Vec2(mouseX, mouseY));}});\[/code\]However when I run that the circle doesn't attach itself properly to the mouse, instead it swings around the point where the mouse cursor is and behaves more like a distance joint would if one of the objects were the mouse cursor.I can't tell what I'm doing wrong but I'm still new to this so any help would be appreciated.
 
Back
Top