Rebound length

I had a few minutes to kill, so I just decided to roughly code what I meant into the code that you posted.

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) --\> Physicslocal physics = require("physics") physics.start() --physics.setGravity( 0, 9.81) physics.setDrawMode( "hybrid" ) local reboundValue = -190 local ball local block = {} local function onLocalCollision( self, event ) if ( event.phase == "ended" ) then print( self.category .. ": collision ended with " .. event.other.category ) local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( vx, reboundValue ) end end local background = display.newRect( 160, 250, 500, 500 ) background:setFillColor( 0.7, 0.5, 0.5 ) ball = display.newCircle(160,200, 10) ball:setFillColor( 1, 0, 0 ) physics.addBody(ball, "dynamic", { density=1, friction=0.0, bounce=1, radius=10 }) ball.collision = onLocalCollision ball:addEventListener( "collision" ) ball.category = "ball" for i = 0, 2 do block[#block+1] = display.newRect( 80+i\*80, 240+i\*40, 20, 20 ) block[#block]:setFillColor( 0, 1, 0 ) physics.addBody( block[#block], "static" ) block[#block].collision = onLocalCollision block[#block]:addEventListener( "collision" ) block[#block].category = "block" end function background:touch(event) if(event.phase=="moved")then ball.x = event.x -- use physics to accomplish this. elseif(event.phase=="ended")then local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( 0, vy ) end end background:addEventListener( "touch" )

I didn’t touch the x movement, or implement the safety checks for resetting the bounce, but this should give you a good idea of what I was talking about above.

As already stated, I’d recommend using physics to move the ball on the x-axis. You could, for instance, see where the player touches the background. If the player touches to the left of the ball, then you apply linear velocity to the left and vice versa.

@XeduR @Spyric

but what happen with vx value?

if I use 

  local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( vx, reboundValue )

I get the desired bounce, but the ball keeps bouncing at vx = 0 and it’s supposed to flow with a natural collision movement.

:wacko: 

In the example above, I added the “ball:setLinearVelocity( 0, vy )” bit to try to reduce the bugged movement that originates from manually setting the ball’s x value.

Add the following print statement into your code:
 

local vx, vy = ball:getLinearVelocity() print(vx) ball:setLinearVelocity( vx, reboundValue )

If the ball doesn’t have any movement on the x axis, then it will remain as zero when using that. As long as the ball is moving left or right before the collision occurs, then getLinearVelocity gives you a linear X velocity that is not zero. This means that if you set linear X velocity to be vx, then it will retain its previous linear X velocity.
 

My main issue is if I implement this method is when the ball hit a square corner.  Its suppose to bounce with more force to a natural angle direction and in this case It’s bounce up and down. I’m using buttons not a background listener.

Can you provide us with a brief code sample of that happening?

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.

Wrong thread… post moved.

It’s all a matter of time. Prepare a mini-project to show what I explain could take 2 hours to adapt the whole scenario. It is a practice project with 10 levels, which at some point I might consider uploading it to the store for free download. I do not have the experience and speed of you.

I understand that it can be time consuming to write a sample. The reason as to why I asked was because the problem that you described, as I understand it, does not exist in the code sample that I provided.

Edit: To demonstrate my point, if you insert the following line of code into the sample above, then the ball will hit a corner and it will jump off accordingly. It will not have a linear X velocity of zero.

 

ball.x = ball.x+12

Place that anywhere after the ball has been created.

@XeduR @Spyric

Thank you very much for your advice and very useful code.

I think I can manage to get by now but I’m not sure. I’ll try and let you know.

@roaminggamer

thanks also for your sample code, it is very useful

One question, how do I know how many pixels equals “reboundValue = -190” is there a formula for this?

Still here.

I put the bounce checks only at the top. And I added the controls to move the ball’s “.x” with physics.

I’m having problems though:

1.If the ball hits the side of a block instead of standing still, it vibrates.

2.compared to before the fluidity has improved but I think we can do more.

Here is the new code:

 local horizontalSpeed = 400 local reboundValue = -190 local ball local block = {} local background = display.newRect( 160, 250, 500, 500 ) background:setFillColor( 0.7, 0.5, 0.5 ) local function onLocalCollision( self, event ) if ( event.phase == "ended" ) then --print( self.category .. ": collision ended with " .. event.other.category ) if(event.other.category == "ball")then if((ball.y+ball.height\*0.5) \< (self.y+self.height\*0.5))then local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( vx, reboundValue ) end end end end ball = display.newCircle(160, 200, 10) ball:setFillColor( 1, 0, 0 ) ball.category = "ball" physics.addBody(ball, "dynamic", { density=1, friction=0.0, bounce=1, radius=10 }) ball.collision = onLocalCollision ball:addEventListener( "collision" ) for i = 0, 2 do block[#block+1] = display.newRect( 80+i\*80, 240+i\*40, 20, 20 ) block[#block]:setFillColor( 0, 1, 0 ) if(i==0)then block[#block].height = 200 end physics.addBody( block[#block], "static" ) block[#block].collision = onLocalCollision block[#block]:addEventListener( "collision" ) block[#block].category = "block" end block[#block+1] = display.newRect( 160, 380, 300, 20 ) block[#block]:setFillColor( 0, 1, 0 ) physics.addBody( block[#block], "static" ) block[#block].collision = onLocalCollision block[#block]:addEventListener( "collision" ) block[#block].category = "block" local tmp = display.newRect( 160, 100, 20, 0 ) tmp.anchorY=1 tmp.y = block[2].y-(block[2].height\*0.5) local nowX, nowY = ball.x, ball.y function background:touch(event) if(event.phase=="began")then nowX, nowY = event.x, event.y elseif(event.phase=="moved")then nowX, nowY = event.x, event.y --ball.x = event.x -- use physics to accomplish this. elseif(event.phase=="ended")then local vx, vy = ball:getLinearVelocity() ball:setLinearVelocity( 0, vy ) end end background:addEventListener( "touch" ) local function enterFrame( self ) local vx, vy = ball:getLinearVelocity() --horizontalSpeed if((ball.x \<= nowX+10)and(ball.x \>= nowX-10))then ball:setLinearVelocity( 0, vy ) elseif(ball.x \< nowX)then ball:setLinearVelocity( horizontalSpeed, vy ) elseif(ball.x \> nowX)then ball:setLinearVelocity( -horizontalSpeed, vy ) end end ball.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", ball )

The physics engine tries to follow the standard physics formulas within certain limitations. If I’m not mistaken, the -190 linear Y velocity is roughly equal to -190 pixels on the Y axis per second. There is variance due to the engine, frame rate and the calculations, etc. But, as for how far the -190 linear Y velocity goes depends on the gravity. This is called uniform acceleration and its formula, if I remember correctly, is a = ( Vf - Vi ) / t.

Out of curiosity, are you and dodi_games working on the same project? Your needs code-wise seem similar, if not the same, and your code is now demonstrating the issue that dodi_games was talking about and which doesn’t exist in my code. :smiley:

You are currently experiencing that “vibration issue” because of your enterFrame function. You are resetting the ball’s linear X velocity every frame, so even if the ball is bouncing away from some rect that it just collided with, your code removes that bounce and resets the linear X velocity to move the ball towards the block, i.e. the ball collides, gets pushed back and reset to collide again with the block, and repeat.

Here, the most important thing for you, and for everyone trying to help you, is understanding the purpose of your game and how the ball’s movement should work. The easiest method for eliminating that vibration, as you call it, is by setting a rule that the user can freely move the ball until the ball collides with a block. At this point, if the ball collides with the top of a block, you can reset that movement. If the ball collides with some other side, the movement is not reset and so the player can’t keep slamming the ball into a block.

All of these are just some guesses and ideas on how you might be able to implement the movement. This will be ultimately up to you. You need to finish your plans regarding the game and its movement system. I mean, even though I’ve written you a sample of how this movement could be done with physics, I’m only guessing as I don’t know your project and so I’m not yet 100% sold on the idea if physics are absolutely needed for this or if you could do this old school, i.e. without physics.

Nop

@XeduR @Spyric

No we do not work together. Although I recognize that it is very curious that we have the same problem…

However I think it’s a fairly frequent situation.

As for me, I’m trying to imitate something like “Doodle Jump” (https://www.youtube.com/watch?v=wjofzwaC_Oo)

The difference is that my blocks are rectangles.

In addition I would like that if the ball takes the rectangle to the side does not bounce but remains locked there.

I do not want to use physics at any cost. I would rather be able to use the most fluid and effective method.

Can you help me in this? Now is it clearer what I’m trying to do? thank you for your time

If you are working on Doodle Jump like “jumping physics”, then you should have a look at https://docs.coronalabs.com/api/type/PhysicsContact/isEnabled.html.

What you could do is enable contact between the ball and the blocks only if the ball is moving down. This means that the ball can jump through the blocks from below. If you want something else, then you need to write special rules for those cases.

If you don’t want to use physics, then you’d need to write math based collision rules for the ball and the blocks. This wouldn’t be a too difficult task for something like Doodle Jump, but I think that it might be more difficult than you want. Plus, if your game is relatively simple, then using the physics themselves won’t be that burdensome anyway.

Thank you!

I know the function to remove the collision but it is not exactly what I want to do. Or the problem and remove the collision with the movement of the ball according to event.x

Also because using the runtime event I do not have a fluid movement of the ball.

I would like to create a fluid motion like when I change the position of the ball through the" .x" property. But without doing so with physics to avoid the problems we have already discussed here.

I did some tests with joints, and invisible bodies but I can not. I do not know if I explained myself. I know it may seem simple but I can not actually get the desired result

The isEnabled value for collisions that I linked from the docs isn’t about removing the collision at all. It is simply about enabling or disabling the current collision.

To ensure maximum “fluidity of motion”, you want to interfere with the physics engine as little as possible. Perhaps you could try applying a single linear impulse when the background is touched? In any case, you definitely don’t want to affect the ball’s position in that enterFrame function.

But! If you are really looking for just Doodle Jump like jumping motion, then you should be able to get away with what you had originally in terms of changing the ball’s x coordinates if you also use “ball.isFixedRotation = true”. Well, technically this doesn’t properly work with a circular body, but if you had a rectangular body, or at least rectangular “feet”, then this would ensure that the ball won’t jump off the blocks if it hits an edge.

I’ll do a bit of testing to see the best solution then. I’ll let you know