Camera raycast

c++11
scrawl 10 years ago
parent 2869424847
commit bed31996c9

@ -7,6 +7,7 @@
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
#include <BulletCollision/CollisionShapes/btConeShape.h>
#include <BulletCollision/CollisionShapes/btSphereShape.h>
#include <BulletCollision/CollisionShapes/btStaticPlaneShape.h>
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
@ -778,6 +779,30 @@ namespace MWPhysics
return result;
}
PhysicsSystem::RayResult PhysicsSystem::castSphere(const osg::Vec3f &from, const osg::Vec3f &to, float radius)
{
btCollisionWorld::ClosestConvexResultCallback callback(toBullet(from), toBullet(to));
callback.m_collisionFilterGroup = 0xff;
callback.m_collisionFilterMask = CollisionType_World|CollisionType_HeightMap;
btSphereShape shape(radius);
const btQuaternion btrot = btQuaternion::getIdentity();
btTransform from_ (btrot, toBullet(from));
btTransform to_ (btrot, toBullet(to));
mCollisionWorld->convexSweepTest(&shape, from_, to_, callback);
RayResult result;
result.mHit = callback.hasHit();
if (result.mHit)
{
result.mHitPos = toOsg(callback.m_hitPointWorld);
result.mHitNormal = toOsg(callback.m_hitNormalWorld);
}
return result;
}
bool PhysicsSystem::getLineOfSight(const MWWorld::Ptr &actor1, const MWWorld::Ptr &actor2)
{
Actor* physactor1 = getActor(actor1);

@ -101,6 +101,8 @@ namespace MWPhysics
RayResult castRay(const osg::Vec3f &from, const osg::Vec3f &to, MWWorld::Ptr ignore = MWWorld::Ptr(), int mask =
CollisionType_World|CollisionType_HeightMap|CollisionType_Actor);
RayResult castSphere(const osg::Vec3f& from, const osg::Vec3f& to, float radius);
/// Return true if actor1 can see actor2.
bool getLineOfSight(const MWWorld::Ptr& actor1, const MWWorld::Ptr& actor2);

@ -80,21 +80,28 @@ namespace MWRender
return mTrackingPtr;
}
void Camera::updateCamera(osg::Camera *cam)
osg::Vec3d Camera::getFocalPoint()
{
if (mTrackingPtr.isEmpty())
return;
const osg::Node* trackNode = mTrackingNode;
if (!trackNode)
return;
return osg::Vec3d();
osg::MatrixList mats = trackNode->getWorldMatrices();
if (!mats.size())
return;
return osg::Vec3d();
const osg::Matrix& worldMat = mats[0];
osg::Vec3d position = worldMat.getTrans();
if (!isFirstPerson())
position.z() += mHeight;
return position;
}
void Camera::updateCamera(osg::Camera *cam)
{
if (mTrackingPtr.isEmpty())
return;
osg::Vec3d position = getFocalPoint();
osg::Quat orient = osg::Quat(getPitch(), osg::Vec3d(1,0,0)) * osg::Quat(getYaw(), osg::Vec3d(0,0,1));
@ -373,12 +380,14 @@ namespace MWRender
rotateCamera(getPitch(), getYaw(), false);
}
void Camera::getPosition(osg::Vec3 &focal, osg::Vec3 &camera)
void Camera::getPosition(osg::Vec3f &focal, osg::Vec3f &camera)
{
//mCamera->getParentSceneNode()->needUpdate(true);
focal = getFocalPoint();
//camera = mCamera->getRealPosition();
//focal = mCameraNode->_getDerivedPosition();
osg::Quat orient = osg::Quat(getPitch(), osg::Vec3d(1,0,0)) * osg::Quat(getYaw(), osg::Vec3d(0,0,1));
osg::Vec3d offset = orient * osg::Vec3d(0, -mCameraDistance, 0);
camera = focal + offset;
}
void Camera::togglePlayerLooking(bool enable)

@ -5,6 +5,7 @@
#include <osg/ref_ptr>
#include <osg/Vec3>
#include <osg/Vec3d>
#include "../mwworld/ptr.hpp"
@ -113,8 +114,10 @@ namespace MWRender
void setAnimation(NpcAnimation *anim);
osg::Vec3d getFocalPoint();
/// Stores focal and camera world positions in passed arguments
void getPosition(osg::Vec3 &focal, osg::Vec3 &camera);
void getPosition(osg::Vec3f &focal, osg::Vec3f &camera);
void togglePlayerLooking(bool enable);

@ -514,6 +514,11 @@ namespace MWRender
}
}
float RenderingManager::getNearClipDistance() const
{
return mNearClip;
}
bool RenderingManager::vanityRotateCamera(const float *rot)
{
if(!mCamera->isVanityOrPreviewModeEnabled())

@ -113,6 +113,8 @@ namespace MWRender
void processChangedSettings(const Settings::CategorySettingVector& settings);
float getNearClipDistance() const;
// camera stuff
bool vanityRotateCamera(const float *rot);
void setCameraDistance(float dist, bool adjust, bool override);

@ -1617,6 +1617,19 @@ namespace MWWorld
int blind = static_cast<int>(player.getClass().getCreatureStats(player).getMagicEffects().get(ESM::MagicEffect::Blind).getMagnitude());
MWBase::Environment::get().getWindowManager()->setBlindness(std::max(0, std::min(100, blind)));
mRendering->getCamera()->setCameraDistance();
if(!mRendering->getCamera()->isFirstPerson())
{
osg::Vec3f focal, camera;
mRendering->getCamera()->getPosition(focal, camera);
float radius = mRendering->getNearClipDistance()*2.5f;
MWPhysics::PhysicsSystem::RayResult result = mPhysics->castSphere(focal, camera, radius);
if (result.mHit)
mRendering->getCamera()->setCameraDistance((result.mHitPos - focal).length() - radius, false, false);
}
}
void World::updateSoundListener()

Loading…
Cancel
Save