Forums

OverviewV-Play 2 Support (Qt 5) › Revolute joint problems

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #13291

    Darren

    I am trying to create a tank where the turret will rotate around the base, controlled by a slider.  I have tried to use the revolutejoint to turn the turret, however this has unpredictable results where the turret turns, but the tank base also moves down and across the screen.  Please could you advise how to use the joint correctly and whether this is the best course of action or if I should use another method?

    The below code does not implement any controller, it’s just to try to get the turret turning… HELP!!

     

    import QtQuick 2.0
    import VPlay 2.0
    
    Item {
        id: tankWhole
        x: 120
        y: 200
    
        TankBase{
            id: tankBase
        }
    
        TankTurret{
            id: tankTurret
        }
    
        RevoluteJoint {
          id: revolute
          maxMotorTorque: 300 * 32 * 32
          motorSpeed: 1000
          bodyA: tankBase.body
          bodyB: tankTurret.body
          enableMotor: true 
        }
    
    }

     

    • This topic was modified 2 years, 2 months ago.
    • This topic was modified 2 years, 2 months ago.
    #13294

    Alex
    V-Play Team

    Hi Darren,

    can you post a minimum one-qml-file example that shows your problem? As we can’t see the internals of your tank components, its hard to reproduce it.

    Cheers,
    Alex

    #13323

    Darren

    Hi Alex,

    I’ve posted my tank base and tank turret components below.  They are the components referred to by my code above.  I’ve substituted rectangles for the actual images I use, so you can see the effect. I am trying to get the red rectangle (turret) to rotate, whilst the blue rectangle (tank base) should remain stationary. Currently they both rotate in opposite directions and also drift down the screen.

    The example code I have here just gives an arbitrary value for the motorSpeed – in my game this will be controlled by a slider interface I have created.

    I will also add a separate control using the joystick HUD that actually drives the whole thing around the screen.  I have already done this part, but haven’t included it in the code to make it simpler.

     

    // Tank Turret component
    import QtQuick 2.0
    import VPlay 2.0
    
    EntityBase {
    
        width: 58 // width & height correspond to the image size
        height: 25
    
        property alias body: collider.body
    
        BoxCollider {
            id: collider
            width: parent.width
            height: parent.height
            density: 0.005
            friction: 0.3
            restitution: 0.2
            fixedRotation: false
            sleepingAllowed: false
            bodyType: Body.Dynamic
      }
    
        Rectangle {
            color: "red"
            width: parent.width
            height: parent.height
            anchors.horizontalCenterOffset: 11 //the offsets ensure the tank turret aligns correctly with the base
            anchors.verticalCenterOffset: 4
            anchors.centerIn: parent
        }
    }
    
    // Tank Base Component
    import QtQuick 2.0
    import VPlay 2.0
    
    EntityBase {
    
        width: 63 // width & height correspond to the image size
        height: 34
    
        property alias body: collider.body
    
        BoxCollider {
            id: collider
            width: parent.width
            height: parent.height
            density: 0.005
            friction: 1
            restitution: 0.2
            fixedRotation: false
            sleepingAllowed: false
            bodyType: Body.Dynamic
      }
    
      Rectangle {
          color: "blue"
          width: parent.width
          height: parent.height
          anchors.centerIn: parent
      }
    }

     

     

     

    #13331

    Günther
    V-Play Team

    Hi Darren!

    The reason why your body also moves, is because in physics the rotation also creates a force, which affects the base.
    To remove the base from any physics-based calculations set collisionTestingOnlyMode: true.
    Note: 
    This means that for the body, collisions with other entities are detected, but let’s say if another entity crashes into the body, it won’t move due to the impact.

    Alternatively, you can also play around with the density setting of the colliders. E.g. if you make the turret really light (low density) compared to the base, only the turret will turn.

    Also, if you are not happy with these solutions, you may also set the “rotation base” invisible and configure it as you like. Then add another “tank base” that is visible and has nothing to do with the turret rotation.

     

    Best,
    Günther

     

    • This reply was modified 2 years, 2 months ago by  GT.
    #13333

    Alex
    V-Play Team

    Since I guess it’s most important that the base has collision detection and correct physics, I’d go with either modifying density settings or just use a MovementAnimation for rotating the turret. It uses a velocity for the speed, so this can easily be connected to the slider value.

    Cheers,
    Alex

    #13341

    Darren

    Thank you both for your help.  Here is the result of my experiments today:

    1. I have found I need to make the tank base and tank turret separate entities that are placed in my level.  I cannot make an ‘item’ that contains them both and then place that in the level.  If I try doing that, the box colliders do not position correctly.
    2. Lowering the density of the turret and increasing the density of the tank base did not result in the tank turret rotating freely whilst the base stayed still.  Instead it just meant that more force was required to rotate the joint, and both entities still rotated at the same time and speed.
    3. Placing a third, invisible entity that links and rotates with my turret DID work.

    The next problem was to make the whole tank – base, turret and invisible collider, move around together with the appearance of one item under the control of the player.  I linked the turret and base to a joystickHUD controller.  I gave them both the same density etc.  Unfortunately the result was that the turret flew quickly around the screen, separate from the tank base, which moved as it should.  I expected this result, due to the different sizes of the colliders.

     

    I now intend to try a different solution.  I will create an entity consisting of one box collider (for the base) and two images – the base and the turret.  I will link this entity to the joystickHUD controller and will link the turret image to a moveanimation.  This method does not allow the turret to collide with anything, so I am thinking of creating a separate box collider, outside my tank entity, that I will somehow link to the turret image.

     

    I will update the result of my experiments when I’ve tried it, unless you have any better ideas…

     

    Thank you. :-)

    #13346

    Alex
    V-Play Team

    If you need hit detection for the turret you can still place a collider with collisionTestingOnlyMode in the turret, this collider will then not influence (or be influenced by) any kind of force, but still emits contact signals. With this you could use MovementAnimation and still have collision detection.

    Cheers,
    Alex

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