[英]collision detection algorithm issue
here is the jsfiddle i am working on so far : http://jsfiddle.net/TLYZS/ 这是到目前为止我正在使用的jsfiddle: http : //jsfiddle.net/TLYZS/
if you debug the code and check the collision function you can see that the collision is working fine when the user overlap with the other character wish in a fighting game the collision should not work like this : 如果您调试代码并检查碰撞功能,则可以看到当用户与格斗游戏中的其他角色希望重叠时,碰撞工作正常,则碰撞不应像这样进行:
how can i fix this collision detection function to make it work fine ? 如何修复此碰撞检测功能以使其正常工作?
function Collision(r1, r2) { return !(r1.x > r2.x + r2.w || r1.x + r1.w < r2.x || r1.y > r2.y + r2.h || r1.y + r1.h < r2.y); }
Here is my old JS implementation of flash.geom.Rectangle. 这是我的Flash.geom.Rectangle的旧JS实现。 Try to use it in your project: 尝试在您的项目中使用它:
function Rectangle(x, y, w, h) { // constructor
if(x==null) x=y=w=h=0;
this.x = x; this.y = y;
this.width = w; this.height = h;
}
Rectangle.prototype.isEmpty = function() { // : Boolean
return (this.width<=0 || this.height <= 0);
}
Rectangle.prototype.intersection = function(rec) { // : Rectangle
var l = Math.max(this.x, rec.x);
var r = Math.min(this.x+this.width , rec.x+rec.width );
var u = Math.max(this.y, rec.y);
var d = Math.min(this.y+this.height, rec.y+rec.height);
if(r<l || d<u) return new Rectangle();
else return new Rectangle(l, u, r-l, d-u);
}
Then you just call 那你就打电话
var r1 = new Rectangle( 0, 0,100,100);
var r2 = new Rectangle(90,90,100,100);
// r1.intersection(r2) would be Rectangle(90,90,10,10)
if( !r1.intersection(r2).isEmpty() ) ... // there is a collision
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.