Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Question about Physics Collision Detection

Posts: 47

Does anyone know if it is possible to detect what side of a physics object has been collided with? For example if the bottom of a square has been hit and then it does something, but NOT do anything when it is hit from any other side?

Thanks for any help in advance,
Jonathon

Tagged:

• Posts: 5,396

If you have a look at one (I can't remember which) of the physics demos, it actually circles the vertices which collide.

• Posts: 7,809

@JonoGaming00 This kind of interested me, so I thought I'd create a small example. The red circles show the contact points. This should work with any type body, but I was lazy and only used circles. The function that determines the contact points for collisions is the bod:collide function. Everything else is just standard code. I tried to make it as simple as I could so it would be easy to understand.

function setup()
b1=bod()
c=physics.body(CIRCLE,70)
c.x=370
c.y=600
c.restitution=.5
c.gravityScale=.1
c=physics.body(CIRCLE,50)
c.x=190
c.y=280
c.restitution=.8
c.type=STATIC
c=physics.body(CIRCLE,50)
c.x=410
c.y=370
c.type=STATIC
c=physics.body(CIRCLE,20)
c.x=320
c.y=254
c.type=STATIC
end

function draw()
background(0)
b1:draw()
end

function collide(c)
b1:collide(c)
end

bod=class()

function bod:init()
self.bodies={}
self.contacts={}
end

table.insert(self.bodies,b)
end

function bod:draw()
stroke(255)
strokeWidth(2)
noFill()
-- draw circles
for a,b in pairs(self.bodies) do
end
noStroke()
fill(255,0,0)
-- draw contact points
for c,d in pairs(self.contacts) do
for e,f in pairs(d.points) do
ellipse(f.x,f.y,20)
end
end
end

function bod:collide(c)
if c.state==BEGAN then
self.contacts[c.id]=c
elseif c.state==ENDED then
self.contacts[c.id]=nil
end
end

• Posts: 47

@Ignatz I couldn't find the demo I think you mean, but I have found they all do what @dave1707 has done with his code.

@dave1707 I think this what I am looking for. However how do I use this to determine what side has been collided with?

• Posts: 7,809

@JonoGaming00 In your first post you're talking about a square. To determine which side of a square was hit, use the x,y values of the squares corners and the x,y values from my program. That can tell you which side was hit.

• Posts: 7,809

When I have time, I'll setup a square in my program and see what really happens.

• Posts: 47

@dave1707 I see what you mean. I'll give that a shot!