xna - Cube entity collision on plane entity -
how do collision between cube , plane, can sphere on plane cant figure out cube on plane, know must find x,y,z of cube , detection on plane cant figure out.
here collision tester code.
public static bool sphereandsphere(sphere a, sphere b, ref contact contact) { // vector centre of particle b centre of particle vector3 separationvector = b.position - a.position; float sumofradii = a.radius + b.radius; float distance = separationvector.length(); if (distance < sumofradii) { contact.contactpoint = a.position + separationvector / 2f; separationvector.normalize(); contact.contactnormal = separationvector; contact.penetrationdepth = sumofradii - distance; return true; } return false; } // assumes origin in centre of world // , planes boundaries of world , rigid-bodies within boundaries public static bool sphereandplane(sphere a, planeentity b, ref contact contact) { // depth of sphere plane (if negative, no collision) float depth = vector3.dot(a.position, b.directionfromorigin) + a.radius + b.offsetfromorigin; if (depth > 0) { contact.contactpoint = a.position + (a.radius - depth) * b.directionfromorigin; contact.contactnormal = -b.directionfromorigin; contact.penetrationdepth = depth; return true; } return false; } here test on cube on box
public static bool sphereandbox(sphere a, cube b, ref contact contact) { vector3 relativepoint = vector3.transform(a.position, matrix.invert(b.worldtransform)); // out check, based on separation axis theorem if (math.abs(relativepoint.x) - a.radius > b.halfsize.x || math.abs(relativepoint.y) - a.radius > b.halfsize.y || math.abs(relativepoint.z) - a.radius > b.halfsize.z) return false; vector3 closestpoint = vector3.zero; closestpoint.x = mathhelper.clamp(relativepoint.x, -b.halfsize.x, b.halfsize.x); closestpoint.y = mathhelper.clamp(relativepoint.y, -b.halfsize.y, b.halfsize.z); closestpoint.z = mathhelper.clamp(relativepoint.z, -b.halfsize.z, b.halfsize.z); float distance = (closestpoint - relativepoint).lengthsquared(); if (distance < a.radius * a.radius) { contact.contactpoint = vector3.transform(closestpoint, b.worldtransform); contact.contactnormal = a.position - contact.contactpoint; contact.contactnormal.normalize(); contact.penetrationdepth = a.radius - (float)math.sqrt(distance); return true; } return false; }
if either plane or cube not axis aligned -or- cube (width,length,height) not equivalent, find easiest test each corner position of cube individually against plane instead of cube whole. when first corner crosses plane, register collision.
but if both plane , cube axis aligned and cube symmetrical (wlh), think of cube sphere radius half width of cube.
Comments
Post a Comment