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

效果
物理公式
在开始之前先补一下高中基础物理公式(主要涉及匀加速直线运动):
- 速度公式
- 距离公式
- 阻尼公式
(比较贴近)
是指运动物体的
初始速度, 是指运动物体的
运动时间, 是指运动物体的
结束速度, 是指运动物体
运动距离, 是指运动物体的
运动加速度, 是指运动物体的
阻尼系数(模拟的空气阻尼等)。
即:
//速度公式
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();
}
}
}
例子
Box2d刚体轨迹预测的更多相关文章
- CVPR 2019轨迹预测竞赛冠军方法总结
背景 CVPR 2019 是机器视觉方向最重要的学术会议,本届大会共吸引了来自全世界各地共计 5160 篇论文,共接收 1294 篇论文,投稿数量和接受数量都创下了历史新高,其中与自动驾驶相关的论文. ...
- 关于CCSprite改变box2d刚体位置以及角度。
同事今天在讨论一个事情,box2d中,body不可以直接设置位置,这样是不合理的,因为在物理的世界,你去左右它的物理检测.它就没有存在的必要了.但是,有人就想直接用box2d的碰撞.不用物理模拟.怎么 ...
- HTML5之2D物理引擎 Box2D for javascript Games 系列 第二部分
这是系列第二部分,之前部分在本博客中找 源码demo存放在https://github.com/willian12345/Box2D-for-Javascript-Games 向世界添加刚体 刚体(B ...
- 基于单细胞测序数据构建细胞状态转换轨迹(cell trajectory)方法总结
细胞状态转换轨迹构建示意图(Trapnell et al. Nature Biotechnology, 2014) 在各种生物系统中,细胞都会展现出一系列的不同状态(如基因表达的动态变化等),这些状态 ...
- 多目标跟踪方法 NOMT 学习与总结
多目标跟踪方法 NOMT 学习与总结 ALFD NOMT MTT 读 'W. Choi, Near-Online Multi-target Tracking with Aggregated Local ...
- 文献阅读报告 - 3DOF Pedestrian Trajectory Prediction
文献 Sun L , Yan Z , Mellado S M , et al. 3DOF Pedestrian Trajectory Prediction Learned from Long-Term ...
- GDC2016【For Honor-荣耀战魂】的次世代动画技术
生成自然丰富,反应灵敏的动作的“Motion Matching”是什么? Ubisoft在2016年内预定发售的[荣誉战魂],是基于MOBA类集团战斗,并加入了高度紧张的剑斗动作的多人 ...
- hadoop基础教程免费分享
提起Hadoop相信大家还是很陌生的,但大数据呢?大数据可是红遍每一个角落,大数据的到来为我们社会带来三方面变革:思维变革.商业变革.管理变革,各行业将大数据纳入企业日常配置已成必然之势.阿里巴巴创办 ...
- [AI开发]基于深度学习的视频多目标跟踪实现
据我目前了解掌握,多目标跟踪大概有两种方式: Option1 基于初始化帧的跟踪,在视频第一帧中选择你的目标,之后交给跟踪算法去实现目标的跟踪.这种方式基本上只能跟踪你第一帧选中的目标,如果后续帧中出 ...
随机推荐
- Unity检视面板的继承方法研究 (一)
对于检视面板 Inspector 的面板继承方式对项目来说是很有必要的, 比如一个基类, 写了一个很好看的检视面板[CustomEditor(typeof(XXX))], 可是所有子类的面板无法直接继 ...
- Codeforces 1278F: Cards
题目传送门:CF1278F. 题意简述: 有 \(n\) 个独立随机变量 \(x_i\),每个随机变量都有 \(p = 1/m\) 的概率取 \(1\),有 \((1-p)\) 的概率取 \(0\). ...
- python gyp
https://github.com/bnoordhuis/gyp 所以,手动加了这个变量 https://blog.csdn.net/weixin_30576827/article/details/ ...
- 接口测试 从 0 到 1,用 Jmeter 搭建 HTTP 接口自动化引擎 1.0 版本
百度网盘地址更新:https://pan.baidu.com/s/13S0WStZpwlbL15IBp_Q-bg 1.已实现功能描述 1.1 框架包含:Jenkins.Ant.Jmeter 1.2 测 ...
- 【oracle】获取指定表空间的所有表名
select owner||'.'||table_name from dba_tables where tablespace_name='A';
- Spring 中的异常处理
工作中遇到这样的同事,问他活干完吗,他说开发好了,结果测试时发现各种异常情况未处理,联调测试时各种未知错误,最后联调完成比他预期的两倍工作量还多.这样的开发如果是新人还可以原谅,如果有工作经验且出现多 ...
- 【转】python中numpy模块下的np.clip()的用法
转自:https://blog.csdn.net/HHTNAN/article/details/79799612 Numpy 中clip函数的使用 一维数组 其中a是一个数组,后面两个参数分别表示最小 ...
- 一张思维导图辅助你深入了解 Vue | Vue-Router | Vuex 源码架构
1.前言 本文内容讲解的内容:一张思维导图辅助你深入了解 Vue | Vue-Router | Vuex 源码架构. 项目地址:https://github.com/biaochenxuying/vu ...
- Intellij插件之MavenHelper
作用: 一键查看maven依赖,查看冲突的依赖,一键进行exclude依赖 插件提供地址: https://plugins.jetbrains.com/plugin/7179-maven-helper ...
- 【maven】pom.xml的exclusions排除依赖传递
在引用两个有冲突的依赖时,就需要把其中一个的依赖中某个依赖排除掉 exclusions 例如: <dependency> <groupId>org.activiti</g ...