2D弹跳公式无法正常工作 [英] 2D bouncing formula doesn't work properly

查看:101
本文介绍了2D弹跳公式无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个团结的新手,我正在尝试制作一个弹跳球,所以我对弹跳现实物理学进行了许多研究,发现了一个公式:

I am new to unity, and i am trying to create a bouncing ball, so i've did many researches about bouncing realted physics and i found a formula :

公式:

-2*(V dot N)*N + V

其中V是速度矢量,N是球将反弹的表面的法线

Where V is the velocity vector and N is the normal of the surface on which the ball will bounce

这是我的剧本:

using UnityEngine;

using System.Collections;

public class BallPhysics : MonoBehaviour {



    void Start () {
        rigidbody2D.velocity =new Vector2 (-1,-3);

    }

    // Update is called once per frame
    void Update () {

    }
    void OnTriggerEnter2D(Collider2D col) {

        if (col.gameObject.name == "Pong") {
            tBounce ();
                }


    }

    void tBounce(){
        RaycastHit2D hit = Physics2D.Raycast (new Vector2 (transform.position.x,transform.position.y), new Vector2(-1f,-1f));
        Vector2 n = hit.normal;
        Vector2 v = rigidbody2D.velocity;
        Vector2 R = -2 * (Vector2.Dot (v, n)) * n + v;
        rigidbody2D.velocity = R;
    }
}

我在开始功能中为球提供了速度矢量,我使用OnTriggerEnter2D进行碰撞处理,并使用raycast2D获得曲面的法线.

I am giving the ball a velocity vector in the start function, i am using OnTriggerEnter2D for collision handling and raycast2D to get the normal of a surface.

问题在于脚本无法反映称为R的速度矢量,我认为问题出在法向矢量中.

The problem is that the script doesn't reflect the velocity vector called R, i think the probleme is in the normal vector.

例如,假设V是一个Vector2(-1,-1),那么基本上R应该是(-1,1),但事实并非如此. R是(3,1)!

For example let's say V is a Vector2(-1,-1) so basically R should be (-1,1), but it's not. R is (3,1) !

我已经成功地通过反转球的速度使球在水平/垂直表面上弹跳,但是这在倾斜角度下无法正常工作,这就是为什么我使用此公式. 那是什么问题呢?

i've successfuly been able to make a ball bouncing on Horizontal/vertical surface by reversing the ball velocity but this won't work properly with arbitary angles,that's why i am using this formula. So what's the problem ?

推荐答案

您的光线投射可能碰到了错误的对撞机.您从transform.position启动它.那是您的球形物体的中心点.

Your raycast is likely hitting the wrong collider. You start it at transform.position. That is the center point of your ball object.

想象一下,光线从圆心发出.它打的第一行是什么?它首先击中了圆圈本身.因此,您的Raycast报告的是球的表面法线,而不是乒乓球拍.

Imagine a ray coming out of a circle's center. What's the first line it hits? It hits the circle itself first. So your Raycast reports the surface normal of the ball, not the pong paddle.

有很多方法可以解决此问题.您可以应用物理层来忽略射线广播.我认为有一个这样的预定义.您还可以创建自定义对象,并为raycast提供一个图层蒙版.

There are many ways to get around this. You can apply a Physics Layer that ignores raycasts. I think there is one pre-defined like that. You can create custom ones as well and supply a layer mask to your raycast.

还有其他解决方法,例如从其他地方发出射线广播,但是由于您是在物体碰撞时执行此操作的,因此图层蒙版可能是最简单的.

There are other ways to solve it like originating the raycast from somewhere else, but since you're doing this as the objects collide, a layer mask is probably the simplest.

这篇关于2D弹跳公式无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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