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

Forums

OverviewFelgo 3 Support (Qt 5) › MouseJoint – how to use it

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #18360

    Marcin

    How Felgo,

    Have few questions related to MouseJoint, hopefully you will be able to answer them.
    In general I followed docs: https://felgo.com/doc/mousejoint/#example-usage

     

    1. How to create MouseJoint?
    If I follow exactly the docs, so
    MouseArea.onPressed: creates dynamically MouseJoint, set target, set bodyB(exactly in this order)
    MouseArea.onPositionChanged: changes target to new point
    MouseArea.onReleased: destroys MouseJoint
    behavior is fine.
    Is this the only valid way, to create every time MouseJoint dynamically and destroy it quickly on release, and repeat?

    2. Create one MouseJoint and reuse it/control it MouseArea

    Unfortunately I had tons of issues if I tried to create one MouseJoint with targetB set once and change target later in MouseArea.
    Then the behavior is really weird …
    One of the way to mimic this behavior is to follow the docs but change order of things.
    From:

    // set the target point to the current mouse position
    mouseJointWhileDragging.target = Qt.point(mouseX, mouseY)
    
    // set the body to move to the currently selected one
    mouseJointWhileDragging.bodyB = selectedBody

    to:

    // set the body to move to the currently selected one
    mouseJointWhileDragging.bodyB = selectedBody
    
    // set the target point to the current mouse position
    mouseJointWhileDragging.target = Qt.point(mouseX, mouseY)

    3. MouseJoint behavior
    This is probably how MouseJoint should behave, but it “moves” bodyB to selected target BUT keeps the distance between the touch and bodyB fixed.
    This is probably how it should work.
    The side effect is it never moves it to exactly target position but by vector from bodyB position to new coordinates.

    Looks like to make MouseJoint working you need to exacly follow the way described in docs, if you do things in a different order then you will have problems.
    Is this expected?

    #18361

    Marcin

    One thing to clarify.
    I followed docs but my body is not taken from the touch position in the PhysicsWorld, instead it is always the same player body.
    Maybe it matters.
    That’s why for me it’s a little weird that I need to dynamical recreate the whole MouseJoint if my selectedBody is always the same.
    I want to move player even if user doesn’t click exactly on it before dragging.

    #18382

    Alex
    Felgo Team

    Hi Marcin,

    this example is in fact a bit of a special case, we’ll possibly add another example with a static MouseJoint, like this:

    import Felgo 3.0
    import QtQuick 2.0
    
    GameWindow {
    
      EntityManager {
        id: entityManager
      }
    
      Scene {
    
        PhysicsWorld {
          id: physicsWorld
          debugDrawVisible: true
        }
    
        EntityBase {
          id: player
          property alias collider: collider
          width: 40
          height: 40
          BoxCollider {
            id: collider
            width: 40
            height: 40
          }
        }
    
        MouseJoint {
          id: mouseJoint
          dampingRatio: 1
          frequencyHz: 2
          bodyB: player.collider.body
        }
    
        MouseArea {
          anchors.fill: parent
          onPressed: {
            mouseJoint.maxForce = 30000
            mouseJoint.target = Qt.point(mouseX, mouseY)
          }
    
          onPositionChanged: {
            mouseJoint.target = Qt.point(mouseX, mouseY)
          }
    
          onReleased: {
            mouseJoint.maxForce = 0
          }
        }
    
      } // end of Scene
    } // end of GameWindow
    

    The reason for your behavior, and the need for the correct order of commands, is that the example from the docs creates the an empty MouseJoint component, and as soon as a bodyB is set, the joint starts operating. The target should be set before that.

    In my example above, I switch the joint on and off by just setting the maxForce to 0 upon release, we’ll discuss if it makes sense to add a running or enabled property to the joint instead for cleaner on/off toggling.

    Hope this helps!

    Cheers,
    Alex

    #18387

    Marcin

    Hi Alex,
    Yes, makes perfect sense.
    Will test it soon.
    Once you update the documentation, will be much better.

    #18392

    Marcin

    Hi Alex,
    So did dome good testing and I discovered few things.

    1 First, your example works perfectly, thanks.

    2 If you look at point number 3 in my original post(MouseJoint behavior) … this was a bug  which I made 🙂
    Now MouseJoint works as it should, moves entity to target not to some distance from target.
    What I did originally was adding this to player EntityBase(bodyB which was moved):

    x: scene.width/2
    y: scene.height/2

    Looks like this property binding collides with MouseJoint work. I thought MouseWork will override it but, looks like, MousePoint operates on forces so not really 🙂
    If you want to set player, lets say middle of the screen at start, you should put numeric values not like this.

    3. Some docs clarification
    I think I understand a little better why maxForce trick is needed.
    In the docs it says that target, by default, is set to (0, 0) hence it will work asap as possible(bodyB is there and maxForce is not zero)

    4. Docs update?
    Could you please update docs related to MouseJoint.maxForce property to explain that default value is 0?
    I don’t see it there at the moment, at least not in the QtCreator docs for MouseJoint.

    Ok, That was fun(not really ;p) now I can focus on other things.

Viewing 5 posts - 1 through 5 (of 5 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