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 基于初始化帧的跟踪,在视频第一帧中选择你的目标,之后交给跟踪算法去实现目标的跟踪.这种方式基本上只能跟踪你第一帧选中的目标,如果后续帧中出 ...
随机推荐
- LCA最近公共祖先-- HDU 2586
题目链接 Problem Description There are n houses in the village and some bidirectional roads connecting t ...
- vue中webpack的配置理解
当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) var path = require('path') ...
- 非main goroutine的退出及调度循环(15)
本文是<Go语言调度器源代码情景分析>系列的第15篇,也是第二章的第5小节. 上一节我们说过main goroutine退出时会直接执行exit系统调用退出整个进程,而非main goro ...
- 在springboot或者ssm框架或者类似的框架中VO、DTO、DO、PO的概念、区别和用处
该文章主要讨论我们开发过程当中会经常用到的对象:VO.DTO.DO.PO;由于项目和每个人的命名习惯,这里只是对这些概念进行阐述.概念: VO(View Object):视图对象,用于展示层,它的作用 ...
- shell-基础2
条件表达式 文件判断 文件测试操作符 常用文件测试操作符 说明 -d文件,d的全拼为directory 文件存在且为目录则为真,即测试表达式成立 -f文件,f的全拼为file 文件存在且为普通文件则为 ...
- 青春正盛,未来可期。马上2020了,低成本投资自己:vip测试提升圈
应部分群友再三强烈建议要求,组建了一个测试提升小分队,相约vip测试提升圈, 这里汇集了一群热爱学习.渴望提升的测试小伙伴,大家都朝着自己的梦想拼命努力: 此圈将助你在接口自动化和性能方向全面提升,提 ...
- CF1217E Sum Queries? (线段树)
完了,前几天才说 edu 的 DEF 都不会,现在打脸了吧 qwq 其实在刚说完这句话 1min 就会了 D,3min 就会了 E 发现,对于大小 \(\ge 3\) 的不平衡集合,它至少有一个大小为 ...
- [THUPC2018]弗雷兹的玩具商店(线段树,背包)
最近状态有点颓,刷刷水题找找自信. 首先每次询问就是完全背包.可以 $O(m^2)$. 由于每个物品都可以用无数次,所以对于价格相同的物品,我们只用考虑愉悦度最高的. 直接上线段树.$val[i]$ ...
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- PS:老权限登录Action 中 WebObjManager有问题,一直登录不起问题
.ashx后面代码要多继承一个, IRequiresSessionState接口