Physics Assistance for a Noob

Hi All,

         I’ve just started playing around with the physics library and want to knock up something really basic… a bat on the left hand side of the screen with a ball being pitched from the right hand side. When the user touches the screen it will swing the bat and hit the ball if it collides.

Can anyway give me a few basic tips on how to do this?

How do I make the bat apply force to the ball to give it a realistic hit trajectory?

Can I use a sprite for an animation of the bat, if so how do I make this interact with the ball, do I just use hidden rectangles underneath the image?

Any advice or code assistance will be greatly appreciated!

Kind Regards,

Krivvenz.

Still interested in how to do this if anyone knows!

Should this be a top-down view of the player/bat? Or side view?

Brent

Hi Brent,

             It would be a side view. I played around trying to add physical shapes to the sprite but it didn’t seem to work very well. Just wondering what someone with a bit more knowledge would recommend just to get me started :).

Here’s what I would do

  • Use physics for the ball only, no need for physics on the bat

  • Launch the ball by using object:applyLinearImpulse()

  • When the user taps the bat, play the animation

  • For the animation, use sprites or transitions, depending on how you want to move the bat (if it’s just a rotational movement, you may be able to get away with a simple transition.to() )

  • At the right point in the animation (use timer.performWithDelay() ), check the position of the ball

  • If the ball is at the right position to be struck by the bat, use applyLinearImpulse to launch it the other way

  • Otherwise, do nothing and let the ball drop to the floor

Thanks _memo! I’ve made a start with that but hit a wall already lol.

Is there a way to find out if an object is at a certain X or Y position?

So for example I could do sweetspotx = 70, sweetspoty = 100

if ball.x = sweetspotx and ball.y = sweetspoty then apply linear impulse. Is there a way to cover if the ball is touching the sweetspot rather than just the balls x position if you get what I mean? Like if any of the ball object touches those two coordinates apply force?

Many Thanks.

You could probably just check the ball’s left and right coordinates and determine if they’re in the sweet spot, i.e. based on the radius of the ball, use its center and then check left and right of its center location. Granted, that’s not a perfect representation of whether the ball is in a specific “round region” because the ball is round (not square), but it might still suffice for your simulation.

Brent

Here’s a snippet from my utils that could be handy:

-- if 2 params are supplied, it is assumed they are objects with .x and .y properties -- if 4 params are supplied, it is assumed they are x0, y0, x1, y1 local getDistance = function(a, b, c, d) if(a and b and c and d) then return sqrt((c - a)^2 + (d - b)^2) else return sqrt((b.x - a.x)^2 + (b.y - a.y)^2) end end

You can use it like this:

if(getDistance(ball.x, ball.y, sweetspotx, sweetspoty) < 100) then

  – ball is in the sweet spot, so do something

end

Of course, change the 100 to whatever size (radius) you want your sweet spot to be.

Still interested in how to do this if anyone knows!

Should this be a top-down view of the player/bat? Or side view?

Brent

Hi Brent,

             It would be a side view. I played around trying to add physical shapes to the sprite but it didn’t seem to work very well. Just wondering what someone with a bit more knowledge would recommend just to get me started :).

Here’s what I would do

  • Use physics for the ball only, no need for physics on the bat

  • Launch the ball by using object:applyLinearImpulse()

  • When the user taps the bat, play the animation

  • For the animation, use sprites or transitions, depending on how you want to move the bat (if it’s just a rotational movement, you may be able to get away with a simple transition.to() )

  • At the right point in the animation (use timer.performWithDelay() ), check the position of the ball

  • If the ball is at the right position to be struck by the bat, use applyLinearImpulse to launch it the other way

  • Otherwise, do nothing and let the ball drop to the floor

Thanks _memo! I’ve made a start with that but hit a wall already lol.

Is there a way to find out if an object is at a certain X or Y position?

So for example I could do sweetspotx = 70, sweetspoty = 100

if ball.x = sweetspotx and ball.y = sweetspoty then apply linear impulse. Is there a way to cover if the ball is touching the sweetspot rather than just the balls x position if you get what I mean? Like if any of the ball object touches those two coordinates apply force?

Many Thanks.

You could probably just check the ball’s left and right coordinates and determine if they’re in the sweet spot, i.e. based on the radius of the ball, use its center and then check left and right of its center location. Granted, that’s not a perfect representation of whether the ball is in a specific “round region” because the ball is round (not square), but it might still suffice for your simulation.

Brent

Here’s a snippet from my utils that could be handy:

-- if 2 params are supplied, it is assumed they are objects with .x and .y properties -- if 4 params are supplied, it is assumed they are x0, y0, x1, y1 local getDistance = function(a, b, c, d) if(a and b and c and d) then return sqrt((c - a)^2 + (d - b)^2) else return sqrt((b.x - a.x)^2 + (b.y - a.y)^2) end end

You can use it like this:

if(getDistance(ball.x, ball.y, sweetspotx, sweetspoty) < 100) then

  – ball is in the sweet spot, so do something

end

Of course, change the 100 to whatever size (radius) you want your sweet spot to be.