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

Ask a Question related to Macromedia Director 3D, Design and Development.

  1. #1

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

    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:

    Code:
    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.
    Unregistered Guest

  2. Similar Questions and Discussions

    1. Must Move Mouse
      All: I am having a problem in Flex with the click event... I click on a panel set with a click that changes the view to another screen which has...
    2. making movieclip move away from mouse
      Anyone care to explain to me how to make a movieclip move away from my mouse or know any tutorials on the web on how to do this? When the...
    3. both mouse buttons down to move model/camera
      Hi Andrew, To detect whether both mouse buttons are depressed, check mouse properties. The mouseDown is for the left button, the rightMouseDown...
    4. Mouse Down, Move Sprite, How?
      To anyone out there, Below is my script of moving a sprite to the right while mouse is down, but instead of move it seems like the entire sprite...
    5. Move mouse cursor to X,Y location and simulate mouse click
      Hi, Just something that slip my mind.. but how do you move the mouse cursor to a sprite member and simulate a mouse click? Any help is...
  3. #2

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

    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:

    Code:
    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.
    hyper is offline Junior Member
    Join Date
    Jan 2011
    Posts
    2

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139