Giter Site home page Giter Site logo

bmoren / p5.collide2d Goto Github PK

View Code? Open in Web Editor NEW
568.0 15.0 297.0 330 KB

A collision detection library for 2D geometry in p5.js

License: Other

JavaScript 100.00%
collision-detection p5js 2d-collision-library p5 p5js-game p5xjs p5-library p5-js hitbox

p5.collide2d's People

Contributors

bmoren avatar conradoqg avatar davidgranstrom avatar dependabot[bot] avatar eranws avatar fidoaf avatar gotoloop avatar ognjenvucko avatar ream88 avatar rikdewit avatar tektsu avatar wisehackermonkey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

p5.collide2d's Issues

statically hosted file

need to add a statically hosted file that can be used to embed script in sketches without actually downloading it

How do I use this in p5 instance mode and React?

Hi there, I'm trying to incorporate using p5.collide2D into React. To do that, I'm using p5-react-wrapper and also I need to write my p5 sketch in instance mode.

However I'm having trouble using this library.

I did the import "p5.collide2d" and if I try to use it p5.collidePointRect... I get an error.
I'm sure the library is loaded because I see the following in the console.
### p5.collide v0.7.3 ###.

However I'm not sure how to call the API.

Interior option for collidePolyPoly()

I was having issues with collidePolyPoly() not detecting collisions when one polygon is inside another. Looking at the source I see that collidePolyPoly() detects when p2 is inside p1, but not when p1 is inside p2. It seems like that is an equally valid collision, unless I am misunderstanding something.

RectMode Center

the rect mode center mode is not supported in the library

npm package

Please publish this package on npmjs.com ๐Ÿ˜Š

Undefined parameters for collidePointPoint

Just a small note, on line 382

p5.prototype.collidePointPoint = function (x,y,x2,y2, buffer) {
    if(buffer == undefined){
      buffer = 0;
    }

    if(dist(x,y,z,x2) <= buffer){//I think this should be (x,y,x2,y2)?
      return true;
    }

  return false;
};

Collide rotated rects

p5.collide2d.js lib can't collide rotated rects.
I'm suggesting to add this feature, bcs I lost much time to write it myself.

/*
 * collide rotated rects  
 */

// get corners of rects
// x - center x of rect
// y - center y of rect
// w - width of rect
// h - height of rect
// a - angle
p5.prototype.getCorners = function getCorners(x, y, w, h, a) {
  return {
    get topLeft() {
      var cx = x - ((w / 2) * cos(a)) + ((h / 2) * sin(a));
      var cy = y - ((w / 2) * sin(a)) - ((h / 2) * cos(a));
      return {
        x: cx,
        y: cy
      };
    },
    get topRight() {
      var cx = x + ((w / 2) * cos(a)) + ((h / 2) * sin(a));
      var cy = y + ((w / 2) * sin(a)) - ((h / 2) * cos(a));
      return {
        x: cx,
        y: cy
      };
    },
    get bottomLeft() {
      var cx = x - ((w / 2) * cos(a)) - ((h / 2) * sin(a));
      var cy = y - ((w / 2) * sin(a)) + ((h / 2) * cos(a));
      return {
        x: cx,
        y: cy
      };
    },
    get bottomRight() {
      var cx = x + ((w / 2) * cos(a)) - ((h / 2) * sin(a));
      var cy = y + ((w / 2) * sin(a)) + ((h / 2) * cos(a));
      return {
        x: cx,
        y: cy
      };
    },
    toArray: function() {
      return [
        this.topLeft,
        this.topRight,
        this.bottomRight,
        this.bottomLeft
      ];
    }
  }
}

// collide 2 rects by corners as a polies

p5.prototype.collideRects = function collideRects(x1, y1, w1, h1, a1, x2, y2, w2, h2, a2) {
  var c1 = p5.prototype.getCorners(x1, y1, w1, h1, a1).toArray();
  var c2 = p5.prototype.getCorners(x2, y2, w2, h2, a2).toArray();
  return p5.prototype.collidePolyPoly(c1, c2, true);
}

// vector version of colliderects

p5.prototype.collideRectsVector = function collideRectsVector(p1, s1, a1, p2, s2, a2) {
  return p5.prototype.collideRects(p1.x, p1.y, s1.x, s1.y, a1, p2.x, p2.y, s2.x, s2.y, a2);
}

my version of code

Support for instantiated sketches?

Is there a way to use this library from an instantiated sketch, currently I can call the the collide2d methods using the namespace created, i.e. p.collideCirclePoly() but when subsequent methods are called an error is thrown as these are not found in the global scope.

Chrome: index.js:258 Uncaught ReferenceError: collideLineCircle is not defined

Could be I'm missing something simple.
Many thanks.

Add unit tests for all function to validate on commit

js unit tests with examples of correct collisions. and examples of non collisions

pros

  • reduce time required to make updates to function and not have to manully test each one

cons

  • having to write all those unit tests :)

CollideRectPoly has some broken functionality

When using the collideRectPoly method with the internalCollision option set to true, everything works fine until the rectangle is bigger than the polygon. Once the rect is bigger and the polygon is inside the rect, it will not result in an output even with the internalCollision option set. It works fine when the rect is smaller and is inside the polygon though. Not sure if this is intended or not but definitely caused some issues in my project and I had to switch to using collidePolyPoly. Example: https://editor.p5js.org/akgaming1322/sketches/-zQIP8tAj

CollidePointPoint not guaranteed to detect point-point collisions with default buffer

I tried using collidePointPoint in my missile command game @ https://matthew-herman.github.io/personal-website/projects.html, using collidePointPoint without the buffer argument. After pressing spacebar, the aim is to have the blue line terminate when reaching the target white cross. However, using collidePointPoint sometimes does not detect the collision. I coded it as follows:

		if (collidePointPoint(this.xPos, this.yPos, this.xTarget, this.yTarget)) {
			var index = arrFiredMissiles.indexOf(this);
			arrFiredMissiles.splice(index, 1);
			arrExplosions.push(new Explosion(this.xPos, this.yPos));
		}

I think this is due to the use of floating points. I think this behavior with the default buffer should be explained in the readme because it could cause bugs, which are not easy to trace.

CDN support?

Hey great work on the collision detection routines.

How can we add that to CDN?

Can you guys add some functionality?

don't be wrong, it's a pretty helpful plugin for my games. but, it's only detect the collision which only a bool, I have to smuggle some code so I know which direction the collision came in a rect to rect and circle to circle. so then I can use it on my platformer game. Can you guys add that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.