Skip to content

Commit

Permalink
Cleaning up camera pins
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Mar 7, 2023
1 parent b0be111 commit 71e7a6b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
56 changes: 31 additions & 25 deletions game/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ void Camera::setMode(Camera::Mode m) {
}
}

void Camera::setMarvinMode(Camera::MarvinMode m) {
if(camMarvinMod==m)
void Camera::setMarvinMode(Camera::MarvinMode nextMod) {
if(camMarvinMod==nextMod)
return;

if(auto pl = Gothic::inst().player()) {
if(camMarvinMod==M_Pinned) {
dst.spin.y = pl->rotation() - cameraOffsetAng;
src.spin = dst.spin;
float range = src.range*100.f;
Vec3 dir = {0,0,1};
Expand All @@ -144,22 +143,19 @@ void Camera::setMarvinMode(Camera::MarvinMode m) {
cameraPos = src.target;
rotOffset.y = 0;
}
if(m==M_Pinned) {
float trY = pl->isSwim() ? 0 : -pl->translateY();
Vec3 offset = {0,trY,0};
Matrix4x4 rotOffsetMat = pl->transform();
if(pl->isDive())
rotOffsetMat.rotateOX(pl->rotationY());
rotOffsetMat.inverse();
rotOffsetMat.translate(origin);
rotOffsetMat.project(offset);
cameraOffset = offset;
cameraOffsetAng = pl->rotation() - dst.spin.y;
dst.spin.y = pl->rotation();
src.spin = dst.spin;
if(nextMod==M_Pinned) {
const auto& def = cameraDef();
auto offset = origin;
Matrix4x4 rotMat = pl->cameraMatrix(false);

rotMat.inverse();
rotMat.project(offset);
pin.origin = offset;
pin.spin.x = src.spin.x - def.best_elevation;
pin.spin.y = src.spin.y - (pl ? pl->rotation() : 0);
}
}
camMarvinMod = m;
camMarvinMod = nextMod;
}

bool Camera::isMarvin() const {
Expand Down Expand Up @@ -526,7 +522,7 @@ void Camera::followCamera(Vec3& pos, Vec3 dest, float dtF) {
return;
}

float speed = def.velo_trans*100.f*dtF;
float speed = def.velo_trans*dtF*100.f;
float tr = std::min(speed,len);
float k = tr/len;
pos += dp*k;
Expand All @@ -540,7 +536,7 @@ void Camera::followAng(Vec3& spin, Vec3 dest, float dtF) {

void Camera::followAng(float& ang, float dest, float speed, float dtF) {
float da = angleMod(dest-ang);
float shift = da*std::min(1.f,2.f*speed*dtF);
float shift = da*std::min(1.f, speed*dtF*1.f);
if(std::abs(da)<0.01f || dtF<0.f) {
ang = dest;
return;
Expand Down Expand Up @@ -620,8 +616,22 @@ void Camera::calcControlPoints(float dtF) {
followCamera(cameraPos,camTg,dtF);

origin = cameraPos - dir*range;
if(camMarvinMod==M_Free)
if(camMarvinMod==M_Free) {
return;
}

const auto pl = Gothic::inst().player();
if(camMarvinMod==M_Pinned && camMod!=Dialog && pl!=nullptr) {
auto rotMat = pl->cameraMatrix(false);
auto offset = pin.origin;
rotMat.project(offset);
origin = offset;
src.target = dst.target;
src.spin = dst.spin + pin.spin;
offsetAng = Vec3();
return;
}

if(def.collision!=0) {
range = calcCameraColision(camTg,origin,src.spin,range);
origin = cameraPos - dir*range;
Expand All @@ -632,15 +642,11 @@ void Camera::calcControlPoints(float dtF) {
offsetAng = Vec3(); else
offsetAng = calcOffsetAngles(origin,baseOrigin,dst.target);

if((fpEnable && camMarvinMod==M_Normal) || (camMarvinMod==M_Pinned && camMod!=Dialog)) {
if(fpEnable && camMarvinMod==M_Normal) {
origin = dst.target;
offsetAng = Vec3();

Vec3 offset = {0,0,20};
if(camMarvinMod==M_Pinned) {
offset = cameraOffset;
rotOffset.y = cameraOffsetAng;
}
rotOffsetMat.identity();
rotOffsetMat.rotateOY(180-src.spin.y);
rotOffsetMat.project(offset);
Expand Down
9 changes: 7 additions & 2 deletions game/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,19 @@ class Camera final {
Tempest::Vec3 target = {};
};

struct Pin {
Tempest::Vec3 origin = {};
Tempest::Vec3 spin = {};
};

Tempest::Vec3 cameraPos = {};
Tempest::Vec3 cameraOffset = {};
float cameraOffsetAng = 0;
Tempest::Vec3 origin = {};
Tempest::Vec3 rotOffset = {};
Tempest::Vec3 offsetAng = {};
State src, dst;

Pin pin;

float dlgDist = 0;
float userRange = 0.13f;

Expand Down
8 changes: 8 additions & 0 deletions game/world/objects/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ Vec3 Npc::cameraBone(bool isFirstPerson) const {
return r;
}

Matrix4x4 Npc::cameraMatrix(bool isFirstPerson) const {
const size_t head = visual.pose().findNode("BIP01 HEAD");
if(isFirstPerson && head!=size_t(-1)) {
return visual.pose().bone(head);
}
return visual.pose().rootBone();
}

float Npc::collisionRadius() const {
return physic.radius();
}
Expand Down
1 change: 1 addition & 0 deletions game/world/objects/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Npc final {
auto transform() const -> Tempest::Matrix4x4;
auto position() const -> Tempest::Vec3;
auto cameraBone(bool isFirstPerson = false) const -> Tempest::Vec3;
auto cameraMatrix(bool isFirstPerson = false) const -> Tempest::Matrix4x4;
float collisionRadius() const;
float rotation() const;
float rotationRad() const;
Expand Down

0 comments on commit 71e7a6b

Please sign in to comment.