17 Replies Latest reply on Jan 14, 2011 12:37 PM by .hyper

    Clicking on objects to pick them up. Need help please

    .hyper

      Hi,

       

      Following on from my other thread: http://forums.adobe.com/thread/775468?tstart=0

       

      I am trying to work out how to make my game so that if you click on objects you can pick them up. I.e. click and hold down the mouse on an object will pick it up, moving the mouse will make the object follow the mouse, letting go of the mouse will drop the object.

       

      I'm using the Havok xtra.

       

      This is what I have right now:

       

      - Created the ground, a box, and a character.

      - Made the arrow keys move the character forward, backward and turn left and right

      - Made the camera a child of the character

       

      Then I've set up the Havok physics:

       

      - Created a Havok Physics Scene media element.

      - Applied a Havok Physics (No HKE) behaviour to the 3D world.

      - Made the box a movable rigid body and the gound a fixed rigid body, like so:

       

       

      -- havok setup (box)

        pHavok = member ("EmptyHavok")

        pHavok.gravity = vector (0, -300, 0)

        pBox.addModifier(#meshdeform)

        pHavok.makeMovableRigidBody(pBox.name, 1000.0, true, #box)

       

       

       

      -- havok setup (ground)

        pHavok = member ("EmptyHavok")

        pHavok.gravity = vector (0, -300, 0)

        pPlane.addModifier(#meshdeform)

        pHavok.makeFixedRigidBody(pPlane.name, true, #box)

       

       

      The physics work, now I need a way to click on the box to pick it up.

       

      What I want to do is exactly the same as in this tutorial: http://www.deansdirectortutorials.com/3D/Havok/

       

      You can see the end result here: http://www.deansdirectortutorials.com/3D/Havok/tutFiles/3DobjectsImportedHKE.dir

       

      But I'm using director created objects, and No HKE. I can't get it working, when I try to apply the Mouse Down behaviour it doesn't let me (any idea why?)

       

      Can I use the code inside the mouse down behaviour to do this?

       

      Need some advice please. Any help is much appreciated.

        • 1. Re: Clicking on objects to pick them up. Need help please
          Dee McDee Level 2

          Havok came with a full complement of library scripts that were very helpful to newbies (we were all newbies once).

           

          The libraries are in the downloads section here:
          http://director-online.com/havok/

           

          (You have to extract them into Configuration\Libs\Havok [relative to Director.exe])

           

          In the following order, you should drag the following scripts on to your 3D sprite and set their parameters:

           

          Havok Physics (No HKE) (to create the physics scene) [from Havok --> Setup]
          Make Fixed Rigid Body (to create the ground rigid body) [from Havok --> Setup]
          Make Movable Rigid Body (to create the box rigid body) [from Havok --> Setup]
          Move Model (to enable the box to be picked up) [from Havok --> Control]
          Mouse Left (trigger for the previous behavior) [from 3D --> Triggers]

           

          I hope that helps.

          1 person found this helpful
          • 2. Re: Clicking on objects to pick them up. Need help please
            .hyper Level 1

            Hi again Dee,

             

            Thanks for the reply. I made a mistake, I forgot to add the Move Model behaviour. That would explain why I couldn't add the Mouse Left behaviour.

             

            It works now I'm still using the code for making the objects fixed / movable rigid bodies instead of the behaviours.

             

            I think I'll keep it that way as this is a university assignment I expect I'll get more marks for writing the code instead of using behaviours.

             

            Next I want to try and make it so if you right click while holding an object, it throws the object.

             

            I'll try it myself, but I may post back if I need help.

             

            Thanks again.

             

            EDIT: One thing I've noticed; I can drag the box through the ground, there is some resistance but it still goes through. Any idea how I can stop this?

            • 3. Re: Clicking on objects to pick them up. Need help please
              Dee McDee Level 2

              The only thing you can easily do is increase the collision tolerance. In Havok, it is only possible to set a scene collision tolerance, and not a tolerance for each rigid body as you can with Ageia.

              1 person found this helpful
              • 4. Re: Clicking on objects to pick them up. Need help please
                .hyper Level 1

                Ah ok, do you mean the tolerance setting of the Havok Physics (No HKE) behaviour?

                 

                I set that to 1 and that actually fixed one of the other problems I was having which was the collision box between by objects (I have added a second box) was too big so there was an invisible gap, but when set and 1 it's fine. The objects still go through the floor, although it's harder when set to 1, do I have to find a balance?

                 

                Oh and one more thing, do you know how I can change the properties of an object like mass and friction, with code? For example pBox.mass= 5

                • 5. Re: Clicking on objects to pick them up. Need help please
                  Dee McDee Level 2

                  Yes. You could also reduce the strength of the mouse spring (the stronger the spring, the easier it is to defeat a rigid body's tolerance).

                   

                  To hide the tolerance gap, it is possible to use a smaller box as a proxy and parent the main box to the proxy box (the main box having no physics).

                  • 6. Re: Clicking on objects to pick them up. Need help please
                    .hyper Level 1

                    Sorry where is the mouse spring setting?

                    • 7. Re: Clicking on objects to pick them up. Need help please
                      Dee McDee Level 2

                      In the user-defined parameters for the "Move Model" behavior.

                       

                      (Select 3D sprite --> Property Inspector --> Select "Move Model" behavior --> Click on cog button)

                       

                      http://www.deansdirectortutorials.com/3D/Havok/images/MoveModel.gif

                      • 8. Re: Clicking on objects to pick them up. Need help please
                        .hyper Level 1

                        Ah got it thanks. Even when set to 1 I can still pull them through the floor though.

                        • 9. Re: Clicking on objects to pick them up. Need help please
                          Dee McDee Level 2

                          You could also try increasing the number of substeps (parameter of Havok Physics (No HKE) behavior).

                          • 10. Re: Clicking on objects to pick them up. Need help please
                            .hyper Level 1

                            That helps a bit too I think, but still not completely.

                             

                            I think I need to do what you said with the creating a smaller box inside to use for the collisions and then I can increase the tolerance a lot more without there being a gap. Which will hopefully solve the problem of objects falling through the ground.

                             

                            I have tried this, but now I have a new problem, I can't click on the box inside (we'll call it hitbox) to pick it up since the main box is bigger. If the hitbox is bigger or the same size as the main box I can pick it up. Any idea how I can still pick it up when the hitbox is smaller? because it needs to be or it's pointless.

                             

                            I've also made a sphere and that seems to fall through the floor all the time, sometimes without even dragging it through the floor with the mouse. Quite annoying :/

                            • 11. Re: Clicking on objects to pick them up. Need help please
                              Dee McDee Level 2

                              Since Director MX 2004 it has been possible to limit raycasting to a model list. You can use this ensure that nothing else is raycast besides the proxy box. Edit the mouseDown handler in the Move Model script to the following:

                               

                              on mouseDown(me)


                               
                                if pModel = "Any model" then   
                                  pClickedModel = pSprite.camera.modelUnderLoc(the mouseLoc - point(pSprite.left, pSprite.top))
                                else   
                                  tOptionsList = [#levelOfDetail: #simple, #modelList:[pMember.model(pModel)]]
                                  tClickedModels = pSprite.camera.modelsUnderLoc(the mouseLoc - point(pSprite.left, pSprite.top), tOptionsList)   
                                  if tClickedModels.count then
                                    pClickedModel = tClickedModels[1]
                                  else
                                    pClickedModel = VOID
                                  end if
                                end if
                               
                              end mouseDown

                               

                              Make sure that the proxy box is selected in the user-defined parameters in the Move Model behavior (not any model).
                              • 12. Re: Clicking on objects to pick them up. Need help please
                                .hyper Level 1

                                Thanks, that worked.

                                 

                                But I have more than one object and wouldn't I need to create a proxy for each one? If so, how do I make the Move Model behaviour affect more than 1 model, but not any?

                                 

                                Or is there a way to use the same proxy for multiple models? (I will probably need another one for the sphere object anyway)

                                 

                                Also, I've used the Push Model with the Mouse Right trigger to "throw" the objects. I'll need a similar thing to the Move Model one I guess, can I use the same code? There is no mouseDown handler in that one though.

                                • 13. Re: Clicking on objects to pick them up. Need help please
                                  Dee McDee Level 2

                                  I wouldn't ordinarily recommend this, but you could go back to using Any model and hard-code the modelList so that only models that you wish to pick up are raycast:

                                   

                                  on mouseDown(me)

                                   

                                    -- add your movable models here


                                    tModelList = [pMember.model("proxy box"), pMember.model("proxy sphere")]

                                   

                                    tOptionsList = [#levelOfDetail: #simple, #modelList: tModelList, #maxNumberOfModels: 1]

                                   

                                    tClickedModels = pSprite.camera.modelsUnderLoc(the mouseLoc - point(pSprite.left, pSprite.top), tOptionsList)  
                                    if tClickedModels.count then
                                      pClickedModel = tClickedModels[1]
                                    else
                                      pClickedModel = VOID
                                    end if

                                  end mouseDown
                                  • 14. Re: Clicking on objects to pick them up. Need help please
                                    Dee McDee Level 2

                                    To throw the objects using the right mouse button, drag the following library scripts on to your 3D sprite and set their parameters:

                                     

                                    Push Model [from Havok --> Control]
                                    Mouse Right [from 3D --> Triggers]

                                     

                                    To prevent button conflicts, you need to remove the rightMouseDown and rightMouseUp handlers from the Move Model script. Then you need you remove the mouseDown and mouseUp handlers from the Push Model script.

                                     

                                    In the Push Model script, edit the rightMouseDown handler to the following:

                                     

                                    on rightMouseDown(me)
                                     
                                      -- add your movable models here 
                                      tModelList = [pMember.model("proxy box"), pMember.model("proxy sphere")]
                                     
                                      tOptionsList = [#levelOfDetail: #simple, #modelList: tModelList, #maxNumberOfModels: 1]
                                     
                                      tClickedModels = pSprite.camera.modelsUnderLoc(the mouseLoc - point(pSprite.left, pSprite.top), tOptionsList) 
                                      if tClickedModels.count then
                                        pClickedModel = tClickedModels[1]
                                      else
                                        pClickedModel = VOID
                                      end if
                                     
                                    end rightMouseDown

                                     


                                    Then edit the rightMouseUp handler to the following:

                                     

                                    on rightMouseUp(me)
                                     
                                      pClickedModel = 0
                                      pAttached = false
                                     
                                    end rightMouseUp

                                     

                                    And hopefully that should solve your problems.

                                    • 15. Re: Clicking on objects to pick them up. Need help please
                                      .hyper Level 1

                                      I changed the Move Model back to Any model and added that mouseDown handler into the script, like so:

                                       

                                      on mouseDown(me)

                                       

                                        tModelList = [pMember.model("pBoxHitBox1"), pMember.model("pBoxHitBox2")]

                                       

                                        tOptionsList = [#levelOfDetail: #simple, #modelList: tModelList, #maxNumberOfModels: 1]

                                       

                                        tClickedModels = pSprite.camera.modelsUnderLoc(the mouseLoc - point(pSprite.left, pSprite.top), tOptionsList)  

                                        if tClickedModels.count then

                                          pClickedModel = tClickedModels[1]

                                        else

                                          pClickedModel = VOID

                                        end if

                                       

                                      end mouseDown

                                       

                                      It gives me an error when I run the game and click anywhere on the screen: it points to this line: tClickedModels = pSprite.camera.modelsUnderLoc(the mouseLoc - point(pSprite.left, pSprite.top), tOptionsList)  

                                       

                                      "Script error: Wrong type. #modelsUnderLoc"

                                       

                                      Did you make an error in the code?

                                       

                                      I haven't tried the push model/right mouse stuff yet.

                                       

                                      Thanks for all your help by the way, you've been a great help.

                                      • 16. Re: Clicking on objects to pick them up. Need help please
                                        Dee McDee Level 2

                                        The type of error that you are describing is indicative of an incorrect model list (all items on the list must be valid models in the 3D world).

                                         

                                        If pBoxHitBox1 and pBoxHitBox2 are references to models (rather than strings), all you need is:

                                         

                                        tModelList = [pBoxHitBox1, pBoxHitBox2]

                                        • 17. Re: Clicking on objects to pick them up. Need help please
                                          .hyper Level 1

                                          Ah whoops. It should have been

                                           

                                          "tModelList = [pMember.model("BoxHitBox1"), pMember.model("BoxHitBox2")]"

                                           

                                          instead of

                                           

                                          "tModelList = [pMember.model("pBoxHitBox1"), pMember.model("pBoxHitBox2")]"

                                           

                                          All working now, Push working as well.

                                           

                                          And that has fixed my problem of things going through the floor. Took a while to tweak the tolerance and substeps settings to get them balanced between realism, no gap between objects and things not going through the floor.

                                           

                                          Thanks a lot.