How to achieve (almost) perfect/exact collision detection between two CCSprite's?

Hello guys,

i am trying to detect collision of two CCSprite's, a ball (PlayFigureSprite) and something that boosts that ball in a specific direction (BoostSprite):

[Insert picture here showing two BoostSprite's and one PlayFigureSprite with their collision boundingboxes.
One BoostSprite (right one) is rotated to the bottom right and it's boundingbox is bigger than itself.
Left BoostSprite has a boundingbox that perfectly fits it size because it's not rotated.
Backgroundcolor of picture is blue.]

Sadly i cant add a picture because of this message: "You have to be around for a little while longer before you can post links."

For debugging reasons i used CCDrawNode to show the BoundingBoxTransformedToParent of the PlayFigureSprite and BoostSprite's objects.
To detect the collision i use following code:

boostSpriteObj.BoundingBoxTransformedToParent.IntersectsRect(playFigureSpriteObj.BoundingBoxTransformedToParent);

Everything works fine. The only problem is the collision detection when the BoostSprite is rotated.
Rotated BoostSprite's have a bigger BoundingBoxTransformedToParent than their actual size because the BoundingBoxTransformedToParent is not rotated when the sprite was rotated.

In the picture above the ball would be accelerated to the bottom right if it hits the blue area next to the BoostSprite (right one) which is unwanted behaviour. The ball should only be affected by the boost if it really hits the drawn BoostSprite. For the left BoostSprite it works perfect.

I couldn't find a way to rotate that boundingbox or create a boundingpolygon around CCSprite's for better approximation (could also be useful for round objects like the ball).

My Question:
How can I create an almost exact collision detection between two CCSprite's (return only true if the considered CCSprite's REALLY overlap/collide) ?

Answers

  • FlanederFlaneder Member ✭✭
    edited January 2018

    I figured out that i can attach pictures as files but not as links! See the attachment below.

  • JoeBonavitaJoeBonavita USMember ✭✭

    Hi @Flaneder did you get this working the way you want? I'm facing the same situation.

  • FlanederFlaneder Member ✭✭

    @JoeBonavita said:
    Hi @Flaneder did you get this working the way you want? I'm facing the same situation.

    Hi @JoeBonavita,

    NOTE:

    I still can't use links, which is why you have to copy paste the links..

    in the end i used a physics engine called Box2D. It's usually implemented in C++ but the CocosSharp developers created a port for C#. It's also part on their GitHub (github.com/mono/CocosSharp).

    Using Box2D can get clunky sometimes, because it's not very object oriented and has it's own style. But i have to admit if you once understood the mechanisms of Box2D it's pretty easy.

    If you consider to use Box2D take following advices:

    • Have a look at C++ tutorials for Box2D like (iforce2d.net/b2dtut/). C# implementations are very similiar.
    • Have a look at CocosSharp sample projects, which are using Box2D like CocosSharpBox2d (github.com/mono/cocos-sharp-samples/tree/master/CocosSharpBox2d) or AngryNinjas (github.com/mono/cocos-sharp-samples/tree/master/AngryNinjas/src).

    So basically i didn't find a way to solve my problem with "pure" CocosSharp, which is why i did not post this as my answer.

    If you just started development with CocosSharp and want to implement a more complex game, i would recommend you to think twice about which framework you want to use. I feel sorry to say that, but i don't recommend CocosSharp to anyone, except the game is very very simple.

    My Reasons:

    • The forum is almost dead
    • Last update on GitHub at least 2 years ago
    • A lot of gaps in the documentation
    • Physics folder on GitHub causes confusion because there are no tutorials and so on (forums.xamarin.com/discussion/107986/using-ccphysicsbody)
  • JoeBonavitaJoeBonavita USMember ✭✭

    @Flaneder Thank you for your detailed response. I started looking at Box2D but wasn't sure if that was the only option or not.
    CocosSharp is a fairly simple framework but like you said, it's almost dead. I don't know of another 2d gaming engine that can be used in Xamarin though and due to my requirements, I have to use Xamarin. I've done other games in Unity which is so much easier to use.

    Thanks again.

  • FlanederFlaneder Member ✭✭

    @JoeBonavita I don't think it's the only option but i think it's a common one. It also depends on the game you want to develop. If you're trying to implement a physics based game, Box2D is definitly not a bad choice.

    For other options than CocosSharp have a look at MonoGame and UrhoSharp (developer.xamarin.com/guides/cross-platform/game_development/). I dont know much about them but for my next game i will definitly take them into consideration.

  • JoeBonavitaJoeBonavita USMember ✭✭

    @Flaneder I 've been trying to following with the tutorial for Box2D but can't seem to figure out how to get it working. For example, the position has to be set for the body def which is separate from the position of the actual CCNode (in my case a CCSprite) that has a b2Body as a property. So does the position need to be set for both?
    In the tutorial the screen shots show that the objects are being drawn, yet I don't see that in my application. I don't even see a way to specify a color for a body.

    Would you know of a code snippet that could get me started? Mainly I only need 3 objects to detect collision with 2 of them being static.

    Thanks!

  • FlanederFlaneder Member ✭✭
    edited March 2018

    @JoeBonavita said:
    I 've been trying to following with the tutorial for Box2D but can't seem to figure out how to get it working. For example, the position has to be set for the body def which is separate from the position of the actual CCNode (in my case a CCSprite) that has a b2Body as a property. So does the position need to be set for both?

    Box2D is just simulating physics (calculating collisions, gravitation, a lot of math etc.) "in the background" and is not responsible for drawing stuff or rendering images. So what you need to do is to update your CCSprite's position each frame to the position of it's corresponding b2Body. And don't for get about PTM ratio when you're doing that! CCPhysicsSprite from the CocosSharpBox2d sample project shows you how it's done right. Call the UpdateBodyTransform() method for each CCPhysicsSprite every frame in your CCLayer.

    @JoeBonavita said:
    In the tutorial the screen shots show that the objects are being drawn, yet I don't see that in my application. I don't even see a way to specify a color for a body.

    @JoeBonavita You need to set a debug drawer to actually show the b2Bodies on the screen. Every b2Body is invisible by default.

    How you turn on debug drawings for Box2D?

    1. Implement a debug drawer. You can find an implemenation in the CocosSharp sample CocosSharpBox2d. Just copy the Box2DDebug file into your solution.
    2. Add the debug drawer from 1. as a child to your CCLayer so the drawings are actually shown on the screen.
    3. Use the SetDebugDraw() method of b2World to set the debug drawer.

    Assuming you have copied the file from 1., this is the code:

    NOTE: Following code snippets are copied from here

    void StartDebugging()
    {
        var debugNode = new CCDrawNode();
        // 2. step
        AddChild(debugNode, 1000);
        // from 1. step
        debugDraw = new Box2DDebug(debugNode, PTM_RATIO);
    
        // this is not necessesary but can be helpful
        debugDraw.Flags = b2DrawFlags.e_shapeBit | b2DrawFlags.e_aabbBit | b2DrawFlags.e_centerOfMassBit |  b2DrawFlags.e_jointBit;
        //3. step
        world.SetDebugDraw(debugDraw);
    }
    

    After everything has been set you have to render the debug drawings every frame.

    // call this method every frame
    void RenderDebug()
    {
        if (debugDraw != null)
        {
            debugDraw.Begin();
            world.DrawDebugData();
            debugDraw.End();
        }
    }
    

    @JoeBonavita said:
    Mainly I only need 3 objects to detect collision with 2 of them being static.

    To recognize and handle collisions i would recommend you to implement your own b2ContactListener.

    Providing some parts of your code might help.

    Hope i could help you a lil bit!

  • JoeBonavitaJoeBonavita USMember ✭✭

    @Flaneder Thank you very much!!! This is a huge help!

Sign In or Register to comment.