Forums

OverviewV-Play 1 Support › BoxCollider

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #4003

    alexandra.grohmann

    Hello!

    We have a problem with our V-Play Game. We try to implement an “Unblock Me” Game. The aim is it to bring the pink block to the right of the gamearea.

    Now we can move the vertical stones up and down and the horizontal stones left and right.

    We move the stones via a MouseArea and would like to implement a BoxCollider to realize if a stone collides with another stone. The problem is how to say, that if there is a Collision, the stone should not move forward.

    If the stones now collides with another stone, we put down their opacity only to test if the BoxCollider works.

    We hope you can help us.

    Best regards, Alexandra

    The first pictures shows our current game, the second picture is the qml-file of our pink block.

    #4116

    Christian
    V-Play Team

    Hi Alexandra,

    you can stop the movement of the stone, by setting the enabled property of MouseArea to false in fixture.onBeginContact. That way, the dragging of the stone is stopped.

    Cheers,
    Chris

    #4123

    alexandra.grohmann

    Do you think so?

    fixture.onBeginContact: {
    mouse.enabled = false
    }

    because then the stone stops, but he is “inside” the next stone and not before

    Best regards, Alexandra

    #4124

    Christian
    V-Play Team

    Yes that was what I was thinking of!

    Are you sure your colliders are correctly set up and match the visual representation? Please go to main.cpp and turn on the qmlRenderer, to verify that.

    You can also get the position of the entity at your collision point and add the half of the width to it, so you will also receive the position where your collision took place.

    Cheers,
    Chris

    #4125

    johanna_g18

    Further, there is the problem that if the stone was stopped once, we can never move it again. I think there should be code like this:

    mouse.enabled = true

    but where?

    Best wishes,
    Johanna

    #4126

    Christian
    V-Play Team

    That’s a very good question!

    The problem is, that after disabling the MouseArea, no onReleased gets called so you cannot re-enable it at that time. So I think it may be better to not disable it, but instead set the drag.target to another (temporary) item, and in onReleased set it back to your entity.

    The code would look something like that:

    
    import VPlay 1.0
    import QtQuick 1.1
    
    EntityBase {
      id: entity
    
      MouseArea {
        id: mouseArea
        anchors.fill: parent
        drag.target: entity
    
        onReleased: {
          // set it to the entity again, so it can be re-dragged
          mouseArea.drag.target = entity
        }
      }
    
    
      Item {
        // this gets only used to avoid dragging the entity when a collision occured
        id: temporaryDragTarget
      }
    
      BoxCollider {
        // ... your boxCollider information here ...
    
        fixture.onBeginContact: {
          // avoid dragging further, when a collision was reached
          mouseArea.drag.target = temporaryDragTarget
        }
      }
    }
    

    Cheers,
    Chris

    #4127

    alexandra.grohmann

    Ok, it is working now.
    But there is still the problem that, if I move a stone and he collides with an other stone that the stones are overlapping.
    I have noticed, that when i move the stone faster it is more over the edge of the other stone and when i move it slower it stops in front of the stone or is a little bit overlapping.

    #4128

    Christian
    V-Play Team

    Yes that sounds reasonable. You could solve that by calculating your non-overlapping position by using the size of the collided entity, and the size of your entity. You can then set the entity position to be outside of the collision range.

    See the fixture.onBeginContact doc how to get the body of the collided entity: http://doc.v-play.net/qml-components-fixture.html#onBeginContact-signal

    Cheers,
    Chris

    #4672

    alexandra.grohmann

    We have now set the drag target to a temporary item and calculated the position to be outside but the stones are still flickering if they collide.

    Our Code in one standing stone looks like:

            fixture.onBeginContact: {
                mouse.drag.target = temp
    
                var collidedColliderComponent  = other.parent.parent
                var collidedEntity = collidedColliderComponent.parent
    
                if(collidedEntity.y > entity.y) {
                    entity.y = collidedEntity.y - entity.heigth - 1
                } else {
                    entity.y = collidedEntity.y + collidedEntity.heigth + 1
                }
            }

     

    Do we need another collider component or should this work?

     

    Greeting,

    Alexandra

    #4694

    David

    Hi Alexandra,

    maybe you want to rethink your physical concept. Basically there are two ways how to handle and use physic systems:

    1. Only use collision detection without physics effects (logic needs to be implemented by your own, which is kinda tricky) – which is your current approach.
    2. Use the complete physic system with real collisions and physical effects. Might be easier!

    If you try approach two, you can see the StackTheBoxDemo which handels already boxes, such as your blocks which can be dragged and moved around with a Mouse Joint. You can restrict the movement direction by defining the correct target point. This is the MouseJoint from StackTheBoxDemo but with the restriction of movement only into x direction archieved in onPositionChanged.

     

            MouseArea {
                anchors.fill: parent
    
                property Body selectedBody: null
                property MouseJoint mouseJointWhileDragging: null
    
                onPressed: {
    
                    selectedBody = physicsWorld.bodyAt(Qt.point(mouseX, mouseY));
                    console.debug("selected body at position", mouseX, mouseY, ":", selectedBody);
                    // if the user selected a body, this if-check is true
                    if(selectedBody) {
                        // create a new mouseJoint
                        mouseJointWhileDragging = mouseJoint.createObject(physicsWorld)
    
                        // set the target position to the current touch position (initial position)
                        mouseJointWhileDragging.targetPoint = Qt.point(mouseX, mouseY)
    
                        // connect the joint with the body
                        mouseJointWhileDragging.movingBody = selectedBody
    
                        // set the physicsWorld where the joint should be created
                        mouseJointWhileDragging.world = physicsWorld
                    }
                }
    
                onPositionChanged: {
                    // this check is necessary, because the user might also drag when no initial body was selected
                    if (mouseJointWhileDragging && selectedBody) {
                      mouseJointWhileDragging.targetPoint = Qt.point(mouseX, selectedBody.parent.parent.y)
                    }
                }
                onReleased: {
                    // if the user pressed a body initially, remove the created MouseJoint
                    if(selectedBody) {
                        selectedBody = null
                        if (mouseJointWhileDragging)
                            mouseJointWhileDragging.destroy()
                    }
                }
            }

     

    Cheers,

    David

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

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support