2 Replies Latest reply on Jan 11, 2010 10:56 AM by tyree_2

    Events Triggered By Collision Detection In Shockwave 3D Environment Happen More Than Once?

    Joe Spinebuster

      Okay, I've finally managed to get my Director 3D game to move an object to a new random position when collision detection occurs with that object! HOWEVER, sometimes it will work as expected and other times the object seems to move SEVERAL times before settling. Anyone have any idea why it isn't always just moving once and settling as I'd hoped it would...?

      To see an example of the problem I'm facing, create a new Director movie then insert a new Shockwave 3D media element into that Director movie named "Scene" and apply the below code to that 3D media element. I've tried to trim the code down as much as possible to get right to the heart of the problem:
      =====

      property Scene  -- reference to 3D cast member
      property pSprite    -- referebce to the 3D sprite
      property pCharacter -- reference to the yellow in the 3D world
      property pTransformgreen01Flag -- reference to the flag which will trigger action to move green01 + increase score on collision
      property pTransformgreen02Flag -- reference to the flag which will trigger action to move green02 + increase score on collision

      on beginSprite me
       
        -- initiate properties and 3D world
        pSprite = sprite(me.spriteNum)
        Global Scene
        Scene = sprite(me.spriteNum).member
        Scene.resetWorld()
       
        Tempobjres = Scene.newmodelresource("sphereRES", #sphere)  --create sphere modelresource
        Tempobjres.radius = 10     --set sphere modelresrource radius to 20
        Tempobjres.resolution = 17   --set the sphere modelresource resolution to 17
        Tempobj = scene.newmodel("yellow", tempobjres)   --create a model from the sphere modelresource
        Tempshader = scene.newshader("sphereshd", #standard)   --create a new standard shader
        Tempshader.diffuse = rgb(250,250,10)   --set the diffuse color (in this case, a light green)
        Tempshader.texture = void   --set the texture of the shader to void
        Tempobj.transform.position=vector(0,0,0)
        Tempobj.shaderlist = tempshader   -- assign the shader to the model
       
        Tempobjres1 = Scene.newmodelresource("sphereRES1", #sphere)  --create sphere modelresource
        Tempobjres1.radius = 10     --set sphere modelresrource radius to 20
        Tempobjres1.resolution = 17   --set the sphere modelresource resolution to 17
        Tempobj1 = scene.newmodel("green01", tempobjres1)   --create a model from the sphere modelresource
        Tempshader1 = scene.newshader("sphereshd1", #standard)   --create a new standard shader
        Tempshader1.diffuse = rgb(100,200,50)   --set the diffuse color (in this case, a light green)
        Tempshader1.texture = void   --set the texture of the shader to void
        Tempobj1.transform.position = vector(25,25,0)
        Tempobj1.shaderlist = tempshader1   -- assign the shader to the model
       
        Tempobjres2 = Scene.newmodelresource("sphereRES2", #sphere)  --create sphere modelresource
        Tempobjres2.radius = 10     --set sphere modelresrource radius to 20
        Tempobjres2.resolution = 17   --set the sphere modelresource resolution to 17
        Tempobj2 = scene.newmodel("green02", tempobjres2)   --create a model from the sphere modelresource
        Tempshader2 = scene.newshader("sphereshd2", #standard)   --create a new standard shader
        Tempshader2.diffuse = rgb(100,200,50)   --set the diffuse color (in this case, a light green)
        Tempshader2.texture = void   --set the texture of the shader to void
        Tempobj2.transform.position = vector(-25,-25,0)
        Tempobj2.shaderlist = tempshader2   -- assign the shader to the model
       
        --the following lines will add collision detection for all three on-screen 3D objects
        Tempobj.addModifier(#collision)
        Tempobj.collision.enabled=true
        Tempobj1.addModifier(#collision)
        Tempobj1.collision.enabled=true
        Tempobj2.addModifier(#collision)
        Tempobj2.collision.enabled=true
        Tempobj.collision.resolve=true
        Tempobj1.collision.resolve=true
        Tempobj2.collision.resolve=true
       
        --the following lines will tell Director what to do with a specific object when it collides with another
        Tempobj.collision.setCollisionCallBack(#beepsound, me)
        Tempobj1.collision.setCollisionCallBack(#hitgreen01, me)
        Tempobj2.collision.setCollisionCallBack(#hitgreen02, me)
       
        pCharacter = Scene.model("yellow")
        -- we must define pCharacter after we use the resetWorld() command
        -- otherwise this variable object will be deleted
       
        createCollisionDetect
        pTransformgreen01Flag = false
        pTransformgreen02Flag = false
       
      end


      on createCollisionDetect
        Global Scene
        -- add collision modifier to the character
        pCharacter.addmodifier(#collision)
        -- set bounding geometry for collision detection to bounding box of model
        pCharacter.collision.mode = #mesh
        -- resolve collision for character
        pCharacter.collision.resolve = TRUE
      end


      on keyDown
        Global Scene
        case(chartonum(the keypressed)) of
          30: --up arrow
            scene.model("yellow").translate(0,5,0)
          31: --down arrow
            scene.model("yellow").translate(0,-5,0)
          28: --left arrow
            scene.model("yellow").translate(-5,0,0)
          29: --right arrow
            scene.model("yellow").translate(5,0,0)
        end case
      end


      --when "yellow" (player character) hits another object, this will happen:
      on beepsound me, colData
        beep --plays sound
      end


      --when "green01" is hit by another object, this will happen:
      on hitgreen01 me, colData
        Global Scene
        pTransformgreen01Flag=true
      end


      --when "green02" is hit by another object, this will happen:
      on hitgreen02 me, colData
        Global Scene
        pTransformgreen02Flag=true 
      end


      on enterFrame me
        Global Scene
        if pTransformgreen01Flag then
          --the following lines will generate new random x and y co-ordinates for green01
          randomx=random(-110,110)
          green01x=randomx
          randomy=random(-90,90)
          green01y=randomy
          Scene = member("Scene")
          Scene.model("green01").transform.position = vector(green01x,green01y,0)
          pTransformgreen01Flag = false
        end if
        if pTransformgreen02Flag then
          --the following lines will generate new random x and y co-ordinates for green02
          randomx=random(-110,110)
          green02x=randomx
          randomy=random(-90,90)
          green02y=randomy
          Scene = member("Scene")
          Scene.model("green02").transform.position = vector(green02x,green02y,0)
          pTransformgreen02Flag = false
        end if
      end

      =====

      I imagine the part that's causing the issue is the "on enterFrame me" part at the end, but can't see any reason why it wouldn't just perform the desired action ONCE every time...?

      This is really confusing the hell out of me and is pretty much the final hurdle before I can call my game "finished" so any and all assistance would be GREATLY appreciated!