4 Replies Latest reply on Jan 12, 2007 7:43 AM by Skavengur

    Attaching A Camera To An Object

    davidandrewheath
      I'm currently putting together a demonstration of a user-controlled ball rolling around on a piece of land and I'd really like the camera to follow the ball from a distance (i.e. have the camera move along the same axis as the ball is from it's position in the sky) but am having some trouble doing this. What terminology or syntax should I use for this, and whereabouts in my code should it fit?

      My code is as follows:


      quote:

      global w, hk, rb, vPlaneH, rb2


      on beginSprite me

      createPhysicalObjects()

      end


      on createPhysicalObjects me


      -- ball
      m = w.model("Marble")
      m.addModifier(#meshdeform)
      rb = hk.makeMovableRigidBody(m.name, 100)
      rb.restitution = 2
      rb.friction = 500

      -- ground
      m = w.model("Landscape")
      m.addModifier(#meshdeform)
      rb2 = hk.makeFixedRigidBody(m.name, false)

      -- invisible bottom
      vPlaneMR = w.newModelResource("ground", #box)
      vPlaneMR.height = 10
      vPlaneMR.width = 225
      vPlaneMR.length = 225

      vPFixed = w.newModel("bottom", vPlaneMR)
      vPFixed.transform.position = vector (0, 0, -20)
      vPFixed.transform.rotation = vector (90, 0, 0)
      --vPFixed.visibility = #none
      vPFixed.addModifier (#meshDeform)
      --vPFixed.visibility = #none

      vPFixedH = hk.makeFixedRigidBody ("bottom", true, #box)

      hk.gravity = vector (0, 0, -100)

      end


      on exitFrame me

      -- x axis (RED) point left, y axis (GREEN) points towards the camera and z axis (BLUE) points up

      -- Marble Movement
      if keyPressed (126) then rb.applyImpulse (vector (-300, 0, 0 ))
      else if keyPressed (125) then rb.applyImpulse (vector (300, 0, 0))
      else if keyPressed (123) then rb.applyImpulse (vector (0, 300, 0))
      else if keyPressed (124) then rb.applyImpulse (vector (0, -300, 0))

      end if


      I've managed to transform the camera now into a suitable position from the Marble.

      quote:

      w.camera[1].transform.position = vector (-350, -350, 250)


      This is the perfect starting viewpoint, though I still cannot find the right code for attaching it.
        • 1. Re: Attaching A Camera To An Object
          davidandrewheath Level 1
          Can't anyone help me with this, please?

          I really am tearing my hair out here.
          • 2. Re: Attaching A Camera To An Object
            Skavengur
            I'm actually in a similar position, and you'll probably get more response if you post this in the 3d forum, but...

            Each frame I matched the camera to the position of the ball, moved it back with a translate, and then used poinAt to always look at the ball. This works well enough, unless you want the camera to always follow "behind" the ball no matter what direction your ball is going. You could also set up camera positions throught the scene, and switch to those as requested by the user, using pointAt with in each frame. I've got a question in the 3d forumn you might want to watch and see if anyone comes up with a better idea.

            good luck!
            • 3. Re: Attaching A Camera To An Object
              davidandrewheath Level 1
              Thanks for your reply.

              I've been trying all day to get the thing using the pointAt and it just keeps throwing up errors. I can point at objects I've created using Director/Lingo, but all the objects from the W3D file throw up errors if I try and pointAt them. Would it be too much to ask to request you put a little bit (even a couple of lines) of code together using my variable names etc. to see how it should look?

              Many thanks.
              • 4. Re: Attaching A Camera To An Object
                Skavengur Level 1
                I don't have director in front of me to check the syntax, but try this:

                on exitFrame me

                -- x axis (RED) point left, y axis (GREEN) points towards the camera and z axis (BLUE) points up

                -- Marble Movement
                if keyPressed (126) then rb.applyImpulse (vector (-300, 0, 0 ))
                else if keyPressed (125) then rb.applyImpulse (vector (300, 0, 0))
                else if keyPressed (123) then rb.applyImpulse (vector (0, 300, 0))
                else if keyPressed (124) then rb.applyImpulse (vector (0, -300, 0))

                end if

                --Make camera follow ball (although rotation never changes this way)
                ballTransform = m.getWorldTransform()
                w.camera[1].transform.position = ballTransform.position
                w.camera[1].translate(-350,-350,250,#self)
                lookAtPosition = ballTransform.position
                w.camera[1].pointAt(lookAtPosition)

                end

                It should give you a good a start at least....