any way to get contentBounds.xMax etc as floats rather than ints?

I wrote a function that gives you width and height of two intersecting rectangles.  only problem is that xMax etc seem to always give you integers instead of floats.  Any way to modify this?

function rectFromIntersection( obj1, obj2 ) return math.abs(obj1.contentBounds.xMax - obj2.contentBounds.xMin), math.abs(obj1.contentBounds.yMax - obj2.contentBounds.yMin) end

No, but why would you want to?  The bounds of an object are in (design space) pixels.

Please give a usage case where this would be needed.  There may be an alternate solution.

I though about this a bit more.

Question: Are moving objects on sub-pixel alignments and attempting to get the width of two overlapping rectangles?

http://www.raywenderlich.com/62049/sprite-kit-tutorial-make-platform-game-like-super-mario-brothers-part-1

Look at this tutorial. Trying to implement this in corona. Im doing a tile based platformer. Check out the part of this code where the guy uses cgrectintersectsrect. That’s the code I implemented above. He gets intersection rectangle and reverses the Sprite by that amount. Anyway integers make the Sprite vibrate in place and that is not good

Well, I looked briefly, but I’m afraid I can’t spend a lot of time reading a tutorial not designed for Corona to answer a question about Corona.

Having said that, let’s try this a different way. 

What is the exact problem you are encountering (besides not getting sub-pixel values)? i.e.

  1. What did you do (we know this already).

  2. What did you see?

  3. What did you expect to see?

Please frame the answers to #2 and #3 in terms of visual issues not missing floating-point values. Ex: “I saw gaps between block, or blocks overlapped, etc.”

Also, please consider providing the following:

  1. config.lua settings you’re using.

  2. Device you’re testing on or simulated device, including resolution.

  3. If you’re really keen on this, consider providing a small sample app demonstrating just the problem.

I know this seems like a lot of work, but at this point, I know you wish that content bounds were expressed on the sub-pixel level (not going to happen), but I’m unclear why you need it.  Once I know that I may be able to suggest a solution or workaround.

In all likelihood the solution to this issue will probably be grounded in a thorough understanding of how Corona scales design space versus screen space, combined with an understanding of how to extract that information and use it.

  1.  i see my sprite colliding into the floor and bouncing up and down since my rect is calculated as 0 or 1 pixels width rather than a float on every frame.

  2.  i expect to see my sprite sit still

config.lua

application = { content = { width = 320, height = 568, scale = "letterBox", fps = 60, }, }

i’m testing on the corona simulator using an iphone 5

here is my project zipped up

https://drive.google.com/file/d/0B4kzFbLmJr19NTRfR0hiSFItUTg/view?usp=sharing

Original code:

function rectFromIntersection\_orig( obj1, obj2 ) local width = math.abs(obj1.contentBounds.xMax - obj2.contentBounds.xMin) -- if width \< 1 then width = 0 end local height = math.abs(obj1.contentBounds.yMax - obj2.contentBounds.yMin) -- if height \< 1 then height = 0 end return width, height end

Fixed code:

local mAbs = math.abs function rectFromIntersection( obj1, obj2 ) local sx = display.contentScaleX local sy = display.contentScaleY local x1, y1 = obj1.x / sx, obj1.y / sy local x2, y2 = obj2.x / sx, obj2.y / sy local cw1, ch1 = obj1.contentWidth / sx, obj1.contentHeight / sy local cw2, ch2 = obj2.contentWidth / sx, obj2.contentHeight / sy local xMax = x1 + cw1/2 local xMin = x2 - cw2/2 local yMax = y1 + ch1/2 local yMin = y2 - ch2/2 local width = mAbs(xMax - xMin) \* sx local height = mAbs(yMax - yMin) \* sy return width, height end

nice code… does this take anchor points into account too? :slight_smile:

No.  You’ll have to adjust for anchor points.  That makes the whole bit a little more complicated.

This assumes the anchor is in the default position for x and y.

great code.  i was rolling up my own solution before you posted your code …  this is what i came up with.  it takes anchor points into account but not object scale.  just throwing it out there.  thanks for the help :slight_smile:

function rectFromIntersection( obj1, obj2 ) function createBoundsTbl(obj) function xMin(obj) return obj.x - (obj.anchorX \* obj.contentWidth) end function xMax(obj) return xMin(obj) + obj.contentWidth end function yMin(obj) return obj.y - (obj.anchorY \* obj.contentHeight) end function yMax(obj) return yMin(obj) + obj.contentHeight end return { xMin = xMin(obj), xMax = xMax(obj), yMin = yMin(obj), yMax = yMax(obj) } end local obj1Bounds = createBoundsTbl(obj1) local obj2Bounds = createBoundsTbl(obj2) local width = obj1Bounds.xMax - obj2Bounds.xMin local height = obj1Bounds.yMax - obj2Bounds.yMin return math.abs(width), math.abs(height) end

No, but why would you want to?  The bounds of an object are in (design space) pixels.

Please give a usage case where this would be needed.  There may be an alternate solution.

I though about this a bit more.

Question: Are moving objects on sub-pixel alignments and attempting to get the width of two overlapping rectangles?

http://www.raywenderlich.com/62049/sprite-kit-tutorial-make-platform-game-like-super-mario-brothers-part-1

Look at this tutorial. Trying to implement this in corona. Im doing a tile based platformer. Check out the part of this code where the guy uses cgrectintersectsrect. That’s the code I implemented above. He gets intersection rectangle and reverses the Sprite by that amount. Anyway integers make the Sprite vibrate in place and that is not good

Well, I looked briefly, but I’m afraid I can’t spend a lot of time reading a tutorial not designed for Corona to answer a question about Corona.

Having said that, let’s try this a different way. 

What is the exact problem you are encountering (besides not getting sub-pixel values)? i.e.

  1. What did you do (we know this already).

  2. What did you see?

  3. What did you expect to see?

Please frame the answers to #2 and #3 in terms of visual issues not missing floating-point values. Ex: “I saw gaps between block, or blocks overlapped, etc.”

Also, please consider providing the following:

  1. config.lua settings you’re using.

  2. Device you’re testing on or simulated device, including resolution.

  3. If you’re really keen on this, consider providing a small sample app demonstrating just the problem.

I know this seems like a lot of work, but at this point, I know you wish that content bounds were expressed on the sub-pixel level (not going to happen), but I’m unclear why you need it.  Once I know that I may be able to suggest a solution or workaround.

In all likelihood the solution to this issue will probably be grounded in a thorough understanding of how Corona scales design space versus screen space, combined with an understanding of how to extract that information and use it.

  1.  i see my sprite colliding into the floor and bouncing up and down since my rect is calculated as 0 or 1 pixels width rather than a float on every frame.

  2.  i expect to see my sprite sit still

config.lua

application = { content = { width = 320, height = 568, scale = "letterBox", fps = 60, }, }

i’m testing on the corona simulator using an iphone 5

here is my project zipped up

https://drive.google.com/file/d/0B4kzFbLmJr19NTRfR0hiSFItUTg/view?usp=sharing

Original code:

function rectFromIntersection\_orig( obj1, obj2 ) local width = math.abs(obj1.contentBounds.xMax - obj2.contentBounds.xMin) -- if width \< 1 then width = 0 end local height = math.abs(obj1.contentBounds.yMax - obj2.contentBounds.yMin) -- if height \< 1 then height = 0 end return width, height end

Fixed code:

local mAbs = math.abs function rectFromIntersection( obj1, obj2 ) local sx = display.contentScaleX local sy = display.contentScaleY local x1, y1 = obj1.x / sx, obj1.y / sy local x2, y2 = obj2.x / sx, obj2.y / sy local cw1, ch1 = obj1.contentWidth / sx, obj1.contentHeight / sy local cw2, ch2 = obj2.contentWidth / sx, obj2.contentHeight / sy local xMax = x1 + cw1/2 local xMin = x2 - cw2/2 local yMax = y1 + ch1/2 local yMin = y2 - ch2/2 local width = mAbs(xMax - xMin) \* sx local height = mAbs(yMax - yMin) \* sy return width, height end

nice code… does this take anchor points into account too? :slight_smile:

No.  You’ll have to adjust for anchor points.  That makes the whole bit a little more complicated.

This assumes the anchor is in the default position for x and y.

great code.  i was rolling up my own solution before you posted your code …  this is what i came up with.  it takes anchor points into account but not object scale.  just throwing it out there.  thanks for the help :slight_smile:

function rectFromIntersection( obj1, obj2 ) function createBoundsTbl(obj) function xMin(obj) return obj.x - (obj.anchorX \* obj.contentWidth) end function xMax(obj) return xMin(obj) + obj.contentWidth end function yMin(obj) return obj.y - (obj.anchorY \* obj.contentHeight) end function yMax(obj) return yMin(obj) + obj.contentHeight end return { xMin = xMin(obj), xMax = xMax(obj), yMin = yMin(obj), yMax = yMax(obj) } end local obj1Bounds = createBoundsTbl(obj1) local obj2Bounds = createBoundsTbl(obj2) local width = obj1Bounds.xMax - obj2Bounds.xMin local height = obj1Bounds.yMax - obj2Bounds.yMin return math.abs(width), math.abs(height) end