Side of a rect

Hello,

i have a question, another one) How can one determine one side or other side of rectangle?
Say i have a rect that 100 pixels wide and then i move another rectangle to it from side
how can i determine what side another rect hits without using any physics? How can i get x coordinates of rectangle corners for example?

thanks in advance

a bit of maths, you need to draw these on paper and see...

I did not really understand your question other than the fact that you want to detect collision without using physics.

say you have rectangle A and rectangle B
A is at 10,10 and is 100x100
B is at 50,50 and is 200x100

the corners for A are
x ,y = 10, 10
x1,y = 110, 10
x ,y1 = 10,110
x1,y1 = 110,110

and, the corners for B are
x ,y = 50, 50
x1,y = 250, 10
x ,y1 = 50,110
x1,y1 = 250,110

you do not have to calculate all of these, but you need to check if the x,y of rectangle B fall within x,y and x1,y1

which can be managed as

if (rectB.x > rectA.x and rectB.x < rectA.x+rectA.contentWidth ) and (rectB.y > rectA.y and rectB.y < rectA.y+rectA.contentHeight ) then
--Well, congratulations!!! You are the proud owner of a collision
end

cheers,

?:)

thanks for answer
i'll ask another question, if its not a problem

How to determine if object2 standing near object2 in 50(or whatever) pixel radius? or just 50 pixels left or right

Search the forums for non-physics collisions. There are two easy to implement functions that test if two objects are occupying the same space, one for rectangles, the other for circles.

The circle one already has a radius parameter that you can provide a bigger area than the object to test. The rectangle version could be easily modified to pass a range parameter in to check if the collision happens in a bigger box than the physical rectangle.

I know that you don't want to use physics but why?

i just dont like it)many complications with all that "dynamic" and "kinematic" things..
why they cant just register collisions without restrains?

Physics adds a lot of overhead and complexity if all you want to know is if to objects occupy the same space.

so, i tried to use physics) why is this kind of thing not working? are physics cant detect collision while transitioning or is it because of the way "player" moves?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local player = display.newCircle(0,0,20)
player:setFillColor(255,0,0)
player.x = 50
player.y = _H-flor.height-player.height
player.visible = false
physics.addBody(player, "dynamic")
player.name = "player"
 
local function going()
        if left == 1 then
        player.x = player.x - 3
end
if right == 1 then
        player.x = player.x + 3
end
end
 
local npc = display.newCircle(0,0,25)
npc:setFillColor(0,255,0)
group:insert(npc)
physics.addBody(npc, "kinematic")
 
local npc_rect = display.newRect(0,0,150,30)
group:insert(npc_rect)
npc_rect.x = npc.x
npc_rect.y = npc.y
physics.addBody(npc_rect, "kinematic")
npc_rect.name = "rect"
 
 
function to_left()
if blo == true then
transition.to(group, {time=3000, x = group.x - 100, onComplete = to_right})
end
end
 
function to_right()
transition.to(group, {time=3000, x = group.x + 100, onComplete = to_left})
end
 
to_right()
 
local function onCollision(event)
        if event.object1.name == "rect" and event.object2.name == "player" then
        print("lol")
        end
end
 
Runtime:addEventListener("collision", onCollision)
views:1514 update:2011/10/28 9:34:19
corona forums © 2003-2011