Make bounce more random?

I’m working on a game similar to pong ( kinda) , except it is the ball is bouncing around in a circle and you use parts of the circle as a paddle. The game is almost done , but the main issue is the bounce is predictable  oW2JqdH.png

pUXsFEB.png

Short Video:

http://i.gyazo.com/b2496ec0277d19bcc8822e9d08183ec6.mp4

The movement is all physics based (set liner velocity mainly). 

My Attempt at randomizing the bounce(Executed as soon as bounce is over)

math.randomseed(os.time()) xForce = xForce + math.random( 3,5) yForce = yForce + math.random( 3,5) ball:setLinearVelocity( xForce, yForce )

But this doesn’t really work.

Any ideas?

Here is a step-by-step that should help.

  1. Post collision (1 or  2 frames later): Get linear velocity.

  2. Calculate angle and magnitude of that velocity.

  3. Add or subtract random angle from calculated angle.

  4. Create new vector based on new angle.

  5. Normalize resultant vector.

  6. Scale normalized vector by original magnitude.

  7. Apply scaled vector to object as new velocity.

Solution 2,

  1. Give the ball and surfaces friction.

  2. After every collision, set a random rotational velocity on the ball. 

Experiment with spin values and friction values for best response.

This spin will affect the next collision by imparting a deviation in the bounce.

Note: You may lose velocity this way though.  So you might need to enforce a minimum velocity rule when  not in collision code.

Could you please give me a code sample of that? I’m still having some trouble

  1. Got get my math2d library (for this example; or use one you like from elsewhere later) :https://github.com/roaminggamer/RG_FreeStuff/blob/master/SSK/RGMath2D/ssk/RGMath2D.lua

  2. Try this code (untested may have typos):

    local math2d = require “RGMath2D” local function tweakDir( obj ) – 1. local vx,vy = obj:getLinearVelocity() – 2. local angle = math2d.vector2Angle( vx, vy ) local len = math2d.length( vx, vy ) – 3. if( math.random(1,2) == 1) then angle = angle + math.random( 5, 15 ) else angle = angle - math.random( 5, 15 ) end – 4. local vec = math2d.angle2Vector( angle, true ) – 5. vec = math2d.normalize(vec) – not really needed (already done by angle2Vector) – 6. (Seems I couldn’t count before…) vec = math2d.scale( vec, len ) – 7. obj:setLinearVelocity( vec.x, vec.y ) end

Now use it like this:

local obj = display.newCircle( ... ) local function onCollision( self, event ) -- or your own veriant if(event.phase == "ended") then timer.performWithDelay(1, function() tweakDir( self ) end ) end return true end obj.collision = onCollision obj:addEventListener( "collision" )

Here is a step-by-step that should help.

  1. Post collision (1 or  2 frames later): Get linear velocity.

  2. Calculate angle and magnitude of that velocity.

  3. Add or subtract random angle from calculated angle.

  4. Create new vector based on new angle.

  5. Normalize resultant vector.

  6. Scale normalized vector by original magnitude.

  7. Apply scaled vector to object as new velocity.

Solution 2,

  1. Give the ball and surfaces friction.

  2. After every collision, set a random rotational velocity on the ball. 

Experiment with spin values and friction values for best response.

This spin will affect the next collision by imparting a deviation in the bounce.

Note: You may lose velocity this way though.  So you might need to enforce a minimum velocity rule when  not in collision code.

Could you please give me a code sample of that? I’m still having some trouble

  1. Got get my math2d library (for this example; or use one you like from elsewhere later) :https://github.com/roaminggamer/RG_FreeStuff/blob/master/SSK/RGMath2D/ssk/RGMath2D.lua

  2. Try this code (untested may have typos):

    local math2d = require “RGMath2D” local function tweakDir( obj ) – 1. local vx,vy = obj:getLinearVelocity() – 2. local angle = math2d.vector2Angle( vx, vy ) local len = math2d.length( vx, vy ) – 3. if( math.random(1,2) == 1) then angle = angle + math.random( 5, 15 ) else angle = angle - math.random( 5, 15 ) end – 4. local vec = math2d.angle2Vector( angle, true ) – 5. vec = math2d.normalize(vec) – not really needed (already done by angle2Vector) – 6. (Seems I couldn’t count before…) vec = math2d.scale( vec, len ) – 7. obj:setLinearVelocity( vec.x, vec.y ) end

Now use it like this:

local obj = display.newCircle( ... ) local function onCollision( self, event ) -- or your own veriant if(event.phase == "ended") then timer.performWithDelay(1, function() tweakDir( self ) end ) end return true end obj.collision = onCollision obj:addEventListener( "collision" )