Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Collider for triangular sprite

Tagged: 

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #15644

    Isak

    Hello i have an spaceship with an triangular shape and first tried a circle collider but cant get it to work i tried this first:

     

    CircleCollider {
            id: shipCollider
            radius: shipSprite.width/2
            collisionTestingOnlyMode: true
            anchors.fill: shipSprite
            categories: Circle.Category1
            collidesWith: Circle.Category2
    
            fixture.onBeginContact: {
    
                console.log("collisionShip")
            }
        }

    The collider is nice and centered on my sprite but its to big so i tried to change to:

     

    CircleCollider {
            id: shipCollider
            radius: shipSprite.width/3
            collisionTestingOnlyMode: true
            anchors.fill: shipSprite
            categories: Circle.Category1
            collidesWith: Circle.Category2
    
            fixture.onBeginContact: {
    
                console.log("KollisionShip")
            }
        }

    But when my collider isn’t centered on the sprite anymore it sits in the top left corner, what do i have to do to center it in the middle?

    #15648

    Alex
    Felgo Team

    Hi Isak,

    the anchors properties of the CircleCollider are unfortunately not working at the moment. You can just use simple x and y positioning though, like this

    CircleCollider {
      /*
      the origin of the collider is its top left corner, so
      we move it by half the parent width to the right,
      and then back by half its own width (equals the
      radius) to the left, to center it
      */ 
      x: shipSprite.width/2 - radius
      y: shipSprite.height/2 - radius
    }

    Cheers,
    Alex

    #15652

    Isak

    Hello Alex, thanks for the reply

     

    But i dont get it to work really as i want, if put the collider inside my TexturePackerAnimatedSpriteVPlay it centers on my sprite but if its outside it appears in the left top corner. But i thought that my EntityBase size wouldnt let it appear up there?

     

    import Felgo 3.0
    import QtQuick 2.0
    
    EntityBase {
    
    
    
        id: ship
        entityType: "ship"
        width: shipSprite.width
        height: shipSprite.height
    
        TexturePackerAnimatedSpriteVPlay {
            id: shipSprite
            source: "../assets/PSFSheet.json"
            frameNames: ["Ship0001.png", "Ship0002.png", "Ship0003.png", "Ship0004.png", "Ship0005.png", "Ship0006.png", "Ship0007.png", "Ship0008.png", "Ship0009.png", "Ship0010.png"]
            x: 110
            y: 500
            frameRate: 15
        }
    
        CircleCollider {
            id: shipCollider
            radius: shipSprite.width/3
            collisionTestingOnlyMode: true
            x: shipSprite.width/2 - radius
            y: shipSprite.height/2 - radius
            categories: Circle.Category1
            collidesWith: Circle.Category2
    
            fixture.onBeginContact: {
    
                console.log("KollisionShip")
            }
        }

    Do the collider need to be inside the sprite?

    #15657

    Günther
    Felgo Team

    Hi Isak!

    It is correct to place the collider within the Entity and not the sprite.
    Did you maybe miss to also take the x/y-position offset of the sprite into account as well?

        CircleCollider {
            id: shipCollider
            radius: shipSprite.width/3
            collisionTestingOnlyMode: true
            x: shipSprite.width/2 - radius + shipSprite.x
            y: shipSprite.height/2 - radius + shipSprite.y
            categories: Circle.Category1
            collidesWith: Circle.Category2
    
            fixture.onBeginContact: {
    
                console.log("KollisionShip")
            }
        }

     

    Best,
    Günther from Felgo

    #15659

    Alex
    Felgo Team

    By the way, if you want to position the whole entity somewhere on the screen, use the x/y properties of the EntityBase and not the sprite.

    Cheers,
    Alex

    #15706

    Isak

    alex.huber said:

    By the way, if you want to position the whole entity somewhere on the screen, use the x/y properties of the EntityBase and not the sprite.

    Cheers,
    Alex

    Thanks Guys,

    i have put everything in my Ship.qml when i wanted to separate but when i instead put position of ship and mousearea for dragging in main everything seems to work better:)

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

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded