Use CollisionManager, how to know the collision direction?

26 views
Skip to first unread message

che...@sina.com

unread,
Jun 7, 2018, 12:23:15 AM6/7/18
to cocos2d discuss
Use CollisionManager, is there a way to know the collision direction ( top, bottom, right, left) when collision happens?  just like RectMapCollider.

I want to do the following:

if player collide with enemy's top:
    do something
elif player collide with enemy's right/left:
    do something else

Daniel Gillet

unread,
Jun 8, 2018, 1:02:06 PM6/8/18
to cocos2d discuss
No, it's not possible with the current code. To implement such a feature, you would need to keep track of the previous position and the current position of each objects. See function detect_collision in mapcollider.py on line 236 to see how to detect which side is colliding. There is calculates the minimum distance to separate the objects. But you could instead return an object describing which side has collided.

che...@sina.com

unread,
Jun 10, 2018, 1:03:03 AM6/10/18
to cocos2d discuss
Thanks Daniel for your suggestion, I will check the code you mention.

在 2018年6月9日星期六 UTC+8上午1:02:06,Daniel Gillet写道:
Reply all
Reply to author
Forward
0 new messages