Rebound length

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

I have implemented a doodle jump clone and the jumping mechanic is very basic.

  1. Have little to no bounce.

  2. Apply an upward impulse on the player when the player collides with the top of a platform in order to make it ‘jump’.

Jake,

I made the basic game of doodle jump and posted about it here: https://forums.coronalabs.com/topic/74120-mechanics-in-250-or-less-with-corona-doodle-jump/

You might want to see the approach I used for fixed height jumps.

Thanks a lot!
I look it now

Jake, 

We just talked about this here: https://forums.coronalabs.com/topic/74097-physics-restitution-question/

Short answer: No, not really.

Well… you can calculate an approximate value for ‘bounce’ that will (reasonably) accurately bounce back a specific distance assuming all of these factors

  • One bounce, after that the rebound distance will be  different
  • Known starting position of both objects involved in bounce.
  • The angle-of-incidence is 0-degrees.  i.e. the rebound vector is the opposite of the motion vector proceeding it.
    • see image below for concept of angle-of-incidence
  • Circular body bouncing off square body.
    • At least two faces that collide must not interfere with rebound and must be equivalent of parallel to each-other
  • Only one body in the collision is moving.
  • Known- and fixed- acceleration or better yet known velocity at time of impact.

You can in fact handle variations in ‘angle-of-incidence’ and the ‘one-body moving’ rules, but that is even more math.

At the end of the day this is simply too much work except in the simplest of cases and even then the result is only semi-accurate.

AngleOfIncidence_800.gif

Jake, 

If you’ve taken Calculus and Physics with Calculus (I know some colleges have Physics courses w/o Calculus), you have all you need to solve this.

Box2D is trying to simulate physics exactly as you learn it in school. i.e. All the same simplifications are applied, homogenous density, point bodies, etc, etc.

( Update: Removed acceleration note because obviously we need at least one force here to slow the rebound and in all except the simplest cases this means using calculus.)

If you can’t tell, I enjoy questions like this… last post till others post or you post back Jake.

Can you tell us a little bit about how you want to use this?  i.e. Give us some scenarios around this mechanic.

We might be able to give other ideas to you to get the result you need.

Hello.

Thank you @roaminggamer

I apologize for opening a new forum but I had not seen a similar question

With physics I’m not very good so any extra help is always welcome!

In reality the dynamics I need are similar to the other post. A ball that jumps from one block to another.

I also considered not using the physics library but I think it would be more complicated…

I saw that you too at some point advised to abandon physics for this case. Have you already done something like this without physics? it’s complicated?

Kk sorry I thought it was clear, but an example is always the best thing:

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) --\> Physicslocal physics = require("physics") physics.start() --physics.setGravity( 0, 9.81) physics.setDrawMode( "hybrid" ) local background = display.newRect( 160, 250, 500, 500 ) background:setFillColor( 0.7, 0.5, 0.5 ) local ball = display.newCircle(160,200, 10) ball:setFillColor( 1, 0, 0 ) physics.addBody(ball, "dynamic", { density=1, friction=0.0, bounce=1, radius=10 }) local block1 = display.newRect( 80, 240, 20, 20 ) block1:setFillColor( 0, 1, 0 ) physics.addBody( block1, "static" ) local block2 = display.newRect( 160, 280, 20, 20 ) block2:setFillColor( 0, 1, 0 ) physics.addBody( block2, "static" ) local block3 = display.newRect( 240, 320, 20, 20 ) block3:setFillColor( 0, 1, 0 ) physics.addBody( block3, "static" ) function background:touch(event) if(event.phase=="moved")then ball.x = event.x end end background:addEventListener( "touch" )

I would like to move the ball from one block to the other keeping the rebound constant.

the problems are 2:

  1. the ball does not bounce constantly

  2. I do not think that changing the position by changing x of ball is the best choice…but I have not found any other with joints

The box2d physics engine that Corona uses is fantastic and allows for some fantastic usage. In fact, I’m working on a physics based game at the moment as well. Still, as Ed already pointed out, the physics simulations aren’t perfect and there are certain limitations that you need to take into consideration.

First off , you are encountering one such problem in your sample code when you are changing the ball’s x-coordinate through the background:touch function. When an object has a physics body and is subjected to linear forces, then you cannot intervene with that object’s movement without “bugging it out”. As you are setting the ball’s x position, the physics engine is trying to adjust it at the same time, which results in weird jittering or jumping motion.

If you want to move a physics object, it’s best done by using the physics engine itself by using the linear impulse and velocity functions. For constant velocity for sideways movement, you could start by getting the object’s linear Y velocity. Then you apply that constant linear X velocity to the desired movement direction and you use the linear Y velocity that you previously retrieved. By getting that linear Y velocity and applying it, you’ll maintain the object’s Y velocity.

Depending on your preferences, you could just set the linear X velocity to 0 again once you end movement, so the object stops its X movement as soon as you let go. Alternatively, you could let the X velocity wind down so that the X movement doesn’t end so abruptly.

Secondly , regarding your question of constant jump height. You could achieve this by tracking collisions between the circle and those rects. Whenever the circle collides with a rect, then you set that constant jump height as the circle’s linear Y velocity. This would essentially reset the jump after every collision regardless of how long the circle’s previous jump was.

However, this will require some extra tricks to prevent unwanted behaviour. You might want to write rules that the linear Y velocity is reset ONLY in the case of the circle falling down and/or if it hits a rect’s top side. Otherwise it would jump higher if it hits a rect’s right, left or bottom sides too.