Skylark wrote:
> Hi Dimitrios,
> 
> 
> > I wrote a function in FFTOceanSurface passing the x,y of a point. That 
> > function finds the corresponding ocean tile and retrives the closest 
> > vertice using the getVertex of OceanTile function. 
> > 
> 
> That's good. We could add another function that would return a weighted 
> average of the closest 4 points, which would be more accurate but also 
> slower.
> 
> Finally we could also add a function that returns a 2D array of heights 
> given an axis-aligned bounding box which is the (x1, y1, x2, y2) of 
> where on the grid your object is. This would allow the collision 
> detection to do what it wants with the points.
> 
> Those 3 functions would flesh out the API for one-way interaction (water 
> -> object).
> 
> As I told Jan, I'm not working on this right now, but will in the near 
> future. Let me know if you get something working, we can share the 
> workload that way :-)
> 
> J-S
> -- 
> ______________________________________________________
> Jean-Sebastien Guay    
> http://www.cm-labs.com/
> http://whitestar02.webhop.org/
> _______________________________________________
> osg-users mailing list
> 
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> 
>  ------------------
> Post generated by Mail2Forum

Hi,
   I tried to write a function during the weekend. It works but the result is 
not so accurate. The floating objects are going up and down but they're not 
completely synchronized with the ocean surface. It seems that they are moving 
on a different phase or something. Here's my code 

Code:

float FFTOceanSurface::getHeightAt(float x, float y) {
    //ocean surface coordinates
    float oceanX, oceanY;
    //value to return
    float heightAverage = 0.0f;

    osg::Vec3f tileOffset;

    //translate x, y to oceanSurface origin coordinates
    oceanX =  -_startPos.x() + x;
    oceanY =   _startPos.y() - y;

    //calculate the corresponding tile on the ocean surface
    unsigned int tile_x = oceanX/_tileResolution;
    unsigned int tile_y = oceanY/_tileResolution ;

    //This might not be corect but I don't want ruin data encapsulation of 
OceanDataType class
    unsigned int frame = ++_oldFrame;

    //Test if the tile is valid 
    if(tile_x >=0 && tile_x < _numTiles && tile_y >=0 && tile_y < _numTiles){

        if (frame >= _NUMFRAMES)
            frame = frame%_NUMFRAMES;

        //calculate the coordinates on the specific tile we're intersted
        tileOffset.x() = oceanX - tile_x*_tileResolution;
        tileOffset.y() = oceanY - tile_y*_tileResolution;

        //Select the tile for the specific frame
        MipmapGeometry* tile = getTile(tile_x,tile_y);
        const std::vector<OceanTile>& curData = _mipmapData[frame];
        const OceanTile& data = curData[ tile->getLevel() ];

        //Calculate the indices of the "closest" vertice on the grid
        unsigned int indexX,indexY;
        indexX = tileOffset.x() / data.getSpacing();
        indexY = tileOffset.y() / data.getSpacing();

        //Pick also few more neighbour vertices to calculate an average
        osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array;
        verts->resize(9);
        (*verts)[0] = data.getVertex(indexX, indexY);

        if(indexX - 2 >0 && indexY - 2 >0){
            (*verts)[1] = data.getVertex(indexX-1, indexY);
            (*verts)[2] = data.getVertex(indexX-2, indexY-2);
            (*verts)[3] = data.getVertex(indexX, indexY-1);
        }
        if(indexX +2< _numVertices && indexY +2< _numVertices){
            (*verts)[4] = data.getVertex(indexX+1, indexY);
            (*verts)[5] = data.getVertex(indexX+2, indexY+2);
            (*verts)[6] = data.getVertex(indexX, indexY+1);
            (*verts)[7] = data.getVertex(indexX-2, indexY+2);
            (*verts)[8] = data.getVertex(indexX+2, indexY-2);
        }


        //Calculate the average
        float heightSum = 0.0f; 
        for(unsigned int i = 0; i < verts->size(); i++){
            heightSum+=(*verts)[i].z();
        }

        if(verts->size() > 0)
            heightAverage = heightSum/verts->size();


    }
    return heightAverage;
}



Don't worry about the large number of vertices to calculate an average I just 
added a few more to make it work better but it is not the case. If you see 
something wrong in my code please let me know. I believe that a missmatching is 
taking place between the object and the ocean surface, maybe I am missing an 
offset or someting
Thanks!

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=17669#17669





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to