阻断运动对碰撞 [英] Blocking Movement On Collision

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

问题描述

我开发XNA中的2D游戏,我目前正在对我的物理课。和我当前的任务是当它与另一个发生碰撞,停止的对象。
I使我的游戏调用这个函数当过2物体发生碰撞:

i'm developing a 2d game in XNA and i'm currently working on my physics class. and my current task is to stop an object when it's colliding with another. i have made my game call this function when ever 2 objects collide:

public void BlockMovement(gameObject target) {
    //you can call width by just typing width. same for height
    owner.position.X += (owner.position.X - target.position.X);
    owner.position.Y += (owner.position.Y - target.position.Y);
} 



问题是,而不是停止。被移动通过它的宽度的量推回该对象。
可有人请告诉/你能告诉我如何做到这一点我一直被困在这个在许多项目的sooo长。

the problem is that instead of stopping. the object that was moving is pushed back by the amount of it's width. can someone please show/tell me how to do this i have been stuck on this for sooo long in many projects.

感谢

推荐答案

您没有把目标的和业主的高度和宽度计算入内。哪里是X,目标/所有者的Y'中间?左上?

You are not putting target's and owner's width and height into calculation. Where is the X,Y of a target/owner? Middle? Left-Top?

如果碰撞是头对头然后

velocity=sqrt(x_velocity*x_velocity+y_velocity*y_velocity); //some pseudo   

public void BlockMovement(gameObject target) {
    //you can call width by just typing width. same for height
    owner.position.X -=owner.x_velocity*penetration_length/(owner.velocity);
    owner.position.Y -=owner.y_velocity*penetration_length/(owner.velocity);
} 

//so, if big part of speed is in x-direction then the position will
//change mostly on x-direction and opposite to velocity

如果你没有一个速度可变的,可以引入的2次迭代的位置历史以计算实际速度的方向。

If you dont have a velocity variable, you can introduce a position history of 2 iterations back to compute actual velocity direction.

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

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