I’ve been trying to come up w/ decent player collisions for my game, but I feel like I could be doing them in a much better way. I want to strike up a conversation with other people who have written decent player collisions with triangles.
Here’s what I’ve done for my engine (Keep in mind I wrote my own triangle raytracing library, so my methodology may seem a little unconventional):
Floor collision:
float distToGround = getDistanceToGround(z, (-vz * this.getDeltaMultiplier()) + 6);
if ((distToGround <= 1) && vz <= 0) {
z = z-distToGround;
}
Wall collision:
float vx = velocity.getX();
float vy = velocity.getY();
float rad = width;
float dis = 0;
if (CollisionHelper.collision_check(map, 0, 0, 0, collisionMask, x + vx, y + vy, z + 6)) {
for(float k=6; k<=16; k+=1) {
for(int i=0; i<360; i+=30){
if (CollisionHelper.collision_line_3d(x + vx, y + vy, z+k, (x + vx) + CollisionHelper.lengthdir_x(rad,i), (y + vy) + CollisionHelper.lengthdir_y(rad,i), z+k, map)>0) {
for(float j=rad; j>=0; j-=.1){
if (CollisionHelper.collision_line_3d(x + vx, y + vy,z+k,(x + vx) + CollisionHelper.lengthdir_x(j,i), (y + vy) + CollisionHelper.lengthdir_y(j,i), z+k, map)==0) {
dis = j;
break;
}
}
x=x+CollisionHelper.lengthdir_x(rad-dis,i+180);
y=y+CollisionHelper.lengthdir_y(rad-dis,i+180);
}
}
}
}
There’s nothing wrong with my collisions, however I fear as though what I use to detect walls is not efficient at all. The way I do it, is I cast rays out every 30 degrees, and if a triangle is found, it casts smaller and smaller rays until it finds free space (moving the player to this free space). Sure this method works, but it is quite costly to the CPU. Also, with my method, it doesn’t “change” the players velocity, if just shifts their location; visually this is fine, but technically, if you are moving fast enough you will push through the wall.
Does anyone have any good collision methods they want to share?