mirror of
https://github.com/jhaizhou-ops/d2r-mobile
synced 2026-11-26 13:57:52 +08:00
Camera: follow the ground height, so first person no longer floats after going down
The original camera keeps a fixed distance (884.03 along its sight line) from the character and moves down with it on lower ground (measured on an iPhone: camera y eased from 442.0 to 430.1 after teleporting down). The character's position used to be where the sight line meets y = 0, so on lower ground the perspective camera stayed at the old height and the character seemed to float. It is now taken as camera - 884.03 * sight line, and both modes place the eye and the look point relative to the character's height. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec7a3cb346
commit
f23047f71e
4 files changed
+41
-24
No files matched your search
@@ -3069,7 +3069,7 @@ static int cam_mode(void) { // 档可在游戏中切换(自定义页):
|
||||
MLOG("镜头实验:档 %d dist=%g h=%g look=%g lookh=%g fov=%g tau=%g reach=%g", g_cam.mode, q->dist, q->h, q->look, q->lookh, q->fov, q->tau, q->reach); } }
|
||||
return g_cam.mode; }
|
||||
static void cam_build(const uint8_t *src, uint8_t *dst) { // dst 已是 src 的拷贝 | dst is already a copy of src
|
||||
xbg_cam_state *st = &g_cam.st; float px, pz, wx = 0, wz = 0; xbg_cam_ground(src, &px, &pz); const int want = dm_cam_want_dir(&wx, &wz);
|
||||
xbg_cam_state *st = &g_cam.st; float px, py, pz, wx = 0, wz = 0; xbg_cam_ground(src, &px, &py, &pz); const int want = dm_cam_want_dir(&wx, &wz);
|
||||
const uint64_t f = atomic_load(&g_cur_frame); float turn = 0;
|
||||
if (f != st->frame) { turn = dm_cam_take_turn(); st->hold = dm_cam_turn_age() < 1.0; } // 体感 / 按钮转向:每帧取一次;一秒内转过就让跟随不动朝向 | turns: taken once per frame; a turn within a second holds the follow
|
||||
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); const double t0 = st->t;
|
||||
|
||||
@@ -18,12 +18,13 @@ static void get(const uint8_t *block,size_t at,float *m,size_t n){memcpy(m,block
|
||||
static void xf(const float *M,const float v[4],float o[4]){for(int j=0;j<4;j++)o[j]=v[0]*M[j]+v[1]*M[4+j]+v[2]*M[8+j]+v[3]*M[12+j];}
|
||||
static bool identity(const float *a,const float *b,double eps){float m[16];xbg_cam_m4_mul(a,b,m);for(int i=0;i<16;i++)if(!near(m[i],i%5==0?1:0,eps))return false;return true;}
|
||||
|
||||
// 原版主相机块:视图(右、上、后三列),镜头在人物 (px, pz) 沿「后」方向 550 处,正交投影 40 × 22.5(16:9)、反向 Z
|
||||
// The original main camera block: view (right, up, back columns), camera 550 along "back" from the character (px, pz), orthographic 40 x 22.5 (16:9), reversed Z
|
||||
static void original(uint8_t *block,float px,float pz){
|
||||
// 原版主相机块:视图(右、上、后三列),镜头在人物 (px, py, pz) 沿「后」方向 884.03 处(原版的固定距离),正交投影 40 × 22.5(16:9)、反向 Z
|
||||
// The original main camera block: view (right, up, back columns), camera 884.03 along "back" from the character (px, py, pz) (the original's fixed
|
||||
// distance), orthographic 40 x 22.5 (16:9), reversed Z
|
||||
static void original(uint8_t *block,float px,float pz,float py=0){
|
||||
memset(block,0,XBG_CAM_BLOCK);
|
||||
const float r[3]={0.70711f,0,-0.70711f},u[3]={-0.35355f,0.86603f,-0.35355f},b[3]={0.61237f,0.5f,0.61237f};
|
||||
const float eye[3]={px+b[0]*550,b[1]*550,pz+b[2]*550};
|
||||
const float eye[3]={px+b[0]*884.03f,py+b[1]*884.03f,pz+b[2]*884.03f};
|
||||
float V[16]={r[0],u[0],b[0],0, r[1],u[1],b[1],0, r[2],u[2],b[2],0,
|
||||
-(eye[0]*r[0]+eye[1]*r[1]+eye[2]*r[2]),-(eye[0]*u[0]+eye[1]*u[1]+eye[2]*u[2]),-(eye[0]*b[0]+eye[1]*b[1]+eye[2]*b[2]),1};
|
||||
float iV[16];xbg_cam_m4_inv(V,iV);
|
||||
@@ -52,9 +53,11 @@ int main(){
|
||||
{uint8_t bad[XBG_CAM_BLOCK];memcpy(bad,src,sizeof bad);float x=0.69f;memcpy(bad+80,&x,4);CHECK(!xbg_cam_sig(bad,XBG_CAM_BLOCK,XBG_CAM_BLOCK));}
|
||||
{uint8_t persp[XBG_CAM_BLOCK];memcpy(persp,src,sizeof persp);float m1=-1;memcpy(persp+208+44,&m1,4);CHECK(!xbg_cam_sig(persp,XBG_CAM_BLOCK,XBG_CAM_BLOCK));}
|
||||
|
||||
// 人物落地点 = 原版视线与地面的交点
|
||||
// The character's ground point = where the original sight line meets the ground
|
||||
{float px,pz;xbg_cam_ground(src,&px,&pz);CHECK(near(px,100,1e-2) && near(pz,50,1e-2));}
|
||||
// 人物位置 = 原版镜头沿视线往前固定距离;人物在低处(y = -12)时高度跟着降、水平位置不偏
|
||||
// The character's position = the fixed distance ahead of the original camera along the sight line; with the character lower (y = -12) the height
|
||||
// follows and the horizontal position does not drift
|
||||
{float px,py,pz;xbg_cam_ground(src,&px,&py,&pz);CHECK(near(px,100,1e-2) && near(py,0,1e-2) && near(pz,50,1e-2));
|
||||
uint8_t low[XBG_CAM_BLOCK];original(low,100,50,-12);xbg_cam_ground(low,&px,&py,&pz);CHECK(near(px,100,1e-2) && near(py,-12,1e-2) && near(pz,50,1e-2));}
|
||||
|
||||
// 首帧:朝向重置为原版视线的水平方向;同一帧再调不再更新
|
||||
// First frame: the heading resets to the horizontal direction of the original sight line; another call in the same frame does not update
|
||||
@@ -101,8 +104,15 @@ int main(){
|
||||
// First person: the eye is fwd ahead of the character at height eye, looking down by pitch
|
||||
{xbg_cam_state s;xbg_cam_init(&s);xbg_cam_params p2=xbg_cam_default_params(2);original(src,100,50);xbg_cam_follow(&s,100,50,1,1.0,0,0,0,p2.tau);
|
||||
memcpy(dst,src,sizeof dst);xbg_cam_build(&s,src,dst,2,&p2,vp,ivp);
|
||||
CHECK(near(s.eye[0],100-0.70711*2.2,2e-2) && near(s.eye[1],6,1e-5) && near(s.eye[2],50-0.70711*2.2,2e-2));
|
||||
CHECK(near(asin(-s.fwd[1])*180/3.14159265358979,8,1e-3) && p2.fov==75);}
|
||||
CHECK(near(s.eye[0],100-0.70711*2.2,2e-2) && near(s.eye[1],6,1e-2) && near(s.eye[2],50-0.70711*2.2,2e-2));
|
||||
CHECK(near(asin(-s.fwd[1])*180/3.14159265358979,8,1e-3) && p2.fov==75);
|
||||
// 人物在低处 12:眼睛跟着低 12(以前停在 y = 6,人像浮在空中);越肩档镜头同样整体低 12
|
||||
// The character 12 lower: the eye is 12 lower too (it used to stay at y = 6, as if floating); the over-the-shoulder camera is 12 lower as a whole too
|
||||
uint8_t low[XBG_CAM_BLOCK],d2[XBG_CAM_BLOCK];original(low,100,50,-12);xbg_cam_state s2=s;memcpy(d2,low,sizeof d2);xbg_cam_build(&s2,low,d2,2,&p2,vp,ivp);
|
||||
CHECK(near(s2.eye[1],-6,1e-2) && near(s2.eye[0],s.eye[0],2e-2) && near(s2.eye[2],s.eye[2],2e-2) && near(s2.fwd[1],s.fwd[1],1e-5));
|
||||
xbg_cam_params p1=xbg_cam_default_params(1);xbg_cam_state a=s,b=s;a.dcur=b.dcur=0;
|
||||
memcpy(d2,src,sizeof d2);xbg_cam_build(&a,src,d2,1,&p1,vp,ivp);memcpy(d2,low,sizeof d2);xbg_cam_build(&b,low,d2,1,&p1,vp,ivp);
|
||||
CHECK(near(b.eye[1],a.eye[1]-12,1e-2) && near(b.eye[0],a.eye[0],2e-2) && near(b.fwd[1],a.fwd[1],1e-5));}
|
||||
|
||||
// 片元槽 10 的场景常量:认签名,写镜头位置、视线与线性深度起点;镜头还没建过就不动
|
||||
// Scene constants of fragment slot 10: the signature is recognised; camera position, sight line and linear depth origin are written; untouched before any camera was built
|
||||
|
||||
@@ -87,15 +87,22 @@ static inline int xbg_cam_m4_inv(const float *m, float *o) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 人物落地点:原版镜头总对准人物,逆视图(+144)第 3 行 = 视图 +z 的世界方向、第 4 行 = 镜头位置,视线落到地面 y = 0 处就是人物
|
||||
// The character's ground point: the original camera always aims at the character; in the inverse view (+144) row 3 = world direction of view +z
|
||||
// and row 4 = camera position, and where the sight line meets the ground y = 0 is the character
|
||||
static inline void xbg_cam_ground(const uint8_t *src, float *px, float *pz) {
|
||||
// 人物位置:原版镜头总对准人物,而且离人物的距离固定(XBG_CAM_ORIG_DIST,沿视线)。逆视图(+144)第 3 行 = 视图 +z 的世界方向、第 4 行 = 镜头位置,
|
||||
// 人物 = 镜头位置 − 距离 × 视图 +z。地面有高低:人物走下坡、从高处传送下来时原版镜头跟着降(iPhone 实测:镜头 y 从 442.0 平滑降到 430.1),
|
||||
// 所以不能按「视线落到 y = 0」算——那样人物在低处时水平位置偏、透视镜头也停在原来的高度,像浮在空中
|
||||
// The character's position: the original camera always aims at the character from a fixed distance (XBG_CAM_ORIG_DIST, along the sight line). In the
|
||||
// inverse view (+144) row 3 = world direction of view +z and row 4 = camera position, so character = camera position - distance * view +z. The ground has
|
||||
// heights: walking down a slope or teleporting down from a height, the original camera goes down with the character (measured on an iPhone: camera y
|
||||
// eased from 442.0 to 430.1), so "where the sight line meets y = 0" is wrong -- with the character lower, the horizontal position is off and the
|
||||
// perspective camera stays at the old height, as if floating
|
||||
// 884.03 = 平地(y = 0)上视线到地面的距离,营地两次进局不同位置都是这个数 | 884.03 = the sight-line distance to the ground on flat ground (y = 0), the same at two different spots in camp
|
||||
#define XBG_CAM_ORIG_DIST 884.03f
|
||||
static inline void xbg_cam_ground(const uint8_t *src, float *px, float *py, float *pz) {
|
||||
float iv[16];
|
||||
memcpy(iv, src + 144, sizeof iv);
|
||||
float bx = iv[8], by = iv[9], bz = iv[10], cx = iv[12], cy = iv[13], cz = iv[14], t = by != 0 ? cy / by : 0;
|
||||
*px = cx - t * bx;
|
||||
*pz = cz - t * bz;
|
||||
*px = iv[12] - XBG_CAM_ORIG_DIST * iv[8];
|
||||
*py = iv[13] - XBG_CAM_ORIG_DIST * iv[9];
|
||||
*pz = iv[14] - XBG_CAM_ORIG_DIST * iv[10];
|
||||
}
|
||||
|
||||
// 镜头跟随,每帧一次(frame 变了才走,返回 1)。want = 宿主给的朝向(瞄准拖拽 > 移动摇杆,已是世界方向),没有就取人物位移方向,都没有就保持。
|
||||
@@ -158,9 +165,9 @@ static inline float xbg_cam_pull(xbg_cam_state *s, float dist, float clear, doub
|
||||
// +400 inverse view-projection and +464 the previous frame's (modified) view-projection for this frame's character position and heading, and switch +592
|
||||
// depth reconstruction to the perspective branch. vp / ivp receive the new view-projection and its inverse, to publish for input conversion
|
||||
static inline void xbg_cam_build(xbg_cam_state *s, const uint8_t *src, uint8_t *dst, int mode, const xbg_cam_params *pp, float vp[16], float ivp[16]) {
|
||||
float pr[16], px, pz;
|
||||
float pr[16], px, py, pz;
|
||||
memcpy(pr, src + 208, sizeof pr);
|
||||
xbg_cam_ground(src, &px, &pz);
|
||||
xbg_cam_ground(src, &px, &py, &pz);
|
||||
float fx = s->fx, fz = s->fz, eye[3], at[3], fov = pp->fov;
|
||||
if (mode == 2) {
|
||||
// 俯仰 = 缺省下俯减去玩家拖的偏移,夹在下俯 60° 到上仰 10° 之间
|
||||
@@ -168,8 +175,8 @@ static inline void xbg_cam_build(xbg_cam_state *s, const uint8_t *src, uint8_t *
|
||||
const float deg = 3.14159265f / 180, p0 = pp->pitch * deg;
|
||||
s->tilt_lo = p0 - 60 * deg; s->tilt_hi = p0 + 10 * deg;
|
||||
float h = pp->eye, ah = pp->fwd, pitch = p0 - fminf(fmaxf(s->tilt, s->tilt_lo), s->tilt_hi);
|
||||
eye[0] = px + fx * ah; eye[1] = h; eye[2] = pz + fz * ah;
|
||||
at[0] = eye[0] + fx * cosf(pitch); at[1] = h - sinf(pitch); at[2] = eye[2] + fz * cosf(pitch);
|
||||
eye[0] = px + fx * ah; eye[1] = py + h; eye[2] = pz + fz * ah;
|
||||
at[0] = eye[0] + fx * cosf(pitch); at[1] = eye[1] - sinf(pitch); at[2] = eye[2] + fz * cosf(pitch);
|
||||
} else {
|
||||
// 避墙拉近时镜头往上升(「吊臂」):镜头到看点高度的距离不变,水平距离变短高度就变高;贴着墙时差不多从头顶往下看,近似原版视角,
|
||||
// 不再贴在人背后(OnePlus 13 实测:背后一格就是墙时镜头落到离人 2、肩高,看着像贴在背上)
|
||||
@@ -204,8 +211,8 @@ static inline void xbg_cam_build(xbg_cam_state *s, const uint8_t *src, uint8_t *
|
||||
const float e = e0 - fminf(fmaxf(s->tilt, s->tilt_lo), s->tilt_hi);
|
||||
const float dwall = d; // 转平了镜头会往后退:不退过避墙算出的距离,只降高度 | levelling out moves the camera back: not past the wall-avoidance distance, only lower
|
||||
d = fminf(R * cosf(e) - la, dwall); h = lh + R * sinf(e);
|
||||
eye[0] = px - fx * d; eye[1] = h; eye[2] = pz - fz * d;
|
||||
at[0] = px + fx * la; at[1] = lh; at[2] = pz + fz * la;
|
||||
eye[0] = px - fx * d; eye[1] = py + h; eye[2] = pz - fz * d;
|
||||
at[0] = px + fx * la; at[1] = py + lh; at[2] = pz + fz * la;
|
||||
}
|
||||
// 视图 +z = 朝后;右 = 上(0,1,0) × 后;上 = 后 × 右
|
||||
// View +z = backwards; right = up(0,1,0) x back; up = back x right
|
||||
|
||||
@@ -2487,7 +2487,7 @@ Buffer patch(uint64_t address,size_t copy,const Buffer &src){
|
||||
// 每帧一次跟随:宿主给的朝向(瞄准拖拽 > 移动摇杆)优先,其次人物位移方向;越肩档把两套矩阵发布给输入换算,第一人称不换算输入
|
||||
// Follow once per frame: the host's heading (aim drag > move stick) first, then the character's displacement; the over-the-shoulder mode
|
||||
// publishes both matrix sets for input conversion, first person does not convert input
|
||||
float px,pz,wx=0,wz=0;xbg_cam_ground(p,&px,&pz);const int want=dm_cam_want_dir?dm_cam_want_dir(&wx,&wz):0;
|
||||
float px,py,pz,wx=0,wz=0;xbg_cam_ground(p,&px,&py,&pz);const int want=dm_cam_want_dir?dm_cam_want_dir(&wx,&wz):0;
|
||||
// 体感转向(宿主 dm_cam.c):每帧取走一次累计的转动;最近一秒内转过就让跟随不动朝向,转动在跟随之后加上
|
||||
// Sensor steering (host dm_cam.c): take the accumulated turn once per frame; with a turn within the last second the follow leaves the heading alone,
|
||||
// and the turn is added after the follow
|
||||
|
||||
Reference in new issue
Block a user