Prozi / detect-collisions

Points, Lines, Boxes, Polygons (also hollow), Ellipses, Circles. RayCasting, offsets, rotation, scaling, bounding box padding, flags for static and ghost/trigger bodies
https://prozi.github.io/detect-collisions/
MIT License
207 stars 22 forks source link

3D collision is it possible? #56

Closed sefirosweb closed 1 year ago

sefirosweb commented 1 year ago

Hi guys, one question this package can calculate the collision boxes of 3D objects? Ex raycast x cube or cube x cube

Sorry if my question is stupid i dont know if one dimension extra is huge more hard

Best!

Prozi commented 1 year ago

@sefirosweb thank you for opening an issue, will have to give it more thought

I suppose rbush which is used for box collision would not be enought for it, but mayme somehow...

Codezilluh commented 1 year ago

Personally, I think it is better to keep 2D and 3D collisions in separate libraries. If all your objects are extremely simple (like cubes and spheres, but not polygons), you could try having three CollisionSystems (one for xy, one for yz, and one for xz). That is extremely hacky though and probably sucks performance-wise, so I wouldn't necessarily recommend it.

sefirosweb commented 1 year ago

thanks for clarify guys,

I'm making it detect 3 solutions for a raycast (XY / XZ / ZY) but as you indicate the adaptation is a bit stinky

Prozi commented 1 year ago

Personally, I think it is better to keep 2D and 3D collisions in separate libraries. If all your objects are extremely simple (like cubes and spheres, but not polygons), you could try having three CollisionSystems (one for xy, one for yz, and one for xz). That is extremely hacky though and probably sucks performance-wise, so I wouldn't necessarily recommend it.

to build up from @Codezilluh answer

@sefirosweb I believe you'd need 2 collision systems each body would have to have bboxXY and bboxXZ

on a side note - it's not always necessary to have 3d detection in a 3d game, example: https://games.pietal.dev/space/ (use WSAD to move, space to shoot)

Prozi commented 1 year ago

maybe start with something like

class XY extends RBush {}
class XZ extends RBush {
  compareMinY(a, b) { return a.minZ - b.minZ }
}
class BodyXYZ {
  bbox = {
    minX: number;
    maxX: number;
    minY: number;
    maxY: number;
    minZ: number;
    maxZ: number;
  }

  updateBody(xy: XY, xz: XZ) {
    xy.remove(this.bbox);
    xz.remove(this.bbox);

    // update bbox here somehow

    xy.insert(this.bbox);
    xz.insert(this.bbox);
  }
}

RBush from https://github.com/mourner/rbush

Prozi commented 1 year ago

@sefirosweb actually maybe use this https://github.com/Eronana/rbush-3d

sefirosweb commented 1 year ago

Thanks Prozi, i cheeck that but seems they don't support box rotatio, I was also looking at three js which is very powerful with 3D but I think I can't use the isolated collision system

Prozi commented 1 year ago

yes box rotation is part of separaring axes theorem checking collision between 2 bodies. the rbushes are for broad phase search optimisations and always use boxes (or cubes in 3d)