In the block breaking game using tmlib.js, the collision is set as follows:
this.setBoundingType("circle");
on the ball
this.setBoundingType("rect");
The collision determination determines that it is a collision between circle
.
if (this.isHitElement(paddle))
(This is the ball)
Here is the specific source code (created at the runstant site).
http://goo.gl/RjXC6v
I have quoted the following code for "11. Ball and Paddle Hit Determination".
"""Let's make a game with tmlib.js!"""
http://qiita.com/h_mjlife/items/94c7381ff56966cceccb
Change the fps value, size of the ball and paddle, and position of the paddle to make it easier to understand, and cut the ball positioning when hit.
tmlib.js
If you look at the source of the main body (isHitElement
in tm.app.Object2D.js
), you can see that circle
and rect
are:
It doesn't seem to come off.Is there any good way to judge?
The internal behavior of tmlib.js is the calling side of isHitElement. In other words, this type of bounding is the preferred implementation.
The collision determination between circle and rect has a larger load than circle x circle and rectangle x rectangle. That's how it's implemented.
If it is absolutely necessary, I will make it as a separate method!
I made a temporary collision decision between circle and rect.
runstant ->http://goo.gl/ABuxY4
code
var testCircleRect=function(circle,rect){
// First, use a large rectangle to judge (speed up)
var bigRect=tm.geom.Rect(rect.left-circle.radius, rect.top-circle.radius, rect.width+circle.radius*2, rect.height+circle.radius*2);
if(bigRect.contains(circle.x,circle.y)==false){
return false;
}
// Collision determination with two rectangles
var=tm.geom.Rect(rect.left-circle.radius, rect.top, rect.width+circle.radius*2, rect.height);
if(r.contains(circle.x,circle.y)){
return true;
}
r.set(rect.left, rect.top-circle.radius, rect.width, rect.height+circle.radius*2);
if(r.contains(circle.x,circle.y)){
return true;
}
// four-point determination of a circle and a rectangle
varc=tm.geom.Circle(circle.x,circle.y,circle.radius);
// left top
if(c.contains(rect.left, rect.top)){
return true;
}
// right top
if(c.contains(rect.right,rect.top)){
return true;
}
// right bottom
if(c.contains(rect.right, rect.bottom)){
return true;
}
// left bottom
if(c.contains(rect.left, rect.bottom)){
return true;
}
return false;
};
As expected, the cord gets longer.
However, I can play it quite often with my head, so I think I can put it in.
I'll reconsider.
© 2024 OneMinuteCode. All rights reserved.