前言

在游戏开发中经常会接触到各种物理引擎,虽然开源的引擎各种各样,但是基本原理是相通的。实质上物理引擎只是以时间为单位的刷新物理世界中的刚体的位置(其中运用了大量物理公式和知识),然后刷新刚体关联的物品(节点)的位置来达到模拟效果。其中的细节是我们开发者不需要知道,也不知道的。所以刚体轨迹预测成为了难题。

效果

物理公式

在开始之前先补一下高中基础物理公式(主要涉及匀加速直线运动):

  1. 速度公式 
  2. 距离公式 
  3. 阻尼公式  (比较贴近)

 是指运动物体的初始速度,  是指运动物体的运动时间,  是指运动物体的结束速度,  是指运动物体运动距离,  是指运动物体的运动加速度,  是指运动物体的阻尼系数(模拟的空气阻尼等)。

即:

//速度公式
v(v0, a, t) {
return v0 + a * t;
}
//距离公式
s(v0, a, t) {
return v0 * t + (a * t * t) / 2;
}
//阻尼公式
let damping = -this.linear_Damping * this.v_y * dt;
this.v_y = this.v(this.v_y, this.a_y, dt) + damping;

运动分解

运动比较复杂,但是可以简单的拆解为X轴运动和Y轴运动,把运动简单化。

  • Y轴

阻尼系数:

typescript this.linear_Damping = this.RigidBody.linearDamping;

加速度为物理世界重力*刚体的重力缩放(PTM_RATIO为单位换算比):

typescript let world_y = cc.director.getPhysicsManager().gravity.y; this.a_y =(-this.RigidBody.gravityScale * world_y) /cc.PhysicsManager.PTM_RATIO;

速度为物体初始速度(PTM_RATIO为单位换算比):

typescript this.v_y =-this.RigidBody.linearVelocity.y / cc.PhysicsManager.PTM_RATIO;

刚体Y轴位置:

typescript this.y = this.RigidBody.node.y;

经过dt时间,物体Y轴运动为(PTM_RATIO为单位换算比):

typescript this.y -= this.s(this.v_y, this.a_y, dt)*cc.PhysicsManager.PTM_RATIO; let damping = -this.linear_Damping * this.v_y * dt; this.v_y = this.v(this.v_y, this.a_y, dt) + damping;

  • X轴

同理...

关键代码

const { ccclass, property } = cc._decorator;

@ccclass
export default class RigidBodyaPrediction extends cc.Component {
@property(cc.RigidBody)
RigidBody: cc.RigidBody = null; @property(cc.Prefab)
show_prefab: cc.Prefab = null; @property(cc.Node)
result: cc.Node = null; //米/秒^2
a_y = 0;
a_x = 0;
//米/秒
v_y = 0;
v_x = 0;
//位置
x = 0;
y = 0;
//阻尼
linear_Damping = 0; status = true; pool: cc.Node[] = new Array<cc.Node>(); onLoad() {
for (let index = 0; index < 25; index++) {
let tmp = cc.instantiate(this.show_prefab);
this.pool.push(tmp);
this.result.addChild(tmp);
}
}
init() {
let world_y = cc.director.getPhysicsManager().gravity.y;
let world_x = cc.director.getPhysicsManager().gravity.x;
this.a_y =
(-this.RigidBody.gravityScale * world_y) /
cc.PhysicsManager.PTM_RATIO;
this.a_x =
(this.RigidBody.gravityScale * world_x) /
cc.PhysicsManager.PTM_RATIO;
this.v_y =
-this.RigidBody.linearVelocity.y / cc.PhysicsManager.PTM_RATIO;
this.v_x =
this.RigidBody.linearVelocity.x / cc.PhysicsManager.PTM_RATIO;
this.x = this.RigidBody.node.x;
this.y = this.RigidBody.node.y;
this.linear_Damping = this.RigidBody.linearDamping;
}
start() {
this.RigidBody.node.on(
cc.Node.EventType.TOUCH_CANCEL,
function() {
this.RigidBody.type = cc.RigidBodyType.Dynamic;
this.status = false;
},
this
);
this.RigidBody.node.on(
cc.Node.EventType.TOUCH_MOVE,
function(event: cc.Touch) {
let vec2 = this.RigidBody.node.convertToNodeSpaceAR(
event.getLocation()
);
this.RigidBody.linearVelocity = new cc.Vec2(
-vec2.x,
-vec2.y * 2
);
},
this
);
} show() {
this.init();
this.node.removeChild(this.RigidBody.node, false);
this.node.addChild(this.RigidBody.node, 9999);
for (let index = 0; index < 25; index++) {
let tmp = this.pool[index];
tmp.x = this.x;
tmp.y = this.y;
this.updatePostion(0.15);
}
}
updateX(dt) {
this.x += this.s(this.v_x, this.a_x, dt) * cc.PhysicsManager.PTM_RATIO;
let damping = -this.linear_Damping * this.v_x * dt;
this.v_x = this.v(this.v_x, this.a_x, dt) + damping;
}
updateY(dt) {
this.y -= this.s(this.v_y, this.a_y, dt) * cc.PhysicsManager.PTM_RATIO;
let damping = -this.linear_Damping * this.v_y * dt;
this.v_y = this.v(this.v_y, this.a_y, dt) + damping;
}
updatePostion(dt) {
this.updateX(dt);
this.updateY(dt);
} v(v0, a, t) {
return v0 + a * t;
}
s(v0, a, t) {
return v0 * t + (a * t * t) / 2;
}
update() {
if (this.status) {
this.show();
}
}
}

例子

RigidBodyaPrediction

Box2d刚体轨迹预测的更多相关文章

  1. CVPR 2019轨迹预测竞赛冠军方法总结

    背景 CVPR 2019 是机器视觉方向最重要的学术会议,本届大会共吸引了来自全世界各地共计 5160 篇论文,共接收 1294 篇论文,投稿数量和接受数量都创下了历史新高,其中与自动驾驶相关的论文. ...

  2. 关于CCSprite改变box2d刚体位置以及角度。

    同事今天在讨论一个事情,box2d中,body不可以直接设置位置,这样是不合理的,因为在物理的世界,你去左右它的物理检测.它就没有存在的必要了.但是,有人就想直接用box2d的碰撞.不用物理模拟.怎么 ...

  3. HTML5之2D物理引擎 Box2D for javascript Games 系列 第二部分

    这是系列第二部分,之前部分在本博客中找 源码demo存放在https://github.com/willian12345/Box2D-for-Javascript-Games 向世界添加刚体 刚体(B ...

  4. 基于单细胞测序数据构建细胞状态转换轨迹(cell trajectory)方法总结

    细胞状态转换轨迹构建示意图(Trapnell et al. Nature Biotechnology, 2014) 在各种生物系统中,细胞都会展现出一系列的不同状态(如基因表达的动态变化等),这些状态 ...

  5. 多目标跟踪方法 NOMT 学习与总结

    多目标跟踪方法 NOMT 学习与总结 ALFD NOMT MTT 读 'W. Choi, Near-Online Multi-target Tracking with Aggregated Local ...

  6. 文献阅读报告 - 3DOF Pedestrian Trajectory Prediction

    文献 Sun L , Yan Z , Mellado S M , et al. 3DOF Pedestrian Trajectory Prediction Learned from Long-Term ...

  7. GDC2016【For Honor-荣耀战魂】的次世代动画技术

    生成自然丰富,反应灵敏的动作的“Motion Matching”是什么?         Ubisoft在2016年内预定发售的[荣誉战魂],是基于MOBA类集团战斗,并加入了高度紧张的剑斗动作的多人 ...

  8. hadoop基础教程免费分享

    提起Hadoop相信大家还是很陌生的,但大数据呢?大数据可是红遍每一个角落,大数据的到来为我们社会带来三方面变革:思维变革.商业变革.管理变革,各行业将大数据纳入企业日常配置已成必然之势.阿里巴巴创办 ...

  9. [AI开发]基于深度学习的视频多目标跟踪实现

    据我目前了解掌握,多目标跟踪大概有两种方式: Option1 基于初始化帧的跟踪,在视频第一帧中选择你的目标,之后交给跟踪算法去实现目标的跟踪.这种方式基本上只能跟踪你第一帧选中的目标,如果后续帧中出 ...

随机推荐

  1. 如何下载windows版的kubectl.exe文件

    github上的下载链接,不能直接下载. https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.14.md#client-b ...

  2. Flask框架之功能详解

    1|0浏览目录 配置文件 路由系统 视图 请求相关 响应 模板渲染 session 闪现 中间件 蓝图(blueprint) 特殊装饰器 1|1配置文件 知识点 给你一个路径 "settin ...

  3. Excel-信息函数&数组公式

    1.IS系列函数-逻辑函数 is函数是一个逻辑函数,可以用来判断一些特定的内容 Istext判断单元格是否是文本 Isnumber判断单元格是否为数值 Istext和isnumber的判断的结果相反 ...

  4. myeclipse开发工具的简单使用

    一.使用eclipse.myeclipse开发JAVA程序 将程序开发环境和调试环境集合在一起,提高开发效率 1.创建java项目2.创建程序包3.编写JAVA源程序4.运行JAVA程序 二.程序移植 ...

  5. CF1151F Sonya and Informatics(概率期望,DP,矩阵快速幂)

    明明是水题结果没切掉……降智了…… 首先令 $c$ 为序列中 $0$ 的个数,那么排序后序列肯定是前面 $c$ 个 $0$,后面 $n-c$ 个 $1$. 那么就能上 DP 了.(居然卡在这里……) ...

  6. [LeetCode] 663. Equal Tree Partition 划分等价树

    Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to tw ...

  7. mysql 类型自动化转换问题

    mysql 类型自动化转换问题 背景  有个业务需求,使用到find_in_set函数,简单贴下,如下: SELECT FIND_IN_SET('b','a,b,c,d'); //返回值为2,即第2个 ...

  8. tecplot无法处理高版本fluent导出的Ensight格式

    高版本的Fluent完成计算,将计算结果导出为Ensight格式,然后再导入tecplot当中进行后处理的时候会遇见如下的错误: 但是将低版本的Fluent计算结果导出为Ensight格式,却可以顺利 ...

  9. portal项目启动问题

    错误信息: Disconnected from the target VM, address: '127.0.0.1:58909', transport: 'socket' Process finis ...

  10. 推荐一款健康App 多喝水,引领全民时尚喝水生活习惯

    推荐一款健康App 多喝水,引领全民时尚喝水生活习惯 1 介绍 多喝水,一款鼓励大众喝水的APP.我们倡导大众健康生活,培养人们爱喝水的习惯,让每一次喝水,都能产生价值,让人们在喝水的同时,可享受赚钱 ...