3 Replies Latest reply on Jan 31, 2007 1:32 AM by Nickname_is_none

    3D movement and collision queries

    Nickname_is_none
      I have a 3D box that I want to move a camera around the inside of, using mousedown on sprites. I want the camera to pan left/right, pan up/down, and move in/out (to replicate a zoom).

      I have a bit of script where I set up the 3D environment, and below is the code that I have attached to the 3D member, which is triggered by a left button clicks on each sprite. I've mixed the lingo together from different bits of scripts. The camera pans left/right/up/down, but it doesn't 'dolly' in/out. Can someone show me where I'm going wrong?

      property pGroupName
      property pPanAmount
      property pDollyAmount
      property pAlerted
      property pCamera
      property pCameraOriginTransform
      property pCenter
      property pInitialized
      property pMember
      property pMemberStateOK
      property pQueuedTriggeredEvents
      property pRect
      property pSprite
      property p3dMember
      property pCameraSphere


      on beginSprite(me)

      p3dMember = sprite(me.spriteNum).member
      pSprite = sprite(me.spriteNum)
      pMember = pSprite.member
      pMemberStateOK = me.isMemberStateOK()
      pCamera = pSprite.camera

      if pMemberStateOK then
      pInitialized = me.initialize()
      else
      pInitialized = FALSE
      end if

      p3dMember = sprite(me.spriteNum).member
      pCamera = pSprite.camera

      pAlerted = FALSE

      camSphereRes = p3dMember.newModelResource("camSphereRes",#sphere)
      camSphereRes.radius = 20
      pCameraSphere = p3dMember.newModel("cameraSphere",camSphereRes)
      pCamera.addChild(pCameraSphere,#preserveParent)
      p3dMember.registerForEvent(#timeMS,#controlCamera,me,2000,50,0)

      end beginSprite


      on endSprite(me)

      if pInitialized then
      pCamera.transform = pCameraOriginTransform
      end if

      end endSprite


      on enterFrame(me)

      if pMemberStateOK then

      if pInitialized then

      if pQueuedTriggeredEvents.count > 0 then

      repeat with j = 1 to pQueuedTriggeredEvents.count
      tEvent = pQueuedTriggeredEvents[j]

      case tEvent of
      #panCameraUp:
      me.panCamera(pPanAmount, 0, 0)
      #panCameraDown:
      me.panCamera(-pPanAmount, 0, 0)
      #panCameraLeft:
      me.panCamera(0,pPanAmount, 0)
      #panCameraRight:
      me.panCamera(0,-pPanAmount, 0)
      #dollyCameraIn:
      me.dollyCamera(0,0,-pDollyAmount)
      #dollyCameraOut:
      me.dollyCamera(0,0,pDollyAmount)
      end case

      end repeat

      pQueuedTriggeredEvents = []

      end if

      else
      pInitialized = me.initialize()
      end if

      else
      pMemberStateOK = me.isMemberStateOK()
      end if

      end enterFrame


      on registerEvent(me, aEvent, aTriggerSpriteNum, aTargetBehaviorInstance)

      if pInitialized then
      if me = aTargetBehaviorInstance then

      tHandler = [#panCameraUp: "Pan Camera Up",\
      #panCameraDown: "Pan Camera Down",\
      #panCameraLeft: "Pan Camera Left",\
      #panCameraRight: "Pan Camera Right",\
      #dollyCameraIn: "Move Camera In",\
      #dollyCameraOut: "Move Camera Out"].getOne(aEvent)

      if symbolP(tHandler) then
      pQueuedTriggeredEvents.add(tHandler)
      end if

      end if
      end if

      end registerEvent



      on withGroupNameOf(me, aName, aList)

      if aName = pGroupName then
      if voidP(aList.getAProp(me.spriteNum)) then
      tNewList = [:]
      tNewList.addProp(me.getScriptName(),me)
      aList.addProp(me.spriteNum, tNewList)

      else
      tBehaviorName = me.getScriptName()
      tSpriteBehaviorList = aList.getProp(me.spriteNum)

      if voidP(tSpriteBehaviorList.getAProp(tBehaviorName)) then
      tSpriteBehaviorList.addProp(me.getScriptName(),me)

      else
      nothing
      end if

      end if
      end if

      end withGroupNameOf

      on controlCamera me

      collisionList = p3dMember.modelsUnderRay(pCamera.worldPosition,\
      -pCamera.transform.xAxis,#detailed)

      if (collisionList.count) then
      me.checkForCollision(collisionList[1])
      end if

      collisionList = p3dMember.modelsUnderRay(pCamera.worldPosition,\
      pCamera.transform.xAxis,#detailed)

      if (collisionList.count) then
      me.checkForCollision(collisionList[1])
      end if

      collisionList = p3dMember.modelsUnderRay(pCamera.worldPosition,\
      -pCamera.transform.zAxis,#detailed)

      if (collisionList.count) then
      me.checkForCollision(collisionList[1])
      end if

      collisionList = p3dMember.modelsUnderRay(pCamera.worldPosition,\
      pCamera.transform.zAxis,#detailed)

      if (collisionList.count) then
      me.checkForCollision(collisionList[1])
      end if

      end


      on checkForCollision me, thisData

      dist = thisData.distance

      if (dist < pCameraSphere.resource.radius) then

      diff = pCameraSphere.resource.radius - dist
      tVector = thisData.isectNormal * diff
      pCamera.translate(tVector,#world)

      end if

      end


      on initialize(me)

      pCamera = pSprite.camera
      pCameraOriginTransform = duplicate(pCamera.transform)
      pRect = pSprite.rect
      pCenter = point(pRect.left + (pRect.width / 2.0), pRect.top + (pRect.height / 2.0))
      pQueuedTriggeredEvents = []
      return(TRUE)

      end initialize


      on isMemberStateOK(me)

      tOKstate = FALSE

      case pMember.type of
      #shockwave3d:
      if pMember.state >= 2 then
      tOKstate = TRUE
      end if

      #text:
      if pMember.displayMode = #mode3D then
      if pMember.state >= 2 then
      tOKstate = TRUE
      end if
      else
      if NOT(pAlerted) then
      me.textTypeAlert()
      pAlerted = TRUE
      end if
      end if
      end case

      return(tOKstate)

      end isMemberStateOK


      on panCamera(me, aX, aY, aZ)

      pCamera.rotate(aX, aY, aZ)

      end panCamera


      on dollyCamera(me, aDollyAmount)

      pCamera.translate(0,0, aDollyAmount)

      end dollyCamera


      on textTypeAlert(me)

      tString1 = "The behavior indicated below requires that its text member's display be set to 3D Mode. " & \
      "This sprite's member is not set that way. " & \
      "Please correct this." & RETURN & RETURN
      tString2= "Sprite" & ":" && string(me.spriteNum) & RETURN & RETURN
      tString3 = "Behavior" & ":" && QUOTE & me.getScriptName() & QUOTE
      alert(me.typeset(tString1 & tString2 & tString3))

      end textTypeAlert


      on isOKtoAttach(aScript, aSpriteType, aSpriteNum)

      case aSpriteType of
      #Graphic:
      case sprite(aSpriteNum).member.type of
      #shockwave3d:
      return(TRUE)

      #text:
      if sprite(aSpriteNum).member.displayMode = #mode3D then
      return(TRUE)
      end if
      end case

      #script:
      return(FALSE)
      end case

      end isOKtoAttach


      on getPropertyDescriptionList(aScript)

      if the currentSpriteNum > 0 then

      tGPDList = [:]
      if aScript.is3D(sprite(the currentSpriteNum).member) then

      tGPDList[#pPanAmount] = \
      [#comment:"Degrees to pan per frame",\
      #format: #integer,\
      #default: 2]

      tGPDList[#pDollyAmount] = \
      [#comment:"Amount to dolly per frame",\
      #format:#float,\
      #default: 5.0]

      tGPDList[#pGroupName] = \
      [#comment:"Which group does this behavior belong to?",\
      #format: #string,\
      #default: "Unassigned!" ]

      end if

      return(tGPDList)

      end if

      end getPropertyDescriptionList


      on getScriptName(me)

      tScriptName = string(me.script)
      tName = EMPTY
      repeat with tWord = 2 to tScriptName.word.count
      tName = tName && tScriptName.word[tWord]
      end repeat
      tName = tName.char[3..(tName.char.count) - 2]
      return(tName)

      end getScriptName


      on is3D(me, aMember)

      tIs3D = FALSE

      case aMember.type of
      #shockwave3d:
      tIs3D = TRUE
      #text:
      if aMember.displayMode = #mode3D then
      tIs3D = TRUE
      end if
      end case

      return(tIs3D)

      end is3D


      on typeSet(aScript, aString)

      tCharLimit = 50
      tTempChunk = aString.char[1..tCharLimit]
      tNewString = EMPTY

      repeat while tTempChunk <> EMPTY
      tTempChunk = aString.char[1..tCharLimit]
      tCount = tTempChunk.char.count

      if tTempChunk.char[tCharLimit] = " " then
      tStopWord = tTempChunk.word.count

      else if aString.char[tCharLimit + 1] = " " then
      tStopWord = tTempChunk.word.count

      else if tTempChunk.char.count < tCharLimit then
      tStopWord = tTempChunk.word.count

      else
      tStopWord = tTempChunk.word.count - 1
      end if

      tTempChunk = aString.word[1..tStopWord]

      tNewString = tNewString & RETURN & tTempChunk
      delete aString.word[1..tStopWord]

      end repeat
      return(tNewString)

      end typeSet


      --------------------------------------------------------------------------------
      --beginInterfaceBlock
      --publicHandler = [#panCameraUp: "Pan Camera Up"]
      --publicHandler = [#panCameraDown: "Pan Camera Down"]
      --publicHandler = [#panCameraLeft: "Pan Camera Left"]
      --publicHandler = [#panCameraRight: "Pan Camera Right"]
      --publicHandler = [#dollyCameraIn: "Move Camera In"]
      --publicHandler = [#dollyCameraOut: "Move Camera Out"]
      --respondsTo = [#mouse, #key]
      --respondToOnlyThisSprite = [FALSE]
      --endInterfaceBlock