Apply noise to bounce

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
aidalgol
Prole
Posts: 19
Joined: Tue Feb 07, 2012 7:53 pm
Location: New Zealand
Contact:

Apply noise to bounce

Post by aidalgol »

I'm trying to make a pong game, and I'm not sure how to introduce randomness to the angular velocity of the ball after it bounces off a paddle (which I want to do to make it a little less predictable than the original Pong). Each paddle is a static body, and the ball is a dynamic body with restitution of 0.9 on the ball's fixture. My first thought is to add noise to the ball's linear velocity vector in the endContact callback. Can anyone offer other approaches?
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: Apply noise to bounce

Post by PGUp »

pong without physics.. its much better
-
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Apply noise to bounce

Post by zorg »

What PGUp means is that usually, pong games aren't made with realistic physics engines like Box2D (what love.physics uses), although if you wanted to do it that way, it's not wrong per se; that said, if i'm not mistaken, the setLinearVelocity method of a body wants the dx and dy parameters; you'd need to math out the conversion from the cartesian to the polar coordinate system, so you'll have a magnitude and an angle instead of per-axis deltas; then you could add a small random amount to the angle, which will not mess up the overall speed of the ball, then you'll need to convert back to cartesian so you can actually set the value.

Thankfully, the conversions between the two coordinate systems are simple:

Code: Select all

-- to polar
r = math.sqrt(x^2 + y^2)
phi = math.atan2(y, x)

-- modify the angle by some amount (you could also incrementally increase the magnitude so it gradually speeds up...
phi = phi + (love.math.random()-0.5)*math.pi -- angles are in radians, so this will modify the angle by a maximum of ±90 degrees.

-- turn it back to cartesian
dx = r * math.cos( phi )
dy = r * math.sin( phi )
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Apply noise to bounce

Post by ivan »

You can use preSolve to decide how your ball/paddle collision is resolved.
If you were to randomize the restitution and friction of the collision, you will get randomness that looks more or less plausible.
Make sure to use "Contact:setFriction" and "Contact:setRestitution" (friction affects the tangent and restitution affects the normal axis).
You have to change the friction/restitution of the actual contact, if you just changed it on the paddle it may not work as expected (since the ball's coefficients are important too).
aidalgol
Prole
Posts: 19
Joined: Tue Feb 07, 2012 7:53 pm
Location: New Zealand
Contact:

Re: Apply noise to bounce

Post by aidalgol »

Thanks to everyone for their advice. I have started out using Box2D instead of just bump or HC because I wanted to add silly physics once I had a ball and two paddles. before zorg's post, I had implemented almost exactly that, but with randomNormal() instead of random(). I tried Ivan's suggestion of adding randomness to the friction and restitution, and just doing that results in velocity all over the place, so doing it in the endContact() callback is definitely easiest.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests