检测碰撞方向 [英] Detecting the Direction of a Collision

查看:76
本文介绍了检测碰撞方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个正方形瓷砖与另一个正方形瓷砖碰撞。酒保说...

A square tile collides with another square tile. The bartender says...

我有:


  • 高度,宽度

  • 引起碰撞的运动的二维矢量。

我需要从哪个侧面知道发生了碰撞(例如,顶部,底部,左侧,右侧),以便适当地重置位置。

I need to know from what SIDE the collision occurred (e.g. top, bottom, left, right) in order to reset the location appropriately.

我将给出对任何人都可以回答这个问题的心理cookie,因为我已经尝试了很多小时,这似乎很重要。

I will give a mental cookie to whoever can answer this question, because I've been trying for too many hours and this seems fundamental.

推荐答案

float player_bottom = player.get_y() + player.get_height();
float tiles_bottom = tiles.get_y() + tiles.get_height();
float player_right = player.get_x() + player.get_width();
float tiles_right = tiles.get_x() + tiles.get_width();

float b_collision = tiles_bottom - player.get_y();
float t_collision = player_bottom - tiles.get_y();
float l_collision = player_right - tiles.get_x();
float r_collision = tiles_right - player.get_x();

if (t_collision < b_collision && t_collision < l_collision && t_collision < r_collision )
{                           
//Top collision
}
if (b_collision < t_collision && b_collision < l_collision && b_collision < r_collision)                        
{
//bottom collision
}
if (l_collision < r_collision && l_collision < t_collision && l_collision < b_collision)
{
//Left collision
}
if (r_collision < l_collision && r_collision < t_collision && r_collision < b_collision )
{
//Right collision
}

当对象位于另一个对象中时,这不能解决。但这确实适用于重叠

This doesn't solve when object is inside one of the other. But it does work with overlapping

这篇关于检测碰撞方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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