Find the Best Demos and Examples

Check out this quick tour to find the best demos and examples for you, and to see how the Felgo SDK can help you to develop your next app or game!

Forums

OverviewFelgo 1 Support › BoxCollider

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #6460

    kitKat

    Hi!

     

    Is there any way to check at which length an entity collides with another? (in our game, they shouldn’t be deleted if they collide at an edge, just if they collide at the whole border of a side).

    #6462

    Alex
    Felgo Team

    Hi kitKat,

    this is not that trivial as far as i know, but I think i can show you everything you need to solve your problem.

    First you will need to find out the direction of the collision, this information is stored in the “contactNormal” vector which is available within the “fixture.onBeginContact:” signal. You can find a code sample in Ball.qml of the VPong demo game.

    If you know from which side the entity is approaching the target, you can compare the x and y values to find out the “length” of the collision.

    Let me know if this helped you. Also if you produce some nice code that could help other developers, feel free to post it here 😀

    Cheers,
    Alex

    #6482

    kitKat

    Hi!

     

    Finally i got it!

    Here’s the code:

     var normalX = contactNormal.x;
                    var normalY = contactNormal.y;
    
                    console.debug("X: ", normalX, ", Y: ", normalY);
    
    
                    if (normalX === 1) { //collision from left
                        
                        //check if same height (from the side)!
                        if (component.parent.y === parent.y) {
                            removeEntity();
                        }
                    }
                    else if (normalX === -1) { //collision from right
                       
                        //check if same height (from the side)!
                        if (component.parent.y === parent.y) {
                            removeEntity();
                        }
                    }
                    else if (normalY === 1) { //collision from above
                       
                        //check if same x-coordinates (from above)
                        if (component.parent.x === parent.x) {
                            removeEntity();
                        }
                    }
                    else if (normalY === -1) { //collision from under
                        
                        //check if same x-coordinates!
                        if (component.parent.x === parent.x) {
                            removeEntity();
                        }
                    }

     

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.