Making ball and rectangle solid

I want to make my ball and rectangle solid so when the ball is rolling down, it won’t pass right through the rectangle . I tried .isBullet but that didn’t work . I tried increasing and decreasing my addBody attributes but that still didn’t work . Any help please .

 Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, 'dynamic', {density = 2, friction = 0.5, bounce = 0.5, radius = Ball.width / 2}) Ball.isBullet = true -- More accurate collision detection Ball:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball) rectangle = display.newRect( 0, 0, 150, 50 ) rectangle.strokeWidth = 1 rectangle:setFillColor( 2.5 ) rectangle.isBullet = true rectangle:scale(0.5, 0.5) rectangle:setStrokeColor( 0, 3, 0 ) physics.addBody(rectangle, "static", {density=0.1, friction=0.1, radius=10.5}) screenGroup:insert(rectangle)

Several errors in there:

  1. No need to apply density to static bodies.
  2. Your rectangle was given a round body.
  3. You didn’t look at the bodies by using hybrid mode so you missed #2 DO YOUR OWN DEBUG
  4. Radius of circle body need not be calculated from width.  You already know it was 10.
  5. Impulse forces don’t account for mass.
  6. Used scale on shape before adding body.  Works in most cases, but some cases it doesn’t.  I avoid this if possible and always check w/ hybrid to make sure body matches shape or is as I want it.
  7. Friction value a bit low on static body, but it is really up to you.
  8. Using sceneGroup:insert() when you could just pass sceneGroup as first argument in display.new*() calls.  Waste of code and effort to do it longhand.
  9. isBullet not needed on ball; Only used for fast moving bodies.  
  10. isBullet applied to static.  Not a good idea.  You can do it, but waste of CPU cycles.
  11. Positioning of circle and rectangle don’t make sense to me.
  12. Using globals willy nilly?  Seemed like it, but I’ll hope you forward declared ball and rectangle variables somewhere else.  If you’re still randomly making globals like this you need to go back and do a refresher on basic practices.
  13. Random naming convention ( B all versus r ectangle).  You should choose a naming convention and stick to it.  I suggest either camelCase or snake_case.  (Corona already uses camelCase so that is a good fit.)

Suggestion: Take some time to review the entire Physics API reading the purpose of every function and field. Then read each of the guides.  You’re using some features incorrectly, may be unaware of others, and you could have solve solved this if you’d used the debug features supplied by the physics engine.

I modified your code to run for me.  You can take this and modify it to meet your needs:

local physics = require "physics" physics.start() physics.setGravity(0,10) physics.setDrawMode("hybrid") local ball = display.newCircle( screenGroup, display.contentCenterX + 200, display.contentCenterY - 50, 10) physics.addBody(ball, 'dynamic', { friction = 0.5, bounce = 0.2, radius = 10 } ) ball:applyLinearImpulse( -5 \* ball.mass, 0, ball.x, ball.y ) ball.angularDamping = 1 local rectangle = display.newRect( screenGroup, display.contentCenterX, display.contentCenterY, 600, 50 ) rectangle.strokeWidth = 1 rectangle:setFillColor( 0.25, 0.5, 0.25 ) rectangle:setStrokeColor( 0, 3, 0 ) physics.addBody( rectangle, "static", { friction = 1 } )

No I declared my ball and rectangle at the top . But the ball and/or rectangle still isn’t solid . The ball passes straight through . 

local physics=require("physics") physics.start() physics.setGravity( 0, 10 ) --physics.setDrawMode('hybrid') physics.setDrawMode("hybrid") local Ball local rectangle -- background function scene:create(event) local screenGroup = self.view display.setDefault("background", 0, 6, 0) display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) ball = display.newCircle( screenGroup, display.contentCenterX + 200, display.contentCenterY - 50, 10) physics.addBody(ball, 'dynamic', { friction = 0.5, bounce = 0.2, radius = 10 } ) ball:applyLinearImpulse( -5 \* ball.mass, 0, ball.x, ball.y ) ball.angularDamping = 1 rectangle = display.newRect( screenGroup, display.contentCenterX, display.contentCenterY, 150, 50 ) rectangle.strokeWidth = 1 rectangle:setFillColor( 0.25, 0.5, 0.25 ) rectangle:setStrokeColor( 0, 3, 0 ) physics.addBody( rectangle, "static", { friction = 1 } ) end
  1. Ball is misspelled.  No bearing on issue, but important to fix.

  2. You tested this in a basic project w/o composer or the rest of your code?  It looks more like you plopped it into your project.

Verify the problem outside of your project first.  If the code works in a standalone situation, then the problem is something else.

  1. Turn off all other code in your create() call.  Then slowly add it back.

  2. I didn’t see this before, but you typed: screenGroup  more traditionally it is called sceneGroup.   This has not bearing on the issue, but is a good practice note. 

I can tell you there is nothing wrong with that code.  It works standalone, so you’ve got something else going on.

I just ran this in an example project without any scene managers and the same thing happens . The ball passes straight through . And at a certain point the ball just stays there and I can pass the rectangle through the ball . In the beginning though, what I want to happen does happen but just not for long .

 local physics=require("physics") physics.start() physics.setGravity( 0, 10 ) --physics.setDrawMode('hybrid') physics.setDrawMode("hybrid") Ball = display.newCircle( display.contentCenterX + 200, display.contentCenterY - 50, 10) physics.addBody(Ball, 'dynamic', { friction = 0.5, bounce = 0.2, radius = 10 } ) Ball:applyLinearImpulse( -5 \* Ball.mass, 0, Ball.x, Ball.y ) Ball.angularDamping = 1 rectangle = display.newRect( display.contentCenterX, display.contentCenterY, 150, 50 ) rectangle.strokeWidth = 1 rectangle:setFillColor( 0.25, 0.5, 0.25 ) rectangle:setStrokeColor( 0, 3, 0 ) physics.addBody( rectangle, "static", { friction = 1 } ) local function rectangleTouch( event ) if event.phase == "began" then display.currentStage:setFocus( rectangle, event.id ) rectangle.isFocus = true rectangle.x0 = rectangle.x rectangle.y0 = rectangle.y elseif( rectangle.isFocus ) then local dx = event.x - event.xStart local dy = event.y - event.yStart rectangle.x = rectangle.x0 + dx rectangle.y = rectangle.y0 + dy if event.phase == "ended" then display.currentStage:setFocus( rectangle, nil ) rectangle.isFocus = false end end return true end rectangle:addEventListener( "touch", rectangleTouch )

Not sure what you posted there, but it isn’t a demo of the code I posted.

Here are two proper standalone explorations of the code I posted: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/03/rollingBall.zip

  • v1 - Proper standalone example.
  • v2_composer - Proper test of sample in basic composer project.

I added two additional platforms and modified some variables to make the demo more interesting.

If you run these with no changes and they don’t work, tell us what version of Corona you’re using because it is totally foobar.

I expect them to work fine however.

Assuming they work fine:

  1. Make a copy of v1.

  2. Modify it and add to it slowly, running each change, till you find the issue you’re encountering.

  3. Post back your progress/results.   No code posts please.  Zip it up and attach it instead.

Okay . I just changed the code and the same exact thing is still happening 

** I HAVE NOT LOOKED AT THE ZIP YET**

I see in that code you pasted (NOT THE ZIP) that you are moving it with a touch?  

I assume you’re trying to make it into a paddle that you can drag?

That WON’T work. 

  1. The body is static - Proper body for a body that moves is ‘kinematic’ or ‘dynamic’.  Again.   You need a refresh.  Read the docs.

  2. Manually changing the position body bypasses the physics engine.  It doesn’t give the body velocity, and will cause collisions to behave weird.

  3. If you want to drag a body, use a touch joint.

Please don’t include icons and other junk in demos.  I don’t want to cull that stuff.  KISS.

What is this?  It is not a copy of v1 that is then modified.

Frankly, I’m a bit irked with your right now.  You are wasting my time by not following basic directions.

I already tried dynamic it just flew off the screen and kinematic gives me the same results I am having now .

That is because you’re not doing it right.  If you would read the guides this would become clear.

I’m going to post one more example in a minute and then I’m out of here.

Still working on example, but.

in the future if you want help why don’t you tell us what you’re actually trying to do instead of guessing at what the problem is and asking how to fix it.  

If I’d have know you wanted to make a drag-able paddle this talk would be over already.

Sorry .

Download my zip file again.

There is a third example now: v3_basic paddle

You should take some time to read my old post: https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Often I see you and other folks get tripped up, focusing on what you think the problem is.

What happens is, you’re working on a thing, then you run into a problem. 

You puzzle over it for a bit and experiment.  Finally, you decide you know what the problem is and you simply don’t know how to fix it.

You post a question about how to solve this ‘problem’ with no context and without telling us what your real end goal is.  

Often, in the end, the issue you decided was a problem wasn’t the cause of the your troubles and even if we correct the ‘problem’ you focused on, it isn’t the solution that gets you where you really wanted to go. 

So, you end up stuck again.

How Do I Get Off This Ride!?

The solution to this dilemma is to develop a kind of discipline when asking questions in the forums.  Spend real time and effort on the post so it is clear as day:

  • What you are trying to do.
  • Why you are trying to do it (the part folks always leave out)
  • What you did,
  • What you expected to see 
  • What you did to debug it.

If you can post questions that are detailed and clear, while hitting all the points above, you’ll get better help AND you’ll find that you quickly improve as a game developer/programmer. 

As a bonus, learning to do this now will have significant value for you later in life and in future jobs.

Okay it’s sort of working but how can I make the rectangle go anywhere I want it to on the screen ?

Take a close look at that enterFrame listener and see how I’m limiting it. 

Try turning it off or changing it.

This is my last response.