BrickBreaker

Hi all, I decided to give myself a project to learn all of this. I settled on a quick BrickBreaker (Break Out) clone. I have the physics setup so that the world has zero gravity and the ball doesn't lose any speed from contact with the walls. I even made a simple box at the bottom for the paddle.

When it came time to make the paddle slide left and right I was unsure if I should use the MouseJoint or just move the box to the the current mouse location.

I'm deconstructing the TestBed.fla to lay everything out. I wasn't sure how to arbitrarily just move the x coords of the paddle so I decided to rewrite the MouseDrag function to ONLY attach itself to the the paddle I made. But I don't know how to rewrite this line...

var body:b2Body = GetBodyAtMouse();

to instead reference the "body" of my paddle which was declared earlier as...

barShape = new b2BoxDef();
barBodyDef = new b2BodyDef();
barBodyDef.AddShape(barShape);
barShape.density = 1;
barShape.friction = 1;
barShape.restitution = 0;

barShape.extents.Set((50) / m_physScale, (10) / m_physScale);
barBodyDef.position.Set((640 / 2) / m_physScale, (480 - 10) / m_physScale);
m_world.CreateBody(barBodyDef);

Let me know if I'm making no sense or am not thinking of this the right way. Also if anyone thinks I should just move the paddle to the mouse x position to avoid the lag of using a joint. Thanks! rob
Hi Rob,

You'll need to keep a reference to the paddle and use that instead of GetBodyAtMouse();

I.e
var paddle:b2Body = m_world.CreateBody(barBodyDef);

You could consider applying forces or setting the velocity of the paddle manually to keep it on the mouse, maybe use a prismatic joint to prevent movement on the y axis if need be, and set barBodyDef.preventRotation = true; to stop the paddle from rotating. :P

I'd also keep the velocity and direction of the ball in check, to make sure it doesn't slow down or speed up too much, or hit an angle and get stuck bouncing horizontally from wall to wall.

All the best,
-Matt
Hi skatehead, thanks for the quick reply, in regards to this...
Quote:
You'll need to keep a reference to the paddle and use that instead of GetBodyAtMouse();

I.e
var paddle:b2Body = m_world.CreateBody(barBodyDef);


I currently keep references to the bodydef and shapedef instances of the paddle, I completely spaced on being able to grab a reference to the body from the CreateBody return value. In your opinion, is there any reason to keep refrences to the other two?

All the rest are great ideas too. Thanks, I'll post what I've done when it's less bring. rob

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