hit-test
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

hitTest

简单2D碰撞检测

Install

You can install it via npm:

npm install hit-test

Use

import hitTest from "hit-test";

javascript:

<script src="dist/hitTest.js"></script>

Example

矩形 & 矩形

   var rect1={
       x:0,
       y:0,
       width:50,
       height:60
   };
 
   var rect2={
       x:50,
       y:80,
       x:100,
       height:80
   };
 
   if(hitTest.Rectangle(rect1,rect2)){
       console.log("碰撞");
   }
   else{
       console.log("不碰撞");
   }
   

圆形 & 圆形

   var circle1={
       x:0,
       y:0,
       radius:10
   };
 
   var circle2={
       x:50,
       y:80,
       radius:15
   };
 
   if(hitTest.Circle(circle1,circle2)){
       console.log("碰撞");
   }
   else{
       console.log("不碰撞");
   }

矩形 & 圆形

   var rect1={
       x:0,
       y:0,
       width:50,
       height:60
   };
 
   var circle2={
       x:50,
       y:80,
       radius:15
   };
 
   if(hitTest.RectangleCircle(rect1,circle2)){
       console.log("碰撞");
   }
   else{
       console.log("不碰撞");
   }

矩形(带旋转角) & 圆形

只能判断以矩形中心为旋转中心的情况

   var rect1={
       x:0,
       y:0,
       width:50,
       height:60,
       rotation:1.3    //弦度
   };
 
   var circle2={
       x:50,
       y:80,
       radius:15
   };
 
   if(hitTest.RectangleCircleAngle(rect1,circle2,rect1.rotation)){
       console.log("碰撞");
   }
   else{
       console.log("不碰撞");
   }

多边形 & 多边形

  var points1=[194,212, 235,170,216,107,136,136,146,196];
  var points2=new Float32Array([250, 208, 305,247,402, 238, 340, 150]);
 
   if(hitTest.Polygon(points1,points2)){
       console.log("碰撞");
   }
   else{
       console.log("不碰撞");
   }

Readme

Keywords

Package Sidebar

Install

npm i hit-test

Weekly Downloads

156

Version

1.0.0

License

ISC

Unpacked Size

7.75 kB

Total Files

5

Last publish

Collaborators

  • anderpang