- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望有人能回答我的问题。目前我正在使用 D3DXVectors 进行一些碰撞检测。我已解决碰撞问题,但我需要的是在发生碰撞后要做什么。
我有一个玩家对象和玩家正在与之碰撞的对象,并将它们传递到我的函数中。
基本上,到目前为止我已经有了这个(为清楚起见,变量名称已更改):
D3DXVECTOR3 Collision::wallCol(D3DXVECTOR3 player, D3DXVECTOR3 object, float playerWidth, float playerDepth, float objectWidth, float objectDepth)
{
//store the values of the objects into temps
//left hand side of a box
float minX = object.x - (objectWidth / 2);
//right hand side of a box
float maxX = object.x + (objectWidth / 2);
//front of a box
float minZ = object.z - (objectDepth / 2);
//back of a box
float maxZ = object.z + (objectDepth / 2);
//store the players positions into temps
float pminX = player.x - (playerWidth / 2);
float pmaxX = player.x + (playerWidth / 2);
float pminZ = player.z - (playerDepth / 2);
float pmaxZ = player.z + (playerDepth / 2);
//check to see if we are in the box at all
if (pminX <= maxX && pmaxX >= minX &&
pminZ <= maxZ && pmaxZ >= minZ)
{
//x collision for the left side of the block
if (pmaxX >= minX && pmaxX <= maxX)
{
pmaxX = minX - 50;
player.x = pmaxX;
return player;
}
//x collision for the right side of the block
if (pminX <= maxX && pminX >= minX)
{
pminX = maxX + 50;
player.x = pminX;
return player;
}
//z collision for the front
if (pmaxZ >= minZ && pmaxZ <= maxZ)
{
pmaxZ = minZ - 20;
player.z = pmaxZ;
return player;
}
//z collision for the back
if (pminZ <= maxZ && pminZ >= minZ)
{
pminZ = maxZ + 20;
player.z = pminZ;
return player;
}
}
return player;
}
如您所见,我想将我的播放器对象从它撞到的任何地方移开。我遇到的问题是,无论如何,我的播放器总是会在 X 轴上向左或向右移动,因为它首先考虑 X,它甚至没有机会到达 Z if 语句。
我考虑过制作 2 个 bool 值来判断玩家是先与 X 还是 Z 接触,但由于第一个 if 语句是为了查看我们是否在盒子里,我相信它会在同一时间,无法分辨哪个先出现。
如果有人能就这个问题提供一些建议,将不胜感激!谢谢。
最佳答案
为了实现或多或少逼真的弹跳,仅有碰撞对象的位置和边界框是不够的。您需要了解某种运动学历史:要能够了解对象在 碰撞之后如何移动,您还应该知道它们在 碰撞之前如何移动。
在最简单的情况下,您希望每个对象至少有一个速度(速度) vector (方向和幅度),并且可能还有一个质量。当检测到碰撞时,您可以简单地反转其中一个物体(例如,更小更轻的物体)的速度 vector 的方向。
伪代码是这样的:
if have_collided(object1, object2):
if object1.mass > object2.mass:
object2.velocity = -object2.velocity
else:
object1.velocity = -object1.velocity
这看起来不太现实,但实现起来非常简单。要获得更真实的效果,您可以使用动量守恒 原理或弹性碰撞。要获得更逼真的弹跳,请实现加速度 vector 并在检测到碰撞后重新计算速度和加速度。有了加速度,您现在可以添加影响对象的任意力(重力、摩擦力,甚至可能是电磁力等)。添加更多属性使其更有趣:例如,添加弹性和粘性可用于计算变形。
如您所见,对象交互涉及的不仅仅是位置。
此外,碰撞检测和弹跳代码可以(也应该)分离。这意味着您可能希望在不同的代码部分(不同的函数和类集)实现它们:碰撞子系统将检测碰撞事件,提供如下信息:碰撞是否发生,如果是,对象当前相交的距离彼此。之后,基于这些信息,弹跳代码将决定如何改变物体的行进,而变形代码将决定如何改变物体的形状等。因此,不会出现碰撞代码中哪个“if”语句先出现的愚蠢问题决定弹跳的方向。
总体而言,我建议您:
查阅一些物理知识。例如:
要获得这本优秀的书:Real-Time Collision Detection
通读关于 gamedev.stackexchange.com 的一些热门问题,例如 collision (按投票排序)
<子>最后,您应该知道 DirectX 9 API 已经过时,不受支持,不应该用于新项目。众所周知,将 D3DX* 内容移植到现代 API 是非常困难的。你可能想看看 DirectX 11和 OpenGL 4 ,连同他们的支持库生态系统,甚至可能是一个好的渲染或游戏引擎。具体来说,您可能想使用一些 good math library (带有 vector 、矩阵、线性代数、几何)。
关于c++ - 使用 D3DXVectors c++ 进行碰撞检测,发生碰撞后要做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49568266/
我正在用 jQuery Collision 编写这个游戏,它使用键盘按键来移动 div,当一个 div 接触另一个 div 时,它应该防止重叠。 我到底该怎么做? HTML ----
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java 2D Collision? 嘿,大家好,我有另一篇关于这个问题的帖子刚刚消失了,所以我想我会尝试得到一些关
嘿伙计们,我正在制作一个 2D java 游戏,我正在尝试找出如何制作一个好的碰撞代码。我目前正在使用以下代码: public void checkCollision() { Rect
我的意思是,当我与实体的侧面碰撞并想要跳跃时,我无法向右/向左移动,因为当我与右侧/左侧的实体碰撞时,我有一个标志可以防止这种情况发生,例如所以: 这是我用来检测碰撞的代码: public void
所以我正在运行 collide_mask 检查,以删除与玩家 Sprite 碰撞时的生物实例。它工作得很好。 pygame.sprite.spritecollide(player, mobs, Tru
我正在研究我的砖 block splinter 机,并制作一个适当的碰撞系统,以便使球逻辑地切换方向,我必须检测球与砖 block 的哪一侧碰撞。这是我当前的脚本: int sprite_collid
我做了一个类似颜色切换的游戏。唯一的问题是玩家与每种颜色发生碰撞...... 这是我从github上获取的代码: https://github.com/prometheon/MLNimbleNinja
测试我的游戏,当用户和怪物发生碰撞时,我希望弹出警报但没有成功: function die() { for (var i = 0; i < monster.length; i++) { i
我对 vector 很陌生,这是我第一次真正使用它们进行碰撞检查。这是我的项目,我对如何实现碰撞感到困惑。我目前的碰撞检查和响应代码似乎是……糟糕的设计。 这是我的代码: for(auto it =
我是 javascript 的新手,正在尝试找出如何与球和木板发生碰撞,这将停止游戏并提醒玩家“你输了”。但我只想让红球击中木板,让蓝球不接触地继续前进。这是我正在处理的代码。 (我不介意你是否可以帮
很抱歉提出奇怪的问题,我还是 Android 编程的新手。 我有以下代码: import android.content.DialogInterface.OnClickListener; import
我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,它们都连接到相同的方法。方法是: - (IBAction)handlePan:(UIPanGestur
我想根据某些对象的轴对齐边界框检查视锥体,以粗略检查这些对象是否在视野中。速度不是什么大问题。 最佳答案 我发现构建视锥体的世界空间模型并检查与它的 bbox 碰撞是错误的方法。 一个更简单的方法是以
我项目中的所有这些代码都运行良好,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我不知道如何让它在碰到屏幕边界时不旋转。只有在我使用时才会出现这个问题: plane.physic
在应用程序启动时,我在后台线程中删除旧的 CoreData 行,下面是我的代码。我的问题类似于城市街道问题。所以,我有两个实体,Street 和 City,我有一个关系 City > Street,因
我试图不接触穴居人和其他带有碰撞位掩码的图像,但我的穴居人击中了一切。 func addCaveManBitMasks(){ caveManNode.physicsBody?.category
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
如何在 PyGame 中找到字符和图像之间的碰撞?我已经从图像中绘制了一个玩家,并从瓷砖中绘制了墙壁,那么我如何检测这些碰撞? 最佳答案 如果你使用pygame Rect类来表示对象的边界,您可以使用
我正在使用 ftok() 为 C 应用程序使用的共享内存段生成标识符。我有问题,在一个盒子上我与 root 使用的标识符发生冲突。在这种情况下,我可以通过破解代码来修复它,但我想要一个更强大的解决方案
这个问题在这里已经有了答案: JavaScript: Collision detection (10 个回答) 10 个月前关闭。 检测 2 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我是一名优秀的程序员,十分优秀!