1 decade ago
by Xatruch
Hi, does anybody knows how to make collision between hexagonals? The default collision is with rectangles and uses the function touches() in entity.js
touches: function( other ) {
return !(
this.pos.x >= other.pos.x + other.size.x ||
this.pos.x + this.size.x <= other.pos.x ||
this.pos.y >= other.pos.y + other.size.y ||
this.pos.y + this.size.y <= other.pos.y
);
},
i'm looking for something like:
hexTouches: function( other ) {
return !(
// hex collision
);
},
Thanks
1 decade ago
by Joncom
You may need a more sophisticated physics engine. Check out
Box2D.
Edit: I think I've seen you post around a few Box2D threads already, so I'll assume you've dabbled with it. Does it not do exactly what you're asking?
Edit : sorry my answer was wrong, i removed it.
1 decade ago
by Xatruch
Hi thanks for the replies.
@Joncom Yes Box2D is pretty good but also heavy, was looking for a more lightweight solution as I just wanted to experiment with hexagons.
@vincentpiel have you experienced with hex on games before? I still can't find a solution, and all what i've found is a check for each edge of polygons like box2d does.
If I find anything will post it.
Thanks
1 decade ago
by drhayes
Here's my new favorite resource for all things hexagonal. It doesn't cover collision specifically, however.
Here's another great tutorial about collision and the separating-axis theorem. It doesn't cover hexes, though.
Are the hexes axially-aligned (i.e. they never rotate)? How are you storing their extents, size, position, etc?
1 decade ago
by Xatruch
Hi drhayes thanks alot for the links.
Well to be honest I just have the idea to make a hex board game but got no code for now. But yes hexes don't rotate. Just wanted to check for example when the player clicks on a hex.
1 decade ago
by drhayes
Just talking out of my ass...
If you're just hit-testing you can treat a hexagonal grid as a less dense square grid. If the hexes are flat-topped:
__
/ \
\__/
Then each hex has a square inside of it:
__
/||\
\||/
Now you have two cases: did I click within the square in the center of a hex, or did I click within the square made up of the triangles that are the "points" of four neighboring hexes? The second one is the only one that's complex and, even then, it's just got some algebra.
Page 1 of 1
« first
« previous
next ›
last »