display.newRect does not work as advertised

I spent an hour trying to figure out where my shape creation code was wrong when at last I discovered that such a simple thing as display.newRect(x, y, width, height) doesn’t even work as expected. Just run this and find out:

local r = display.newRect(0, 0, 100, 100)  
print(r.x, r.xOrigin, r.y, r.yOrigin, r.width, r.height)  

It’s obvious that you have more than one developer working on this, but do you have any actual end users in house, or are you relying on us paying customers to do all of your testing? This is frustrating.

Alex
[import]uid: 3473 topic_id: 2434 reply_id: 302434[/import]

The default x and y of a newRect is the center of the rectangle, which in your example is .x = 50 and .y = 50.

Try this:

local r = display.newRect(0, 0, 100, 100) r:setReferencePoint( display.TopLeftReferencePoint ) print(r.x, r.xOrigin, r.y, r.yOrigin, r.width, r.height) [import]uid: 8541 topic_id: 2434 reply_id: 7228[/import]

I did call setReferencePoint and a first test run seemed okay.

But, because I need these shapes to be added to physics, and because I want to pause the simulation once in a while and, at that time, move all bodies back to their original positions, I changed my code to this and the problem (wonky physics) became apparent:

local physics = require "physics"  
physics.setGravity(0, 9.8)  
physics.setDrawMode( "hybrid" )  
  
local function makeBody(x, y, w, h)  
 local r = display.newRect(x, y, w, h)  
 r:setReferencePoint( display.TopLeftReferencePoint )  
 print(r.x, r.xOrigin, r.y, r.yOrigin, r.width, r.height)  
 r:setFillColor(255, 0, 0)  
 r.orgX = r.x  
 r.orgY = r.y  
 r.orgRot = 0  
 return r  
end  
  
physics.addBody(makeBody(0, 0, 100, 100), { density = 1.0, friction = 0.7, bounce = 0.2})  
physics.addBody(makeBody(90, 101, 100, 100), { density = 1.0, friction = 0.7, bounce = 0.2})  
  
physics.start() -- this runs okay the first time, or so it seems  
  
-- but, when user presses pause...  
local function resetBody(aBody)  
 aBody:setReferencePoint(display.TopLeftReferencePoint)  
 aBody.x = aBody.orgX  
 aBody.y = aBody.orgY  
 aBody.rotation = aBody.orgRot  
 aBody:setLinearVelocity (0,0)  
 aBody.angularVelocity = 0  
end  
  
local function pause()  
 physics.pause()  
 for i=1,display.getCurrentStage().numChildren do  
 resetBody(display.getCurrentStage[i])  
 end  
end  
  
-- then presses play again...  
local function play()  
 physics.start()  
end  
  
-- ===========================  
-- physics starts misbehaving!  
-- ===========================  

And now, I am not even able to run the code above. simulator crashes from a bus error as soon as it has loaded the program.

Alex [import]uid: 3473 topic_id: 2434 reply_id: 7230[/import]