0

I am working on a Cocos2D project using Box2D and I am trying to detect when 2 specific edges on an object touch, for example for the red edges in the images below:

enter image description here

I am still learning collisions but I understand the basics and how to detect if 2 specific bodies touch, but I cannot figure out how to detect specific edges.

One solution I thought of was to new create bodies on the edges that I want to detect, but I want to know when both red edges are completely aligned and the boxes form a rectangle.

There may be an easy way to do this but I scoured the internet and found no useful examples.

Any help is appreciated, thanks!

RyanG
  • 4,393
  • 2
  • 39
  • 64

2 Answers2

1

Basically, Box2D detects collisions not between b2Bodies, but between b2Fixtures. All you need is add sensor fixture to your b2Bodies (fexture.isSensor = YES), set it's position to needed point (just 1-2 pixels to the right edge, out of your physical shape) and detect contacts for thise two sesors. I hope this was clear.

Morion
  • 10,495
  • 1
  • 24
  • 33
SentineL
  • 4,682
  • 5
  • 19
  • 38
1

You could add two small fixtures to the relevant corners of each body like this, and check when the two pairs (red/red, blue/blue) are overlapping simultaneously. Circle fixtures would be fine, and resizing them is an easy way to adjust how precise the match needs to be. enter image description here

iforce2d
  • 8,194
  • 3
  • 29
  • 40