This technic works, but there are 3 params that could influence the result, say offset it. If you are into another quadrant, so basically going from plus to min can generate an offset. Here for instance, depending on the rotationX of the elevation you might need to pass negative z position value to retreive the right coordinate. Another issue is the size of your bitmap. be sure to add the scale to your calculation if you're not 1/1.

There is also another way to retreive your "2d" position, and not even using this class. just consider a 2d plan. For instance looking from top, so there z and y would be your "x and y" you can read the projected vertices from top, and just add their scenePosition as offset. if you need to access the bitmapMaterial from there, just translate the 0/1 based uv's to numbers.

Here is a sample of code from the Green Planet demo.
this is the part where the little aliens get updated on behavior and shadow using the same technic as you describe.

This is not a very obvious piece of code, but this is the only working example I have on this. Just note the shadow update part (in bold), the factor used for the elevation, the scale factor of the source and the quadrant correction are in the game here...

I hope it will help a bit...

Fabrice

private function updateAliens(campos:Number3D):void
                {
                        var nowtime:Number = getTimer();
                        var limit:Boolean = false;
                        for(var i:int = 0;i<12; i++){
                                
                                var posialien:Number3D = 
oAliens["alien"+i].object.position;
                                 if(campos.distance(posialien) <= 13200){
                                
                                  if(oAliens["alien"+i].sequence == "stand" ){
if(blow || nowtime - oAliens["alien"+i].starttime > oAliens["alien"+i].endtime){
                                                oAliens["alien"+i].sequence = 
"run";
                                                oAliens["alien"+i].starttime = 
nowtime;
                                                oAliens["alien"+i].endtime = 
Math.random()*20000+2000;
oAliens["alien"+i].object.play(new AnimationSequence("run", true, true, 5));
                                          }
                                        
                                  }else{
                                        
if(oAliens["alien"+i].sequence == "run" && (nowtime - oAliens["alien"+i].starttime) > oAliens["alien"+i].endtime){
                                                oAliens["alien"+i].sequence = 
"stand";
                                                oAliens["alien"+i].starttime = 
nowtime;
                                                oAliens["alien"+i].endtime = 
Math.random()*5000+1000;
oAliens["alien"+i].object.play(new AnimationSequence("stand", true, true, 5));
                                                
                                         } else{
                                                //lets first see if we are in 
bounds
var color = collisionmap.getColorAt(oAliens["alien"+i].object.x, -oAliens["alien"+i].object.z); if( (color == 0x663300 || color == 0xFF0000 || color ==0x00CC00) || Math.abs(oAliens["alien"+i].object.x) >= 13180 || Math.abs(oAliens["alien"+i].object.z) >= 13180) {
                                                        
oAliens["alien"+i].object.moveBackward(70);
                                                        
oAliens["alien"+i].object.rotationY += 65;
                                                }
                                                
                                                
oAliens["alien"+i].object.rotationY += 90;
                                                
if(Math.abs(oAliens["alien"+i].object.x) >= 12000 || Math.abs(oAliens["alien"+i].object.z) >= 12000){
                                                        
oAliens["alien"+i].object.rotationY += 3;
                                                }else{
                                                        
oAliens["alien"+i].object.rotationY += -6+Math.random()*12;
                                                }
                                                
oAliens["alien"+i].object.moveForward((oAliens["alien"+i].sequence == "jump")? tb.timeVal(26) : tb.timeVal(16)); oAliens["alien"+i].object.y = elevationreader.getLevel(oAliens["alien"+i].object.x, - oAliens["alien"+i].object.z, 33);
                                                
oAliens["alien"+i].object.rotationY -= 90;
                                                
if(blow && (Math.abs(oAliens["alien"+i].object.x) <= 9000 && Math.abs(oAliens["alien"+i].object.z) <= 9000)){
                                                        
oAliens["alien"+i].object.x += Math.sin(windX)*10;
                                                        
oAliens["alien"+i].object.z += Math.sin(windZ)*20;
                                                }
                                                
                                                //set back old terrain
                                                pt.x = oAliens["alien"+i].x;
                                                pt.y = oAliens["alien"+i].z;
decorsource.copyPixels(oAliens["alien"+i].terrainsource, oAliens["alien"+i].terrainsource.rect, pt);
                                                
                                                //shadows
                                                pt.x = 
(oAliens["alien"+i].object.x)/factor;
                                                pt.y = 
-((oAliens["alien"+i].object.z)/factor);
                                                //original is blown up 2.5
                                                pt.x *= 2.5;
                                                pt.y *= 2.5;
                                                //add center since geometry is 
centered
                                                pt.x += (decorsource.width*.5);
                                                pt.y += (decorsource.height*.5);
                                                
                                                //copy actual terrain
                                                rectshade.x = pt.x;
                                                rectshade.y = pt.y;
oAliens["alien"+i].terrainsource.copyPixels(decorsource, rectshade, zeropt);
                                                
                                                //apply shadow
decorsource.copyPixels(oAliens["alien"+i].sourceshadow, oAliens["alien"+i].sourceshadow.rect, pt, oAliens["alien"+i].sourceshadow, zeropt, true);
                                                
                                                //save new coordinates
                                                oAliens["alien"+i].x = pt.x;
                                                oAliens["alien"+i].z = pt.y;
                                                
                                         }
                                        
                                  }
                                        
                                }
                                
                        }
                }


On Sep 3, 2008, at 2:34 AM, Ari Braginsky wrote:


I'm having an issue retrieving 2D coordinates off of a mesh based on a
3D object's position on the mesh.

You can normally retrieve the 2D coordinates on the bitmap making up
the mesh by dividing the coordinates of the object by the mesh's X and
Y scale values (see extrusions/CollisionMap.as class).

However, if you look at the position on the mesh and compare it to the
raw bitmap, due to some deformations that happen to the mesh with
certain subdivision values, the objects that appear in the bitmap
don't line up with the 2D coordinates on the raw bitmap.  If you tweak
the subdivision values, the objects on the bitmap in the mesh move
around/get squashed/etc.

Example:

Create a bitmap with a circle in the center filled with the color red.

Create an elevation using a flat height map and use the bitmap for its
skin.

When rendered in 3D, you will see the red circle in the center of the
mesh.  If you move your mouse over it, you can translate the 3D
coordinates into 2D space and divide them by the mesh's X and Y scale
values to get the real coordinates on the raw bitmap.

What I'm seeing with subdivision values X = 200 and y = 200 is that my
point on the mesh doesn't correlate to the same point on the 2D bitmap
when I convert the 3D coordinates to 2D, taking into account the mesh
scale values.

Is there something else I need to do to account for the subdivision
values?

If I didn't want any deformation of the mesh, are there subdivision
values I can use such that 3D points on the mesh would exactly match
up with 2D points on the associated bitmap?

Thanks,
Ari


Reply via email to