2 Replies Latest reply on Oct 24, 2010 12:01 PM by philhog73

    Collision detect script = not responding

    philhog73

      Hi, I am currently attempting to make an interactive 3D game with landscape and character made in 3DS Max 2010. I have been following Deans Director Tutorials and I have used the same code slightly altered because I have only one camera and the character is using bonesplayer. This all worked well until I tried to add code from the collision detect 2 tutorial and when I pressed play it would not load. From here I disabled the register for events line and it played minus the actual detection of the mountain landscape, etc. Hope this makes sense. I am a complete newbie and apologize if it doesn't. Any help would be really appreciated.

       

      Working code before adding collision detect script:-

       

      -- create properties for when arrow keys are pressed
      -- properties will either be TRUE or FALSE values
      property pLeftArrow, pRightArrow, pDownArrow, pUpArrow

       

      property p3Dmember -- reference to 3D cast member
      property pSprite -- referebce to the 3D sprite
      property pCharacter -- reference to character in the 3D world

       

      on beginSprite me

       

      -- initiate properties and 3D world
      pSprite = sprite(me.spriteNum)
      p3Dmember = sprite(me.spriteNum).member
      p3Dmember.resetWorld()

       

      pSprite = sprite(me.spriteNum)
      pMember = pSprite.member

       

      pCharacter = p3Dmember.model("hero")

       


      pCharacter.bonesPlayer.playRate = 0

       

      -- we must define pCharacter after we use the
      -- resetWorld() command otherwise this variable
      -- object will be deleted

       

      setUpCamera

       

      end

       

      on setUpCamera

       

      -- create 1 new cameras
      characterCam = p3Dmember.newCamera("characterCam")

       


      -- characterCam settings
      -- set up the camera's vertical viewing angle
      characterCam.fieldOfView = 75

       

      -- position the camera
      characterCam.transform.position = vector(-1250,0,450)

       

      -- set rotation of camera
      characterCam.transform.rotation = vector(90,0,270)

       


      -- set the camera of the sprite to the characterCam
      pSprite.camera = p3Dmember.camera("characterCam")

       

      -- make cameras children of the character model
      pCharacter.addChild(characterCam,#preserveParent)

       


      end

       


      on keyDown

       

      -- check to see which key has been pressed
      -- and set the property relating to that key to TRUE

       

      -- 123 = left arrow key
      if keypressed(123) then pLeftArrow = TRUE

       

      -- 124 = right arrow key
      if keypressed(124) then pRightArrow = TRUE

       

      -- 125 = down arrow key
      if keypressed(125) then pDownArrow = TRUE

       

      -- 126 = up arrow key
      if keypressed(126) then pUpArrow = TRUE

       

      end

       


      on keyUp
      -- when the arrow keys are released,
      -- set the properties to FALSE
      pLeftArrow = FALSE
      pRightArrow = FALSE
      pUpArrow = FALSE
      pDownArrow = FALSE
      pCharacter.bonesPlayer.playRate = 0
      end

       

      on exitFrame
      characterMove
      end

       

      on characterMove

       

      -- if the right arrow is pressed,
      -- rotate the character 5 degrees about the z-axis
      if pRightArrow then
      pCharacter.rotate(0,0,-5)
      end if

       

      --if the left arrow is pressed,
      -- rotate character -5 degrees about the z-axis
      if pLeftArrow then
      pCharacter.rotate(0,0,5)
      end if

       

      -- if the up arrow is pressed,
      -- move the character 5 pixels along the y-axis
      if pUpArrow then
      pCharacter.bonesPlayer.playRate = 2
      pCharacter.translate(15,0,0)
      end if

       

      -- if the down arrow is pressed,
      -- move the character -5 pixels along the y-axis
      if pDownArrow then
      pCharacter.bonesPlayer.playRate = 2
      pDownArrow = FALSE
      pCharacter.translate(-15,0,0)
      end if

       

      end

       

      -------------------------------------------------------------------------------

       

      With added collision detect script:-

       

      -- create properties for when arrow keys are pressed
      -- properties will either be TRUE or FALSE values
      property pLeftArrow, pRightArrow, pDownArrow, pUpArrow

       

      property p3Dmember -- reference to 3D cast member
      property pSprite -- reference to the 3D sprite
      property pCharacter -- reference to character in the 3D world
      property pCharBoundingBox -- reference to character's bounding box

       

      on beginSprite me

       

      -- initiate properties and 3D world
      pSprite = sprite(me.spriteNum)
      p3Dmember = sprite(me.spriteNum).member
      p3Dmember.resetWorld()

       

       

       

      pCharacter = p3Dmember.model("hero")
      -- we must define pCharacter after we use the
      -- resetWorld() command otherwise this variable
      -- object will be deleted

       

      pCharacter.bonesPlayer.playRate = 0

       

      setUpCamera
      createCharacterBoundingBox

       

      -- register the member for regular timeMS events in order to respond to
      -- user input and resolve camera collisions
      -- i.e. after specified time segments activate collisionDetect handler
      p3Dmember.registerForEvent(#timeMS,#collisionDetect,me,1000,10,0)

       

      end

       

      on createCharacterBoundingBox

       

      -- create model resource
      charModRes = p3Dmember.newModelresource("charModRes",#box)
      charModRes.height = 200
      charModRes.width = 200
      charModRes.length = 400

       

      -- create model and reference to it

       

      pCharBoundingBox = p3Dmember.newModel("CharBoundingBox",charModRes)

       

      pCharBoundingBox.worldPosition = pCharacter.worldPosition

       

      pCharacter.addChild(pCharBoundingBox, #preserveworld)

       

      boxShader = p3Dmember.newShader("transparentShad",#standard)
      boxShader.transparent = TRUE

       

      -- set opacity of shader 0 is invisible - only works if transparent prop = TRUE
      boxShader.blend = 50
      pCharBoundingBox.shaderList = p3Dmember.shader("transparentShad")

       

      end

       


      on setUpCamera

       

      -- create 1 new cameras
      characterCam = p3Dmember.newCamera("characterCam")

       


      -- characterCam settings
      -- set up the camera's vertical viewing angle
      characterCam.fieldOfView = 75

       

      -- position the camera
      characterCam.transform.position = vector(-1250,0,450)

       

      -- set rotation of camera
      characterCam.transform.rotation = vector(90,0,270)

       


      -- set the camera of the sprite to the characterCam
      pSprite.camera = p3Dmember.camera("characterCam")

       

      -- make cameras children of the character model
      pCharacter.addChild(characterCam,#preserveParent)

       


      end

       

       

       

      on collisionDetect me

       

      -- create a list to store collision data created by modelsUnderRay
      -- cast ray to left
      collisionList = p3Dmember.modelsUnderRay(pCharBoundingBox.worldPosition, \
      -pCharacter.transform.yAxis,#detailed)

       

      -- if ray picks up models, then activate checkForCollion
      -- handler and send the collisionList as a parameter
      if (collisionList.count) then me.checkForCollision(collisionList[1])

       

      -- cast ray to right
      collisionList = p3Dmember.modelsUnderRay(pCharBoundingBox.worldPosition, \
      pCharacter.transform.yAxis,#detailed)

       

      -- if ray picks up models, then check for collision
      if (collisionList.count) then me.checkForCollision(collisionList[1])

       

      -- cast ray forward
      collisionList = p3Dmember.modelsUnderRay(pCharBoundingBox.worldPosition, \
      pCharacter.transform.xAxis,#detailed)

       

      -- if ray picks up models, then check for collision
      if (collisionList.count) then me.checkForCollision(collisionList[1])

       

      -- cast ray backward
      collisionList = p3Dmember.modelsUnderRay(pCharBoundingBox.worldPosition, \
      -pCharacter.transform.xAxis,#detailed)

       

      -- if ray picks up models, then check for collision
      if (collisionList.count) then me.checkForCollision(collisionList[1])

       

      end

       


      on checkForCollision me, collisData

       

      -- grab the #distance value from the CollisionList
      dist = collisData.distance

       

      -- check if distance is less than width of bounding box
      if (dist < pCharBoundingBox.resource.width/2) then

       

      -- get distance of penetration
      diff = pCharBoundingBox.resource.width/2 - dist

       

      -- calculate vector perpendicular to the wall's surface to move the character
      -- using the #isectNormal property
      tVector = collisData.isectNormal * diff

       

      -- move the character in order to resolve the collision
      pCharacter.translate(tVector,#world)

       

      end if

       

      end

       


      on keyDown

       

      -- check to see which key has been pressed
      -- and set the property relating to that key to TRUE

       

      -- 123 = left arrow key
      if keypressed(123) then pLeftArrow = TRUE

       

      -- 124 = right arrow key
      if keypressed(124) then pRightArrow = TRUE

       

      -- 125 = down arrow key
      if keypressed(125) then pDownArrow = TRUE

       

      -- 126 = up arrow key
      if keypressed(126) then pUpArrow = TRUE

       

      end

       


      on keyUp
      -- when the arrow keys are released,
      -- set the properties to FALSE
      pLeftArrow = FALSE
      pRightArrow = FALSE
      pUpArrow = FALSE
      pDownArrow = FALSE
      pCharacter.bonesPlayer.playRate = 0
      end

       

      on exitFrame
      characterMove
      end

       

      on characterMove

       

      -- if the right arrow is pressed,
      -- rotate the character 5 degrees about the z-axis
      if pRightArrow then
      pCharacter.rotate(0,0,-5)
      end if

       

      --if the left arrow is pressed,
      -- rotate character -5 degrees about the z-axis
      if pLeftArrow then
      pCharacter.rotate(0,0,5)
      end if

       

      -- if the up arrow is pressed,
      -- move the character 5 pixels along the y-axis
      if pUpArrow then
      pCharacter.bonesPlayer.playRate = 2
      pCharacter.translate(15,0,0)
      end if

       

      -- if the down arrow is pressed,
      -- move the character -5 pixels along the y-axis
      if pDownArrow then
      pCharacter.bonesPlayer.playRate = 2
      pDownArrow = FALSE
      pCharacter.translate(-15,0,0)
      end if

       

      end

        • 1. Re: Collision detect script = not responding
          Dean Utian Adobe Community Professional

          Hi Phillip.

           

          Always worth being concise as possible with your problem.


          Problem could be a complex 3D world and too much processing going on with teh modelUnderRay. Try add the following:
          on collisionDetect me 

            tModelList = [p3Dmember.model("model1"), p3Dmember.model("model2")]

            tOptionsList = [#maxNumberOfModels: 2, #levelOfDetail: #detailed, #modelList: tModelList, #maxDistance: 200]

           

            -- cast ray forward

            collisionList = p3Dmember.modelsUnderRay(pCamera.worldPosition,  pCamera.transform.zAxis, tOptionsList)

           

          --

           

          In the above, tModelList specifies all the models you want to test collsion with. You may not need collision detection with everything.

          tOptionsList defines the maximum number of models (based on tModelList) and the maximum distance of the ray. You don’t need an infinitely long ray as you’re only concerned with objects close to your character.

           

          Dean

          1 person found this helpful
          • 2. Re: Collision detect script = not responding
            philhog73 Level 1

            Hi Dean,

             

            Thanks for your quick response.  I have struggled using the code with my script so have tried to concentrate on the key code you gave me first and this has worked quite well.  The character now continues walking once you have pressed the left or right arrow button, but, down to my lack of director knowledge, the walk-cycle continues once I release a finger from the arrow keys.  This is my code at the moment;-

             

            -- create properties for when arrow keys are pressed

            -- properties will either be TRUE or FALSE values

            property pLeftArrow, pRightArrow, pDownArrow, pUpArrow

             

            property p3Dmember  -- reference to 3D cast member

            property pSprite    -- referebce to the 3D sprite

            property pCharacter -- reference to character in the 3D world

             

            property keylist

            property keypresslist

             

            on beginSprite me

             

              -- initiate properties and 3D world

              pSprite = sprite(me.spriteNum)

              p3Dmember = sprite(me.spriteNum).member

              p3Dmember.resetWorld()

             

              pSprite = sprite(me.spriteNum)

              pMember = pSprite.member

             

              --we must define pCharacter after we use the

              --resetWorld() command otherwise this variable

              --object will be deleted

              pCharacter = p3Dmember.model("hero")

             

             

              pCharacter.bonesPlayer.playRate = 0

             

              setUpCamera

             

             

              keylist = []

              keypresslist = []

             

              -- this sets up a list of keycodes to track.

              -- this code covers the arrow keys only, it could be changed to cover a full range of keys

              repeat with i = 123 to 126

                keylist[i] = i   -- this creates a list of keycodes to track

                keypresslist[i] = 0  -- this creates a "last state" list that corresponds to the keycode.

             

                --keypresslist tracks the last known state of that key, so we can tell when a keypress has changed.

             

             

             

              end repeat

             

             

             

            end

             

            on enterFrame me

              -- this runs every frame.

             

              repeat with i = 1 to keylist.count

                inewstate= keypressed( keylist[i] )

             

                if keypresslist[i] <> inewstate then -- this means the key changed status from whether it's up or down

                  if inewstate= 0 then

                    -- they key was released

                    keyLastReleased = keylist[i]

                    if (keyLastReleased=123) then pLeftArrow  = 0 -- 123 = left arrow key

                    if (keyLastReleased=124) then pRightArrow = 0 -- 124 = right arrow key

                    if (keyLastReleased=125) then pDownArrow  = 0 -- 125 = down arrow key

                    if (keyLastReleased=126) then pUpArrow    = 0 -- 126 = up arrow key

             

                  else

                    -- the key was pressed

                    keyLastPressed = keylist[i]

                    if (keyLastPressed=123) then pLeftArrow  = 1 -- 123 = left arrow key

                    if (keyLastPressed=124) then pRightArrow = 1 -- 124 = right arrow key

                    if (keyLastPressed=125) then pDownArrow  = 1 -- 125 = down arrow key

                    if (keyLastPressed=126) then pUpArrow    = 1 -- 126 = up arrow key

             

                  end if

                  keypresslist[i] = inewstate-- update so we remember its new state.

                end if

             

             

             

              end repeat

             

              -- by the time this repeat loop has finished, keypresslist will contain a complete index of what keys

              -- are held down, and which aren't.

              -- Note: most keyboards have a limit on how many keys they'll track simultaneously.

             

             

            end

             

             

             

             

             

            on setUpCamera

             

              -- create 1 new cameras 

              characterCam = p3Dmember.newCamera("characterCam")

             

             

              -- characterCam settings

              -- set up the camera's vertical viewing angle

              characterCam.fieldOfView = 75

             

              -- position the camera

              characterCam.transform.position = vector(-1250,0,450)

             

              -- set rotation of camera

              characterCam.transform.rotation = vector(90,0,270)

             

             

              -- set the camera of the sprite to the characterCam

              pSprite.camera = p3Dmember.camera("characterCam")

             

              -- make cameras children of the character model

              pCharacter.addChild(characterCam,#preserveParent)

             

             

            end

             

             

            on exitFrame

              characterMove

              end 

             

             

            on characterMove

             

              -- if the right arrow is pressed,

              -- rotate the character 5 degrees about the z-axis

              if pRightArrow then

                pCharacter.rotate(0,0,-5)

              end if

             

              --if the left arrow is pressed,

              -- rotate character -5 degrees about the z-axis

              if pLeftArrow then

                pCharacter.rotate(0,0,5)

              end if

             

              -- if the up arrow is pressed,

              -- move the character 5 pixels along the y-axis

              if pUpArrow then

                pCharacter.bonesPlayer.playRate = 2

                pCharacter.translate(15,0,0)

              end if

             

              -- if the down arrow is pressed,

              -- move the character -5 pixels along the y-axis

              if pDownArrow then

                pCharacter.bonesPlayer.playRate = 2

                pDownArrow = FALSE

                pCharacter.translate(-15,0,0)

              end if

             

            end