OBJ / Box Shape

Software: Away3D 4.x

Avatar
mrpinc, Sr. Member
Posted: 05 December 2011 03:06 PM   Total Posts: 119

I’m currently importing a bunch of OBJ files and creating triangle mesh physics seems to work except that performance is not great.  There are several objects that could probably just use bounding box physics but I am running into a couple of problems.

1.  The obj files I export from max retain their positional information (they appear in the correct place)  but I can’t seem to get the actually X,Y,Z coordinates, even when I iterate through all the children.  The result is always 0,0,0

2.  Is there a way to determine the size of the mesh so that the bounding box can be automatically created to match the size of the provide object?

   

Avatar
Fabrice Closier, Administrator
Posted: 05 December 2011 03:26 PM   Total Posts: 1265   [ # 1 ]

try tools.utils.Bounds

 

   

Avatar
mrpinc, Sr. Member
Posted: 05 December 2011 07:07 PM   Total Posts: 119   [ # 2 ]

Fabrice,

Thanks for the quick reply.  Your suggestion seemed to work for the overall shape of the object (great!)  but the position still seems incorrect.  My floor rigidbody is not aligned with my floor mesh, if I try to move the rigid body, then the mesh is moved along with it.  Is there a way to manually set some type of offset?  When I create Convex objects, the positions seem 100% correct.

Thanks again.

 

   

Avatar
Fabrice Closier, Administrator
Posted: 05 December 2011 08:53 PM   Total Posts: 1265   [ # 3 ]

I’m not sure I do get the issue propperly. Do you have some graphics/swf to show the problem?

One thing that happens with obj exporters, is that they offset the objectspace data. hence why you always would have a position 0,0,0. Because obj format describes object not taking in account their scene position. Short version: position of a mesh is added to its own vertices.

What you could do (only if I do get your issue right) is to use MeshHelper.recenter. This will fix the position and set it to the center of the objectspace data. It will not affect the mesh position visually.
That’s what Prefab does standard with these, in an attempt to reproduce the most common cases. (you can desable this option as well).

 

   

Avatar
mrpinc, Sr. Member
Posted: 05 December 2011 09:02 PM   Total Posts: 119   [ # 4 ]

Fabrice, I think that what you are describing is precisely the problem.  I will investigate to see if that is indeed the issue though.

 

   

Avatar
mrpinc, Sr. Member
Posted: 05 December 2011 09:29 PM   Total Posts: 119   [ # 5 ]

So your suggestion didn’t quite work and I’ll explain why.  The Loader object that returns my asset contains all the obj meshes, but as children.  The Loader3D is not a mesh itself, just a container.

So what I do is I add that Loader3D as a child of a new mesh I create.  I then call MeshHelper.recenter() on that mesh but nothing seems to happen.  If I iterate through all the child mesh objects and recenter those,  the sub meshes are all moved to 0,0,0 in world space - the physics bounding box is now correct though.  Here is a little bit of code:

private function onAssetComplete(e:LoaderEvent):void 
{  
 
var l:Loader3D e.currentTarget as Loader3D;
   
 var 
m:Mesh = new Mesh();
   
 
m.addChild(l);
     
 
deliverAsset(m_currentAssetHandler); 
}

private function handleAsset(m:Mesh)
{
      MeshHelper
.recenter); //Does not work

      
setSub(m); //All meshes are now centered in world space

}

private function setSub(msh:ObjectContainer3D):void
{
       
var nc:int msh.numChildren;
       var 
d3d:ObjectContainer3D;
    
 if (
nc 0
 
{
     
for (var i:int 0nci++) 
     
{
         d3d 
msh.getChildAt(i);    
     
         if  (
d3d is Mesh)
         
{      
            MeshHelper
.recenterd3d as Mesh);
         
}
      
        setSub
(d3d);
      
}
  }

 

   

Avatar
mrpinc, Sr. Member
Posted: 05 December 2011 09:30 PM   Total Posts: 119   [ # 6 ]

Sorry for some reason, hitting preview posted - this one should be deleted.

 

   

Avatar
Fabrice Closier, Administrator
Posted: 06 December 2011 10:03 AM   Total Posts: 1265   [ # 7 ]

var loader:Loader3D = new Loader3D();
Loader3D.enableParser(OBJParser);
loader.addEventListener(AssetEvent.MESH_COMPLETE, onMeshReady);
[...rest of loader code]

private function onMeshReady(event:AssetEvent):void
{
var mesh:Mesh = Mesh(event.asset);
MeshHelper.recenter(mesh);
[..]

nothing seems to happen… as said above “It will not affect the mesh position visually.”

 

   

Avatar
mrpinc, Sr. Member
Posted: 06 December 2011 02:10 PM   Total Posts: 119   [ # 8 ]

I tried the above code but the position of the meshes is indeed changed visually. 

Now all meshes are positioned at the origin of the scene and do not retain their position from 3DSMax.

I should also not that the OBJ files I load sometimes contain multiple meshes

 

   

John Brookes, Moderator
Posted: 06 December 2011 02:58 PM   Total Posts: 732   [ # 9 ]

Think fabrice is mistaken.
reCenter will move your mesh to the origin.

It needs
mesh.x = dx;
mesh.y = dy;
mesh.z = dz;

at the end of the applyPosition function to work.

 

   

Avatar
mrpinc, Sr. Member
Posted: 06 December 2011 03:05 PM   Total Posts: 119   [ # 10 ]

Thanks John,

How do I determine those values though?  Currently my mesh is in the correct position yet lists it’s coordinates as 0,0,0.  How do I determine those dx, dy,dz values?

 

   

John Brookes, Moderator
Posted: 06 December 2011 03:12 PM   Total Posts: 732   [ # 11 ]

In meshHelper look for the applyPositions function
replace with this

public static function applyPosition(mesh:Meshdx:Numberdy:Numberdz:Number):void
{
 
var geometry:Geometry mesh.geometry;
 var 
geometries:Vector.<SubGeometry> = geometry.subGeometries;
 var 
numSubGeoms:int geometries.length;
 var 
vertices:Vector.<Number>;
 var 
verticesLengthuint;
 var 
juint;
 var 
subGeom:SubGeometry;
 for (var 
:uint 0i<numSubGeoms; ++i){
   subGeom 
SubGeometry(geometries[i]);
   
vertices subGeom.vertexData;
   
verticesLength vertices.length;
    
   for (
0j<verticesLengthj+=3){
    vertices[j] 
-= dx;
    
vertices[j+1] -= dy;
    
vertices[j+2] -= dz;
   
}
   
   subGeom
.updateVertexData(vertices);
 
}
 mesh
.dx;
 
mesh.dy;
 
mesh.dz;

Or wait for fabrice to reply wink

 

   

Avatar
Fabrice Closier, Administrator
Posted: 06 December 2011 03:32 PM   Total Posts: 1265   [ # 12 ]

If recenter affects the “visual” position on screen then it’s a bug… Will see if i can find time to test/fix this one.

 

   

Avatar
mrpinc, Sr. Member
Posted: 06 December 2011 04:04 PM   Total Posts: 119   [ # 13 ]

Ok so that change to the MeshHelper class definitely helped - the meshes are no longer at origin, but my Physics Shape is still incorrect.  The Size is fine but the position is off.

//At this point the loaded obj file has had all meshes recentered as per the instructions above.

var pShape:AWPCollisionShape;
   
Bounds.getObjectContainerBounds(loadedOBJ);

pShape = new AWPBoxShapeBounds.widthBounds.heightBounds.depth );

var 
b:AWPRigidBody = new AWPRigidBodypShapeloadedOBJmass);

_physicsWorld.addRigidBody); 

 

   

Avatar
Fabrice Closier, Administrator
Posted: 06 December 2011 04:21 PM   Total Posts: 1265   [ # 14 ]

Uploaded a fix.
The recenter has now an optional “keepPosition” set as default.

Let me know how it goes.

 

   

Avatar
mrpinc, Sr. Member
Posted: 06 December 2011 04:22 PM   Total Posts: 119   [ # 15 ]

So I sort of got things to work by changing my code a bit.  The Collision box of the floor matches up if I do the following:

var b:AWPRigidBody = new AWPRigidBodypShapeloader3D.getChildAt(0), mass);   

b.transform loader3D.getChildAt(0).transform

Unfortunately this is not a great solution since some of the objects I will be creating bounding boxes for contain a variety of sub meshes - so getting the first child is not reliable. I’m sure you have better suggestions.

 

 

   
   
‹‹ Gravity problem

X

Away3D Forum

Member Login

Username

Password

Remember_me



X