简体   繁体   English

C#Monogame和Farseer物理:碰撞

[英]C# Monogame & Farseer Physics: Collisions

I am a begginer. 我是一个初学者。 I am using Monogame and Farseer Physics Library in C#. 我在C#中使用Monogame和Farseer物理库。 (latest versions) (最新版本)

In my game, whenever my ball (circle) hits the corner of a rectangle , (or even another circle), it is supposed to only change direction on the Y-Axis. 在我的游戏中,只要我的ball (圆)碰到corner of a rectangle (或什至另一个圆)的角,它就只会改变Y轴的方向。

However, it does not only change direction on the Y-Axis, (as intended), but it also moves a small amount to the right as well (or the left accordingly), depending on how you hit the corner (or another circle). 但是,它不仅会改变Y轴的方向(如预期的那样),还会根据向角(或另一个圆圈)的方向向右(或向左)少量移动。 。 It is as if some force is being put on the ball, making it move on X-Axis as well. 好像在球上施加了一些力,使其也沿X轴移动。

http://i.imgur.com/rEbNTua.png?1

This movement is cool and all, and it makes a lot of sense, but in my game, it does not, therefore i want to get rid of it. 这个动作很酷,而且很有意义,但是在我的游戏中却没有,所以我想摆脱它。

How is this possible ? 这怎么可能 ? I am guessing i have to change some default values. 我猜我必须更改一些默认值。

This what my coding looks like: 我的代码如下所示:

BallBody.BodyType = BodyType.Dynamic;
BlockBody.BodyType = BodyType.Static;
Ball.LinearVelocity = new Vector(0,-1); // ball going up
BallBody.OnCollision += Ball_OnCollision;

public bool Ball_OnCollision(Fixture f1, Fixture f2, Contact contact)
{
    // if the Ball (f1), collides with the Block (f2)
    if (f2.Body == BlockBody)
    // change the direction of the Ball on Y-Axis
    Ball.LinearVelocity = new Vector(0,-1);
    return true;
}

Also with high speeds, this occurs: 同样在高速下,会发生以下情况:

在此处输入图片说明

Even though the ball is never able to pass through the Block (tunneling), I want to know how could i possible fix that so that the ball never enters the Block area ? 即使球永远无法通过障碍(隧道),我也想知道如何解决这个问题,使球永远不会进入障碍区?

Unfortunately, this is surprisingly difficult to do with Box2D. 不幸的是,使用Box2D很难做到这一点。 The best I could manage when I tried, was to record the velocity of the ball when the contact started, and then when the contact ends, manually set the velocity to what I wanted. 尝试时最好的办法是记录接触开始时的球速,然后在接触结束时手动将速度设置为所需的速度。 That works fine when the ball only begins and end contact with one block at a time, but in some cases the ball can begin touching two blocks in the same time step. 当球仅开始并一次与一个块结束接触时,这种方法就很好了,但是在某些情况下,球可以在同一时间步中开始接触两个块。 You would have to look at the arrangement of the blocks it touched, and calculate for yourself what angle you would expect it to bounce away at. 您将必须查看它所触摸的块的排列,并为自己计算希望其反弹的角度。 Eventually I just gave up and did not bother to cover that case. 最终,我只是放弃了,没有理会这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM