Physics restitution question...

I’m not sure if I can clarify this any more, but I’ll try.  This is a breakdown of the code:

Some Helper Variables and Setting Up Physics

local cx = display.contentCenterX local cy = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local left = cx - fullw/2 local right = cx + fullw/2 local top = cy - fullh/2 local bottom = cy + fullh/2 local physics = require "physics" physics.start() physics.setGravity(0,10) physics.setDrawMode("hybrid")

Locals Specific To Example Positions and Sizes

local size = 80 -- ALL OBJECTS ARE 80 x 80 local radius = 38 -- Radius of ball a little less than half of 80 so the shape fits visible part better -- This actually throws off a later calculation a little accuracy wise. local y0 = cy - 200 -- Position of top marker bottom local y1 = cy + 200 -- Position of block top local tween = (y1 - y0) - size -- distance between those two spaces, less the size of the ball local targetY = y0 + tween/2 + size/2 -- Where target (marker 2) should be placed. i.e. Marks where -- I want ball to bounce to (based on your post)

A function to create markers, block, ball, and set up bounce/restitution

local function createParts( x, text, restitution ) restitution = restitution or 1 local dropMarker = display.newImageRect( "rg256.png", size, size ) dropMarker.x = x dropMarker.y = y0 dropMarker:setFillColor( 1, 0, 0, 0.25 ) local targetMarker = display.newImageRect( "rg256.png", size, size ) targetMarker.x = x targetMarker.y = targetY targetMarker:setFillColor( 0, 1, 0, 0.25 ) local block = display.newImageRect( "corona.png", size, size ) block.x = x block.y = y1 physics.addBody( block, "kinematic", { bounce = 0 } ) local label = display.newText( text, block.x, block.y + 100, nil, 20) local ball = display.newImageRect( "rg256.png", size, size ) ball.x = x ball.y = dropMarker.y physics.addBody( ball, "dynamic", { bounce = restitution, radius = radius } ) return ball, block end

Two Examples That: Do not do what you want. (i.e. naive approaches)

-- Wrong local ball, block = createParts( cx - 225, "Wrong 1" ) local ball, block = createParts( cx - 75, "Wrong 2", 0.5 )

One Example That: You’d think would work (it doesn’t).

-- Close local restitution = 0.5 local ball, block = createParts( cx + 75, "Close: " .. restitution, restitution ) ball.first = true function block.collision( self, event ) local other = event.other if( event.phase == "began" ) then if( other.first ) then other.first = false else event.contact.bounce = 1 end end return false end; block:addEventListener("collision")

The collision listeners does this:

(Nearly) Perfect Solution

-- Perfect local restitution = 0.5 + radius/tween local ball, block = createParts( cx + 225, "Perfect: " .. restitution, restitution ) ball.first = true function block.collision( self, event ) local other = event.other if( event.phase == "began" ) then if( other.first ) then other.first = false else event.contact.bounce = 1 end end return false end; block:addEventListener("collision")

This is almost exactly the same as the ‘close’ example, but I calculated a value for bounce/restitution.  

All physics objects are treated like points for the purpose of calculations.  That means the true position and travel distance of the ball is based on it’s center, not the bottom.  Thus, I have to adjust the restitution as follows:

50% restitution + radius / travel-distance

tween is the travel distance.

i.e. need to make it a little more bouncy because it isn’t falling as far as I need it for it to get enough energy.

This calculation is simplified by the fact that the target bounce-to  position is directly in the center (of the drop position and the block). 

It would be a a little harder if I allowed for arbitrary bounce target positioning.

@roaminggamer
Thanks, I will study the code more thoroughly until I can understand.

I’m a bit late to the party, but I would vouch for the hacky method of solving this by simply resetting the ball’s linear velocity after every collision. I just wrote a rough example of the method to jake1987.jj’s post “Rebound length”.

So, as @davebollinger already said, after each collision, you’d simply set the linear Y velocity to the desired value. This method will, however, require setting up some safety checks so that this doesn’t occur when hitting the platforms other sides, etc.

 

@roaminggamer

Now I understand your example a little better. The difference is in “scope” because your “scope” is superior to mine. Basically you did in 100 lines what I have done in 500. To adjust all my code in your way would be a work of almost 3 weeks. However, there is a difference, you use an image to which you assign a radius, I use a circle whose radius is the edge of the image that fills it.  You also use a pre-arranged distance (y1, y0) where you can get the values and assign them to the variable “restitution”, but in a free scenario, where the ball can bounce wherever it wants, I do not think it will adjust that calculation. I can not get to understand how to make the “restitution” value adapt to my “scope”. However, thank you very much because you have opened my mind to other horizons.

I will continue studying to see what I can do.

I made another test bench for exploring the ‘solving for restitution’ issue. 
 
I am not satisfied with the result, but here it is anyways:
 
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/restitution2.zip
 
I agree, that hacking may be better. 
 
Note :  Just because it is a hack does not mean it is bad.  Sometimes the simplest and/or most direct solution is the best.

@dodi_games - You should forget about the mechanic for a moment and describe how it is used in your game. How will it affect game play and interaction.

Knowing this, we may be able to give you a better choice.

Often times, folks encounter a problem in their game. They then come up with a solution, but don’t know how to implement it. Then they ask about implementing the solution, never telling what the original problem was.

i.e. You’re focused on using ‘restitution’ (or some other method) to limit bounce distance, but we don’t know why or how this is used in your game, nor what problem it is solving.

Maybe try giving us the ‘elevator’ pitch description of your game and remember to tell us why this bounce limit is important.

@roaminggamer

you are right, but this time I do not have a concrete game.  I’m goin to explain what I have.

-I have a menu and 10 levels

-The levels consist of 4 “screen borders”, a “ball” and objects to collide, which in this case are “squares”.

-In particular there is no established game mechanics, I am looking for one, that’s why I have 10 levels.

-Level one has a square, level two has two, and so on.

-To move to the next level I have decided that one of the squares is the goal, so when the ball touches that square its movement stops and I go to the next level

-to move the ball I have two buttons, left and right

-and I want the ball to have a constant rebound movement, so when I move it to the right or left I can bounce with the edges of the screen or squares…boring right?

I want to control the distance of the bounce because if a square is more up than another when colliding with the one below, a bounce too high is triggered as shown in the photo above.

and…

that prevents me from playing with my objects and being able to think in wich game mechanics are fun to be able to develop a game, a good idea.

I started a game and discovered that I am too newbie to do it in so little time of learning, so I decided to use that project to play with it a little, discover and learn more about Corona SDK, with the hope that I can think of a good idea and be able to upload my first game, free, ads probably, to Google Play.

In short, I look for a constant rebound effect in which I can control the distance of the rebound, which allows me to have more control of the ball and I can think of a good idea to develop a game.


@all

thanks guys, all of you are great.