6 Replies Latest reply on Jan 13, 2011 5:28 AM by Dee McDee

    Need help with making the camera move with the mouse (FPS style)

    ollie111

      Hi,

       

      First of all, I'm very new to Director.

      I'm doing a small project which is a game with a first person view and the player can click on objects to pick them up. That's the plan anyway.

       

      I have made a box to act as a character and made the arrow keys move it. Then I've positioned the camera 10 pixels(?) ahead of the character and made the camera a child of the character.

       

      I've also made the camera move with the mouse (with help from an old thread I found on here), but it's far from perfect. Here is the code for all of it:

       

      property pScene, pBox, pPlane, pCharacter, pCamera, xAn, yAn, pMouse


      on beginSprite me

        -- world variable

        pScene = member("3dworld")


        -- reset the world when done

        pScene.resetWorld()


        -- variable for the mouse location

        pMouse = the mouseLoc


        -- x and y angle variables

        xAn = 0

        yAn = 0


        -- run the create functions

        createBox(me)

        createPlane(me)

        createCharacter(me)

        createCamera(me)

      end


      on mouseDown

        pMouse = the mouseLoc

      end


      on exitFrame me

        -- character movement

        -- 125 = down arrow. Move forward

        if keyPressed(125) then pCharacter.translate(0,0,4.5)

        -- 126 = up arrow. Move backward

        if keyPressed(126) then pCharacter.translate(0,0,-4.5)

        -- 123 = left arrow. Move left

        if keyPressed(123) then pCharacter.translate(-1.5,0,0)

        -- 124 = right arrow. Move right

        if keyPressed(124) then pCharacter.translate(1.5,0,0)


        -- camera mouse movement

        dMouse = (pMouse - the mouseLoc) / 100.0

        dx = dMouse.locV*10 -- sensitivity

        dy = dMouse.locH*10 -- sensitivity


        xAn = xAn + dx

        yAn = yAn + dy

        if (xAn > 80) then

          xAn = 80

        end if

        if (xAn < -80) then

          xAn = -80

        end if

        yAnRad = yAn*pi/180.0

        xAnRad = xAn*pi/180.0

        lookAt = pCamera.transform.position+vector(50*sin(-yAnRad),50*tan(xAnRad),-50*cos(-yAnRad))

        pCamera.pointAt(lookAt)


        pMouse = the mouseLoc

      end if

      end


      on createBox me

        -- create a box resource

        boxResource = pScene.newModelResource("BoxResource",#box)

        boxResource.width = 10

        boxResource.height = 10

        boxResource.length = 10


        -- create new model

        pBox = pScene.newModel("Box",boxResource)


        -- assign a texture to all 6 faces

        --pScene.newtexture("box texture",#fromCastMember,member("texture-blue"))

        --pScene.newshader("box shader", #standard)

        --pScene.shader("box shader").texture = pScene.texture("box texture")

        --repeat with i = 1 to pBox.shaderList.count

        --pBox.shaderList[i] = pScene.shader("box shader")

        --end repeat


        -- position box

        pBox.translate(0,-35,0)

      end


      on createPlane me

        -- create a plane resource

        planeResource = pScene.newModelResource("PlaneResource",#plane)

        planeResource.length = 1000

        planeResource.width = 1000


        -- create new model

        pPlane = pScene.newModel("Plane",planeResource)


        -- position plane

        pPlane.translate(0,-40,0)


        -- rotate plane so it is parallel to ground

        pPlane.rotate(90,0,0)

      end


      on createCharacter me

        -- create a box resource

        boxResource = pScene.newModelResource("Character",#box)

        boxResource.width = 10

        boxResource.height = 10

        boxResource.length = 10


        -- create new model

        pCharacter = pScene.newModel("Character",boxResource)


        -- assign a texture to all 6 faces

        --pScene.newtexture("box texture",#fromCastMember,member("texture-blue"))

        --pScene.newshader("box shader", #standard)

        --pScene.shader("box shader").texture = pScene.texture("box texture")

        --repeat with i = 1 to pBox.shaderList.count

        --pBox.shaderList[i] = pScene.shader("box shader")

        --end repeat


        -- position character

        pCharacter.translate(0,-15,205)

      end


      on createCamera me

        -- camera variable

        pCamera = member(1, 1).camera("DefaultView")


        -- camera properties

        pCamera.fieldOfView = 73.7389

        pCamera.projectionAngle = 73.7389

        pCamera.yon = 3.40282346638529e38

        pCamera.hither = 1.0000

        pCamera.rect = rect(0,0, 800,600)


        -- position camera

        --pCamera.transform.position = vector( 0.0000, -20.0000, 500.0000 )


        -- move to 10 infront of the character

        pCamera.transform.position = pCharacter.transform.position + vector(0,0,-10)


        -- lock camera relative to box

        pCharacter.addChild(pCamera)

      end

       

      Hopefully it is simple enough / commented enough to understand. Feel free to try it yourself.

       

      My immediate problem is this: When I move the mouse to the right, for example, the camera turns right, until a certain point where it starts going back to the left, even though I'm moving the mouse to the right.

       

      Any ideas what is causing that? The part of the code most relevant is the "camera mouse movement" between the on exitFrame part.