Collision randomly slows down a physical body?

Hi all,

I’m a little puzzled here… I have a very simple game stage which is just four rectangles forming a box. Inside this box a ball is moving around, bouncing off the walls. The stage is set with zero gravity and there is no friction on any of physical bodies, so the ball just keeps bouncing around forever. So far so good.

Then I add an obstacle (another rectangle) in the middle of the stage. Now the ball starts acting up really weird. At random impacts with either the box walls or the obstacle, the ball will slow down and crawl to the next wall (or obstacle), where it may or may not pick up speed again on impact. Can anyone explain why this is happening?

The code is posted below. If you comment out the four lines that add the obstacle to the stage, the ball runs smoothly. Comment the lines back in and the ball starts acting up.

-- code demonstrating weird behaviour :-)  
  
local screenW, screenH = display.contentWidth, display.contentHeight  
  
local physics = require("physics")  
physics.start()  
physics.setGravity( 0, 0.0 )  
physics.setDrawMode( "normal" )  
--physics.setDrawMode( "debug" )  
--physics.setDrawMode( "hybrid" )  
  
-- draw top and bottom borders  
local topBorder = display.newRect(0, 0, screenW, 10)  
topBorder.x = screenW / 2; topBorder.y = 0;  
physics.addBody( topBorder, "static", { density=0.0, friction=0.0, bounce=0.0 } )  
  
local bottomBorder = display.newRect(0, 0, screenW, 10)  
bottomBorder.x = screenW / 2; bottomBorder.y = screenH;  
physics.addBody( bottomBorder, "static", { density=0.0, friction=0.0, bounce=0.0 } )  
  
-- draw left and right borders  
local leftBorder = display.newRect(0, 0, 10, screenH)  
leftBorder.x = 0; leftBorder.y = screenH / 2  
physics.addBody( leftBorder, "static", { density=0.0, friction=0.0, bounce=0.0 } )  
  
local rightBorder = display.newRect(0, 0, 10, screenH)  
rightBorder.x = screenW; rightBorder.y = screenH / 2  
physics.addBody( rightBorder, "static", { density=0.0, friction=0.0, bounce=0.0 } )  
  
-- draw obstacle  
local obstacle = display.newRect(0, 0, 10, screenH / 2)  
obstacle.x = screenW / 2  
obstacle.y = screenH / 2  
physics.addBody( obstacle, "static", { density=0.0, friction=0.0, bounce=0.0 } )  
  
-- draw ball   
local ball = display.newCircle(0, 0, 10)  
ball.x = 20; ball.y = 20  
physics.addBody( ball, "dynamic", { density=0.0, friction=0.0, bounce=1.0 } )  
  
-- start ball movement  
ball:setLinearVelocity( 300, 300 )  

I’m using simulator version 2011.704 on Windows.

Help would really be appreciated :slight_smile: [import]uid: 118371 topic_id: 20553 reply_id: 320553[/import]

This actually runs fine for me on my Mac using 704.

Are these “random impacts” perhaps near the corners of the rectangle in the center, or are they on flat surfaces as well?

Peach :slight_smile: [import]uid: 52491 topic_id: 20553 reply_id: 80795[/import]

Oh. It must be your Mac and it’s round corners and glass effects that’s cheating on the physics :slight_smile:

No seriously - I *just* figured it out. Not only does the ball slow down randomly; once slowed down, it also sometimes bounces off a wall in a weird angle. Once I noticed this, I realized it might be spinning around its own axis. So I made its rotation static and voila! It works.

ball.isFixedRotation = true  

Fortunately I don’t even want the ball to spin around, so I’m happy with this solution :slight_smile:

Thanks for your help!
[import]uid: 118371 topic_id: 20553 reply_id: 80851[/import]

Well my Mac is pretty spiffy :wink:

I’m glad you were able to find a solution; nicely done.

Peach :slight_smile: [import]uid: 52491 topic_id: 20553 reply_id: 80985[/import]