i wrote collision detection system game working on, , experiencing weird glitch where, occasionally, projectiles go through player or walls scattered throughout level. because projectiles can fired @ angle, decomposed bounding box of each projectile multiple, smaller bounding boxes rotate around center of texture according rotation of projectile in space. reason, spear projectile go through player or wall through others not.
i use following methods determine rotation of texture , translate bounding boxes:
public double rotatetofacetarget() { double rotation = math.atan2((double)_direction.y, (double)_direction.x); return rotation; } public list<boundingbox> translateboundingbox(list<boundingbox> box, double rotation) { list<boundingbox> newbounds = new list<boundingbox>(); foreach (boundingbox b in box) { vector2 boundsorigin = new vector2(b.pos.x + b.size.x / 2, b.pos.y + b.size.y / 2); vector2 texorigin = new vector2(_pos.x + _texture.width / 2, _pos.y + _texture.height / 2); vector2 newposbasedonorigin = vector2.transform(boundsorigin - texorigin, matrix.createrotationz((float)rotation)) + boundsorigin; newbounds.add(new boundingbox(newposbasedonorigin, b.size)); } return newbounds; }
_direction calculated subtracting position of projectile target location , normalizing. use method determine if projectile colliding entity:
public bool projectilecollision(entity e, projectile entity2) { if (entity2.cancollide) { foreach(gameobject.boundingbox b in entity2.boundingbox) { foreach(gameobject.boundingbox b2 in e.boundingbox) { if (b2.intersect(b) && (entity2.ignoredentities.contains(e.type) == false)) { entity2.isactive = false; e.health -= entity2.damage; return true; } return false; } } return false; } return false; }
and bounding box intersection method:
public bool intersect(boundingbox intersected) { if ((_pos.y < intersected.pos.y + intersected.size.y) && (_pos.y + _size.y > intersected.pos.y) && (_pos.x + _size.x > intersected.pos.x) && (_pos.x < intersected.pos.x + intersected.size.x)) { return true; } return false; }
edit: on further testing, seems projectile detect hit if player hits based on top left corner ( makes sense @ intersect code). there way re-write intersect method use more accurate top left corner?
edit2: drew hitboxes objects, , 1 instance of when catch spear going through player:
the player larger pink square. hitboxes not being translated correctly, shouldn't stop working, , not others, right?
it happen, because of high velocity , small object, projectile fly through object. beside of checking if objects intersecting, have check if object will intersect, check if object in line of fire. achieve raycasting.
on cases had function check if object near other. simple checking if object inside of radius of other object. if yes checking if object flying toward other object, , checking distance between them. when distance close collision happened.
Comments
Post a Comment