How to detect if a display object is within a rectangle or intersecting another object

If I have an object flying across the screen, what is the best way to test if it is in a certain area (not using physics collisions)? The area is defined by a rectangle.

I can’t find any intersectRect, hitTest, etc. in the API docs. [import]uid: 52127 topic_id: 9877 reply_id: 309877[/import]

Well its a simple enough case to write your own code.
If you have rect1 and want to know if it interesects rect2, where each rect is something like { x=, y=, width=, height= } you’d basically do like (psuedo code, but might work if I’m lucky!):

if rect1.x + rect1.width \>= rect2.x then if rect1.x \<= rect2.x + rect2.width then if rect1.y + rect1.height \>= rect2.y then if rect1.y \<= rect2.y + rect2.height then print("collision!") end end end endYou could roll all the conditionals into a single line, but I don’t really see the point.
If you want to do lots and lots of collisions simultaneously you could break the screen down into a grid and only do the above test if both rects are in close-by grid cells, but that’s an optimisation problem rather than out and out collision testing. [import]uid: 46639 topic_id: 9877 reply_id: 36011[/import]

[lua]function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin + 5 <= obj2.contentBounds.xMin and obj1.contentBounds.xMax - 5 >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin - 5 >= obj2.contentBounds.xMin and obj1.contentBounds.xMin + 5 <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin- 5 >= obj2.contentBounds.yMin and obj1.contentBounds.yMin + 5 <= obj2.contentBounds.yMax
if left then
which = “left”
elseif right then
which = “right”
elseif up then
which = “up”
elseif down then
which = “down”
end
print(which)
return (left or right) and (up or down)
end[/lua]

pass ur both object to this function [import]uid: 12482 topic_id: 9877 reply_id: 36014[/import]

I’m having the same issue. I don’t want to use physics but just want to do simple hit test collision detection.

I implemented hit test first. But the calculation is quite heavy since the cost is O(N^2).

I could implement a spatial partition algo to optimize it in Lua.
But wondering is there any native method of doing it instead of coldet from script?

Thanks [import]uid: 7995 topic_id: 9877 reply_id: 36032[/import]

pass ur both object to this function

code exchange entry:
http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds

is there any native method of doing it instead of coldet from script?

Using physics sensors is the native method, so if you don’t want to enable physics, well that’s why I wrote that function. I didn’t want to bother with physics for just one occasional collision situation, but if your game requires so much collision detection that the calculation cost is prohibitive then you should probably use physics for the collision detection. [import]uid: 12108 topic_id: 9877 reply_id: 36094[/import]