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

Forums

OverviewFelgo 3 Support (Qt 5) › cameraVPlay question

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13523

    Darren

    Guys,

    For my next trick, I will try to make the camera follow my tank around the screen.  I have achieved this using cameraVPlay.

    When I turn my tank, it rotates around it’s own bottom left corner.  So it appears the cameraVPlay is focussed on the bottom left corner of the tank.  Is it possible to focus the cameraVPlay on the CENTER of the tank?

    Secondly, I want the tank to always appear facing up the screen – so that the whole game scene will rotate around the tank.  Is it possible to make the cameraVPlay rotate and keep the same rotation as the tank?

    Thanks again,

    Darren.

    #13529

    Alex
    Felgo Team

    Hi Darren,

    the camera is focusing the center of the focusedObject by default, so maybe there’s some other issue in your code. I prepared a sample that extends the camera with your requested rotation-lock:

    import Felgo 3.0
    import QtQuick 2.0
    
    GameWindow {
    
      activeScene: gameScene
    
      EntityManager {
        id: entityManager
    
        entityContainer: gameScene.container
      }
    
      Scene {
        id: gameScene
    
        // forward the keyboard input to the TwoAxisController
        Keys.forwardTo: controller
    
        Item {
          id: container
          /*
            THIS IS WHERE THE MAGIC HAPPENS
            We transform the container with a Rotation element in order to be able to set a custom transform origin (the "transformOrigin" property of the Item only allows to use enums for certain edges or the center)
            The custom origin is the center of the player thats why we add half the width.
          */
          transform: Rotation { origin.x: player.x + player.width/2; origin.y: player.y + player.height/2; angle: -player.rotation;}
    
          PhysicsWorld {
            debugDrawVisible: true
          }
    
          // the controllable entity
          EntityBase {
            id: player
    
            x: 100; y: 300
            width: 32; height: 32
    
            // Rotate the player around it's center and not the default 0,0 origin
            transformOrigin: Item.Center
    
            Rectangle {
              anchors.fill: parent
              color: "blue"
            }
            Rectangle {
              color: "red"
              width: parent.height
              height: 5
              anchors.top: parent.top
            }
    
            // this controller helps to move the player
            TwoAxisController {
              id: controller
            }
    
            BoxCollider {
              anchors.fill: parent
    
              // set the velocity, depending on which arrow keys are pressed
              linearVelocity: Qt.point(controller.yAxis * 100 * Math.sin(player.rotation*Math.PI/180), -controller.yAxis * 100 * Math.cos(player.rotation*Math.PI/180))
              angularVelocity: controller.xAxis * 70
            }
          }
    
          // 5 green rectangles to make the player's movement visible
          Rectangle {
            x: 0; y: 0
            width: 32; height: 32
            color: "green"
          }
          Rectangle {
            x: 668; y: 368
            width: 32; height: 32
            color: "green"
          }
          Rectangle {
            x: 668; y: 0
            width: 32; height: 32
            color: "green"
          }
          Rectangle {
            x: 0; y: 368
            width: 32; height: 32
            color: "green"
          }
          Rectangle {
            x: 334; y: 184
            width: 32; height: 32
            color: "green"
          }
        }
    
        CameraVPlay {
          id: camera
    
          // set the gameWindowSize and entityContainer required for the camera
          gameWindowSize: Qt.point(gameScene.gameWindowAnchorItem.width, gameScene.gameWindowAnchorItem.height)
          entityContainer: container
    
          // the camera follows the player
          focusedObject: player
        }
      }
    }
    

    Hope this helps you for now, we’ll have to check if we can add this feature to the camera.

    Cheers,
    Alex

    #13533

    Darren

    Hi Alex,

    So… basically we are using the transform property to rotate the container and keep it at the same rotation as the tank, and just using the camera to keep the view centered on the tank.

    Got it… and whoohoo it works perfectly first time!

    That’s my main problems sorted… for now 😉

    Thanks to you and Gunther – you’ve given me tonnes of help and my game is starting to come together thanks to you both 🙂

    Darren.

    #13534

    Alex
    Felgo Team

    Hi Darren,

    Yeah, we compensate the rotation of the tank by rotating the container in the opposite direction.

    That’s also how the camera component internally works, it basically moves the container in the opposite direction of the tank movement, to keep the tank centered in the screen.

    Glad we could help you 🙂

    Cheers,
    Alex

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