This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

Changeset 9988 for ps


Ignore:
Timestamp:
08/11/11 00:45:55 (13 years ago)
Author:
ben
Message:

Changes GetFocus to return points above water.
Fixes camera following of boats.

Location:
ps/trunk/source/graphics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/source/graphics/Camera.cpp

    r9814 r9988  
    287287}
    288288
    289 CVector3D CCamera::GetFocus()
     289CVector3D CCamera::GetFocus() const
    290290{
    291291    // Basically the same as GetWorldCoordinates
     
    294294    int x, z;
    295295
    296     CVector3D origin, dir, delta, currentTarget;
     296    CVector3D origin, dir, delta, terrainPoint, waterPoint;
    297297
    298298    origin = m_Orientation.GetTranslation();
    299299    dir = m_Orientation.GetIn();
    300300
    301     if (tracer.RayIntersect(origin, dir, x, z, currentTarget))
    302         return (currentTarget);
    303 
    304     // Off the edge of the world?
    305     // Work out where it /would/ hit, if the map were extended out to infinity with average height.
    306 
    307     return (origin + dir * ((50.0f - origin.Y) / dir.Y));
     301    bool gotTerrain = tracer.RayIntersect(origin, dir, x, z, terrainPoint);
     302
     303    CPlane plane;
     304    plane.Set(CVector3D(0.f, 1.f, 0.f),                                     // upwards normal
     305        CVector3D(0.f, g_Renderer.GetWaterManager()->m_WaterHeight, 0.f));  // passes through water plane
     306
     307    bool gotWater = plane.FindRayIntersection( origin, dir, &waterPoint );
     308
     309    // Clamp the water intersection to within the map's bounds, so that
     310    // we'll always return a valid position on the map
     311    ssize_t mapSize = g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide();
     312    if (gotWater)
     313    {
     314        waterPoint.X = clamp(waterPoint.X, 0.f, (float)((mapSize-1)*CELL_SIZE));
     315        waterPoint.Z = clamp(waterPoint.Z, 0.f, (float)((mapSize-1)*CELL_SIZE));
     316    }
     317
     318    if (gotTerrain)
     319    {
     320        if (gotWater)
     321        {
     322            // Intersecting both heightmap and water plane; choose the closest of those
     323            if ((origin - terrainPoint).LengthSquared() < (origin - waterPoint).LengthSquared())
     324                return terrainPoint;
     325            else
     326                return waterPoint;
     327        }
     328        else
     329        {
     330            // Intersecting heightmap but parallel to water plane
     331            return terrainPoint;
     332        }
     333    }
     334    else
     335    {
     336        if (gotWater)
     337        {
     338            // Only intersecting water plane
     339            return waterPoint;
     340        }
     341        else
     342        {
     343            // Not intersecting terrain or water; just return 0,0,0.
     344            return CVector3D(0.f, 0.f, 0.f);
     345        }
     346    }
    308347}
    309348
  • ps/trunk/source/graphics/Camera.h

    r9814 r9988  
    8888        // Get the point on the plane at height h corresponding to pixel (px,py)
    8989        CVector3D GetWorldCoordinates(int px, int py, float h) const;
    90         // Get the point on the terrain the camera is pointing towards
    91         CVector3D GetFocus();
     90        // Get the point on the terrain (or water plane) the camera is pointing towards
     91        CVector3D GetFocus() const;
    9292
    9393        // Build an orientation matrix from camera position, camera focus point, and up-vector
Note: See TracChangeset for help on using the changeset viewer.