2 Replies Latest reply on Jun 16, 2010 12:26 AM by sojourner146

    How to correlate movement of a 3D object to a 2D map?

    sojourner146

      I've read that a bitmap texture can be used to limit the movement of a 3D object to specific areas of color but haven't been able to find examples of code to do that.  I've used the following code from "3D for the Web"  to landscape a terrain with trees and flowers but haven't been able to adapt the same general idea to a moving object.

       

      --create a billboard 'tree'

      --have Director scan a lo res (128 pixel) 16 bit map for red pixels

      --same map can be used for replication of many items referencing different specific colors

       

      repeat with x = 0 to 127
          repeat with y = 0 to 127
            test = member("map").image.getPixel(x,y)
            if test = rgb(255,0,0) then
              createTrees 
            end if
           
          end repeat
        end repeat
       
        createTrees

       

      I would like to use a black and white map to confine keyboard control of the movement of a box to specific paths. I can track the movement of the 3D object by the change in the vector values with, "pBoxLoc = gWorld.model("boxl").transform.position",  "put pBoxLoc.x", and "put pBoxLoc.y", but nothing I've tried works to relate those values to point(x,y) on the bitmap so I can test the color of the pixels the object is moving over and stop it or steer it back on course if it touches pixels of the wrong color.  It seems to me that it should be simple but I'm getting nowhere.  Can anybody help with this?

       

      Thanks,

       

      Sue

        • 1. Re: How to correlate movement of a 3D object to a 2D map?
          James Newton, ACP Level 3

          The code you quote searches a 128 x 128 image for red pixels, and calls the Movie Script handler "on createTrees()" if it finds one.  In fact, it calls the handler every time it finds a red pixel, but it doesn't give any information about where the tree should be created.  A call to createTree(x, y) would be more intelligent.

           

          You want something quite different.

           

          You want to move a box around, but only to points where your map is a particular colour.  One solution would be to place the map as a texture on the terrain.  If you are planning to move the box to a particular location, you could first send a ray downwards from that location, to check where it intersects with the terrain.  You could then use the code that you can find >here< to test the color of the pixel in the texture at that point on the terrain.

           

          If the color is the one you are using for the path on your map, then you can move your box to the planned point.

          • 2. Re: How to correlate movement of a 3D object to a 2D map?
            sojourner146 Level 1

            I gave the wrong impression with the code I included earlier in that it was only the part I considered most relevant to the problem and the approach I had taken.  It's from 3D for the Web and the whole of it allows one to landscape a terrain based on image maps very nicely but it's only suited to static situations.  The authors discussed a number of ways of using image maps to limit the motion of a character but didn't provide finished examples.  Image maps seem to me to have several advantages over other forms of collision detection in that one wouldn't have to create extra polys as collision hulls, could easily move the character around features of assorted heights and shapes, and could confine movements to specific paths even on a flat terrain.  I think it's a great idea but I can't get it to work.  I can see that modelsUnderRay is able to bridge the gap between 2D and 3D by its ability to give UvCoord data but I'm confused as to how to use it and I have not been able to find any examples showing specifically how to write the code to do so.  http://homepage.mac.com/nephilim/sw3ddev/uvpicking.html is tantalizing in that he talks about many useful capabilities provided by combining imaging Lingo and modelsUnderRay and provides a copy of your code to make it possible to do so but all the specific examples are done with modelsUnderLoc which seems to me to be fundamentally different.  It has to do with the mouse and camera.  These are things Director is designed to 'know' about as indicated by all of the reserved words that have to do with mouse locations and actions.  With models it's necessary to give all of that info to Director with code - and I'm confounded as to how to go about it.

             

            I've modified your example to approximate what I'd like to do, so far without 'breaking' it.  ModelsUnderLoc elicits a "hit" when the mouse is over the opaque texture of the plane model.  I've got a box 'character' made that can be moved with the arrow keys over the plane model. http://www.elfstonestudio.com/modelsUnderRayUVTest.htm , http://www.elfstonestudio.com/modelsUnderRayUVTest.zipPlease show me how to use modelsUnderRay to get a similar reaction.  I think (hope) I will be able to figure out how to control the motion of the character based on the color or alpha of the texture after that.  I think the solution to this problem would be helpful to one of the other people who recently posted a question as well.  I appreciate the help you've given so far and wish I could have made better use of it but I unfortunately need more guidance.

             

            Thank you,

             

            Sue M.