用鼠标控制的球拍对快速移动的球进行碰撞检测的问题 [英] Problem with collision detection of a fast moving ball with a racket controlled by mouse

查看:163
本文介绍了用鼠标控制的球拍对快速移动的球进行碰撞检测的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个统一的球拍,该球拍应该可以击球,而球拍是由鼠标直接控制的,即,蝙蝠是由鼠标使用鼠标轴并使用transform.translate()函数移动到移动球拍.

In unity, i have a racket which is supposed to hit a ball, and the racket is controlled directly by the mouse, i.e the bat is being moved by the mouse using mouse axes and the using transform.translate() function to move the racket.

我希望Unity3d的物理原理不能直接通过鼠标正确地转换球拍的运动并相应地影响球,而且我不得不编写一些自定义的东西,事实证明这是真的.

I expected that Unity3d's physics will not properly translate the racket's movement directly by mouse and impact the ball accordingly, and i would have to write something custom, and it turned out to be true.

但是,当球拍移动时,无法正确检测到球的碰撞.当它静止不动时,一切都很好,并且球的行为就像我喜欢的那样.

But the ball's collision is not being detected properly when the racket is moving. When it is still, everything is fine, and the ball behaves as i like.

现在我要编写一个自定义物理脚本(我使用C#编写脚本),其中我将4个长度为0.6F的射线投射到球上,然后进行一些复杂的矢量计算,然后计算球的速度.击球拍,并使用刚体"将其直接应用于球的速度.velocity= calculateVelocity().现在,当球拍不移动时,它又能正常工作,但当我移动球拍时,它又不能正常工作.问题的确切症状是:

Now i went as far as writing a custom physics script (i use C# for scripting) in which i attached 4 raycasts of length 0.6F to the ball, and after doing some complex vector calculations, calculate the velocity of the ball after hitting the racket, and apply it directly to the velocity of the ball using rigidbody.velocity = calculateVelocity(). Now it is again working fine when the racket is not moving, but not when i move the racket. The exact (symptoms of the) problem is:

使用内置的物理特性和碰撞检测:当球拍移动时,球有时会直接穿过球拍,有时会放慢速度(达到令人难以置信的水平).

Using built-in Physics and collision detection: When the racket is moving, the ball sometimes pass straight through the racket, and sometimes, it slows down (to unbelievable levels).

使用我的脚本计算速度:问题是相同的,但是当我打印对撞机(球拍)的法线时,它可以让我识别出问题所在.有时它会给出正确的法线,有时会给出法线向量的负值,这意味着它正直穿过顶面并检测到对撞机(球拍)底部的撞击.

Using my script to calculate velocity: The problem is the same, but it lets me identify what is wrong when i print the normal of the collider (the racket). It sometimes give the right normal, and sometime gives the negative of the normal vector, which means that it is going straight through the top surface and detecting the hit with the bottom side of the collider (racket).

我尝试过的事情:

  1. 增大对撞机的尺寸(它可以与球拍上较宽的盒子对撞机配合使用,但是很明显,球从球拍上移开了一段很长的距离,而我自己的脚本在这里起作用了,默认的物理特性给我带来了奇怪的感觉球拍移动时的结果),简而言之,我没有得到我想要的现实.

  1. Increasing the size of the collider (it works with the wider box collider on the racket, but then obviously the ball moves from quite a distance away from the racket, and my own script works here, default physics give strange results when racket is moved), in short i don't get the reality that i want.

将固定时间戳减少到0.001,这可以显着改善性能,但与我想要的结果仍然相去甚远,并且球再次经常选择错误的方向.

Decreasing the fixed timestamp to 0.001, which significantly improved things, but still very very far away from the result i want, and the ball is again quite often picking the wrong side of the ball.

将碰撞检测更改为连续动态.还是没有改善.

Changing collision detection to continuous dynamic. Which didn't improve things either.

除了在碰撞时选择了错误的一面外,我还观察到的另一个问题是,从球拍弹起后,球虽然在运动,但球拍运动得更快,而不是以完整的弧线或直线运动出现在球的前面,导致两次命中.这是基于可见的推测.

And in addition to the wrong side picked at collision, another problem i have observed is that after bouncing off the racket, the ball is moves but the racket is moved faster, it instead of moving in a complete arc or line, somehow appears in front of the ball, resulting in two hits. It is a conjecture based on what is visible.

此外,很明显,Unity3d的内置物理机制并未读取球拍的运动"方面,从而导致在使用鼠标击球时球拍运动时出现奇怪的行为.

Also it is clear that the "movement" aspect of the racket is not being read by Unity3d's built in physics, resulting in strange behavior when racket is moving using mouse hits the ball.

我被困住了,我不知道该从哪里搬家.请告诉我我做错了什么.

I am stuck, i have no idea where to move from here. Please tell me what is it that i am doing wrong.

推荐答案

正如其他人指出的那样,问题是球从一帧中位于垫子的一侧变成下一帧中的另一侧. .如果障碍物太窄,快速移动的物体通常会这样做.

As others have pointed out, the problem is that the ball goes from being on a side of the pad in one frame to being on the other side in the next. Fast moving objects tend to do that if the barriers are too slim.

对于此问题,有三种非常简单的解决方案:

There are three very simple solutions for this problem:

  • 增加垫或球的大小,这是更改对撞机大小时发生的情况.
  • 确定球的最大速度,以使球永远不会足够快地移动通过垫子.
  • 增加Unity进行物理计算的频率.可以在时间管理器中进行更改,以减少固定时间步长"的值.注意不要减少太多,否则物理引擎将无法在下一回合开始之前结束通话,并且将永远无法跟上比赛的步伐.
  • Increase the size of the pad or the ball, which is what happened when you changed the collider size.
  • Establish a maximum speed for the ball, so that it can never move fast enough to go through the pads.
  • Increase the frequency with which Unity makes its physics calculations. It can be changed in Time Manager, reducing the value of Fixed Timestep. Beware of reducing this too much, or the physics engine will be unable to finish a call before the next round is supposed to start and it will never be able to catch up with the game.

设置移动物体的最大速度是必须始终要做的事情.您不能冒险在游戏过程中让重要的物体飞涨,并使一切保持不受控制的状态.

Setting a maximum speed for moving objects is something that must always be done. You cannot risk having an important object skyrocket during the game and leave everything in an uncontrolled state.

这篇关于用鼠标控制的球拍对快速移动的球进行碰撞检测的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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