如何防止[圆形]物体滑入静态物体之间的0px间隙 [英] how to prevent [circle] body from sliding into 0px gaps between static bodies

查看:114
本文介绍了如何防止[圆形]物体滑入静态物体之间的0px间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的身体是对撞机

有时速度很大

问题是边界的平铺地图是由小瓷砖组成的

the problem is that the tiled map of the boundaries is made of small tiles

身体以高速度穿过它

这是我所有机构的配置:

here is my config of all bodies:

const config = {
  inertia: Infinity, // do not spin
  friction: 0, // X*100% stop on hit
  frictionAir: 0, // X*100% slow on move
  restitution: 0.5, // bounce X*100% on hit
  collisionFilter: this.level.getCollisionFilter(), // default collision filter
  isStatic
}

...

getCollisionFilter (key = null) {
  switch (key) {
    case 'fly':
    return {
      category: 0x0008,
      mask: 0xFFFFFFF1,
      group: -1
    }
    case 'items':
    return {
      category: 0x0004,
      mask: 0xFFFFFFF1,
      group: -1
    }
    case 'woda':
    return {
      category: 0x0002,
      mask: 0xFFFFFFFF,
      group: -1
    }
    default:
    return {
      category: 0x0001,
      mask: 0xFFFFFFFF,
      group: 0
    }
  }
}

```

woda表示water,如果有任何相关性

woda means water if it's of any relevance

这是默认值和woda

推荐答案

问题是您正在使用的something.js没有连续的冲突检测.几年来一直是功能请求.现在,这并不意味着您无能为力.问题描述本身包含一些代码,这可能是通过矩形边界解决问题的最便宜的方法:

The problem is that matter.js you are using has no continuous collision detection. It has been a feature request for a few years. Now that doesn't mean there is nothing you can do. There is some code in the issue description itself which is probably the cheapest way of fixing issue with going through rectangular boundaries:

它会检测一个物体是否在世界范围之外,然后反转速度并向后平移该物体

It detects if a body is outside the world bounds and then reverses the velocity and translates the body back

或者,此帖子提供了一些想法.

Alternatively, this post gives a few ideas.

如果您想自己实现某些功能,我将尝试解释连续碰撞检测算法.

If you want to implement something yourself, I'll try to explain continuous collision detection algorithm.

基本上,对于场景中的每个运动对象,您都必须计算框架0<t0<1内的下一个碰撞时刻,然后将位置提前到框架内的该时刻t0,更新由于碰撞而产生的速度并继续在下一次碰撞t0<t1<1之后,直到到达tn=1的时间(帧结束),确保您不会由于计算的舍入或角"对象而卡在帧的中间.对于球形对撞机,通常使用胶囊vs胶囊(用于对象对)交集和胶囊vs盒为边界.

Basically, for each moving object in your scene you have to calculate moment of next collision within the fraction of the frame 0<t0<1, then advance positions to this moment t0 within the frame, update velocities due to collision and proceed further to the next collision t0<t1<1, until you reach time of tn=1 (end of frame), making sure you don't get stuck in a the middle of the frame due to rounding of calculation or "cornered" objects. For spherical colliders, that is usually done by using capsule vs capsule (for pairs of objects) intersection and capsule vs box for the boundaries.

您还可以通过以较低的速度隐藏多个隐藏帧来作弊:

You can also cheat by having multiple hidden frames at a slower speed:

  • 以最高的物体速度
  • 获取最小的对象碰撞器半径
  • 将最高速度除以对撞机的最小半径,然后将减速比例"四舍五入为整数
  • 以该整数减速比例"减速场景中的所有对象,并更新场景减速比例"时间,而无需重新绘制屏幕
  • 只更新一次屏幕,并显示减速比例"更新的结果,您应该获得与不减速时相同的位置,但要注意碰撞

好运相撞!

这篇关于如何防止[圆形]物体滑入静态物体之间的0px间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆