碰撞检测不起作用

碰撞检测不起作用

本文介绍了为什么我的 3D 碰撞检测不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的世界中有几个对象,主要分为三种类型.第一个是 self,也就是玩家,玩家不会移动,但会旋转.第二种是从用户身上直线飞出的子弹.第三个是目标,它们只是坐在某处等待被击中.

I have several objects in my world, there are three primary types. The first is self, which is the player, the player does not move, but they do rotate. The second are bullets which fly out away from the user in a straight line. And the third are targets, these just sit somewhere and wait to be hit.

这是我用来进行碰撞检测的代码,但它不起作用:

Here's the code I'm using to do collision detection and it isn't working:

        foreach (GameObject go in bullets) {
            float goRadius = go.Model.Meshes.Sum((x) => x.BoundingSphere.Radius) * go.Scale;
            EnemyObject last = null;

            Vector3 goRealPos = Vector3.Transform(go.Position, Matrix.CreateScale(go.Scale) * Matrix.CreateTranslation(go.Position) * Matrix.CreateRotationY(go.Rotation.Y));

            foreach (EnemyObject eo in targets) {
                float eoRadius = eo.Model.Meshes.Sum((x) => x.BoundingSphere.Radius) *eo.Scale;

                if (Vector3.Distance(eo.Position, goRealPos) < eoRadius + goRadius) {
                    //collision has occured
                    if (!eo.TakeHit()) {
                        last = eo;
                    }

                    //remove bullet
                    toBeRemoved.Add(go);

                    break;
                }
            }

            if (last != null) {
                targets.Remove(last);
            }
            if (go.Position.Z > 2000 || go.Position.Z < -2000) {
                toBeRemoved.Add(go);
            }
        }

谁能告诉我为什么它不起作用?

Can anyone tell me why it isn't working?

推荐答案

您是否检查过您对 goRealPos 的计算是否获得了您期望的大致坐标?为什么你要从 go 转换 goRealPos,但没有为 eo 做类似的事情?可能是您正在测试不在同一坐标系上的对象之间的碰撞.

Did you check that your calculation for goRealPos is getting approximately the coordinate you expect? Why are you transforming goRealPos from go, but not doing anything similar for eo? It could be that you are testing for collisions between object that aren't on the same coordinate system.

这篇关于为什么我的 3D 碰撞检测不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:05