Collision Detection

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Collision Detection as PDF for free.

More details

  • Words: 1,084
  • Pages: 14
Gamasutra - Features - "Pool Hall Lessons" [01.18.02]

,VQ WLWZRQGHUIXOWRPRYHWKURXJKWKHYLUWXDOZRUOGLQD'JDPH"7KHZD\\RXFDQZDONLQWR ZDOOVDQGVOLGHDORQJWKHPDV\RXIUHHO\WXUQZLWKRXJJHWWLQJ\RXUUDLOJXQVWXFNEHWZHHQ \RXDQGWKHZDOO"7KHZD\\RXDQG\RXURSSRQHQWFDQUXQDWHDFKRWKHUVZRUGVGUDZQ DFWXDOO\VWRSZKHQ\RXUHDFKHDFKRWKHUDQGFDQWKHQEDFNDZD\ZLWKRXWZRUU\LQJDERXW \RXUVKLHOGJHWWLQJFDXJKWLQKLVFKDLQPDLO"7KHZD\\RXFDQFLUFOHVWUDIH\RXURSSRQHQWRYHU XQHYHQJURXQGZLWKRXWHYHUKDYLQJWRZRUU\DERXWWULSSLQJ",QWKHUHDOZRUOG\RXZRXOGKDYH WRZRUU\DERXWWKLVNLQGRIVWXII« $VGHYHORSHUVZHFDQ WDIIRUGWRGRFROOLVLRQGHWHFWLRQEHWZHHQHYHU\SRO\JRQRIDFKDUDFWHU DQGWKHLUHQYLURQPHQWRQHYHQWKHEHVWFXUUHQWKDUGZDUHLIZHGLGQRWRQO\ZRXOGRXU JDPHVEHSDLQIXOO\VORZEXWZH GDOVRUXQLQWRSUREOHPVOLNHWKRVHGHVFULEHGDERYH8VXDOO\D GHFHQWVSDWLDOSDUWLWLRQLQJV\VWHPDQGWKHXVHRIVSKHUHVWRERXQGFRPSOH[PRELOHREMHFWVOHW XVSHUIRUPFROOLVLRQGHWHFWLRQURXWLQHVWKDWDUHYHU\IDVWDQGRQO\LQYROYHWKHPLQLPXP QXPEHURISRO\JRQV1RWRQO\LVLWIDVWHUEXWLWKHOSVDYRL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QGWKHGLVWDQFH EHWZHHQWKHFHQWHUVRIWKHWZRFLUFOHVXVLQJWKHHTXDWLRQ

7KHQDGGWKHUDGLLRIWKHWZRFLUFOHVWRJHWKHU,IWKHVXPRIWKHUDGLLLVJUHDWHUWKDQRUHTXDOWR

'LVWWKHQWKH

FLUFOHVDUHWRXFKLQJ6LQFHPXOWLSOLFDWLRQVDUHOHVVFRPSXWDWLRQDOO\H[SHQVLYHWKDQVTXDUHURRWV\RXVKRXOG

VSHHGXSWKLVFRGHE\QRWSHUIRUPLQJWKHVTXDUHURRWZKHQFDOFXODWLQJWKHGLVWDQFHDQGLQVWHDGVTXDUHWKH VXPRIWKHUDGLL7KHFRGHEHORZVKRZVDVDPSOHLPSOHPHQWDWLRQXVLQJWKLVVKRUWFXW

double deltaXSquared = A.x - B.x; // calc. delta X deltaXSquared *= deltaXSquared; // square delta X

double deltaYSquared = A.y - B.y; // calc. delta Y deltaYSquared *= deltaYSquared; // square delta Y // Calculate the sum of the radii, then square it

Gamasutra - Features - "Pool Hall Lessons" [01.18.02]

double sumRadiiSquared = A.radius + B.radius; sumRadiiSquared *= sumRadiiSquared; if(deltaXSquared + deltaYSquared <= sumRadiiSquared){ // A and B are touching }
)RUWKLVSUREOHP\RXDUHJLYHQDFLUFOH$WKDWLVPRYLQJWKURXJKDYLUWXDOZRUOG2YHUDILQLWH SHULRGRIWLPHZKLFKZH OOFDOOW$PRYHVDFHUWDLQGLVWDQFHLQDFHUWDLQGLUHFWLRQ:H OO UHSUHVHQWWKLVPRYHPHQWE\DYHFWRU9$OVRLQWKLVYLUWXDOZRUOGLVDFLUFOH%WKDWLVQRW PRYLQJ7KHSUREOHPLVWRILJXUHRXWLI$FRPHVLQWRFRQWDFWZLWK%ZKLOHLWLVPRYLQJDORQJ9 DQGLILWGRHVE\ZKDWDPRXQWGRZHKDYHWRVKRUWHQ9VRWKDW$FRPHVWRUHVWMXVWDWWKH SRLQWRIFRQWDFWZLWK%"$QLOOXVWUDWLRQRIWKH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HVWHGWKHVKRUWHVWGLVWDQFHEHWZHHQ

EXWLWGLGQRWWDNHLQWRDFFRXQWGLUHFWLRQ"+HUH VZKHUHWKDWFDQFRPHEDFNDQGELWHXV &RQVLGHUWKHVLWXDWLRQLOOXVWUDWHGLQILJXUH%RWKDUURZVDUHWKHVDPHOHQJWKEXWLQVOLJKWO\ GLIIHUHQWGLUHFWLRQV7KLVVKRZVWKDW\HVWKHPRYHPHQWYHFWRULVORQJHQRXJKWREULQJWKHWZR FLUFOHVFORVHHQRXJKWRWRXFKEXWWKHGLUHFWLRQLVVXFKWKDWWKH\ZRQ W6RDWWKLVSRLQWLQWKH

9WKHQWKHUH

DOJRULWKPZHQHHGWRGRDUHDOLW\FKHFNLI'LVWDQFHLVJUHDWHUWKDQWKHOHQJWKRI LVQRFROOLVLRQ

)LJXUH

9DQGWKHQPXOWLSO\LWE\'VTUW 7 1RZ&LUFOH$ %7KH-DYDFRGHLPSOHPHQWLQJWKLVDOJRULWKPLVOLVWHG

,IWKHILQDOWHVWLVSDVVHGZHFDQQRUPDOL]H ZLOOPRYHXQWLOLWLVMXVWWRXFKLQJFLUFOH EHORZ

// Early Escape test: if the length of the movevec is less // than distance between the centers of these circles minus // their radii, there's no way they can hit. double dist = B.center.distance(A.center); double sumRadii = (B.radius + A.radius); dist -= sumRadii; if(movevec.Magnitude() < dist){ return false; } // Normalize the movevec Vector N = movevec.copy(); N.normalize(); // Find C, the vector from the center of the moving // circle A to the center of B Vector C = B.center.minus(A.center); // D = N . C = ||C|| * cos(angle between N and C) double D = N.dot(C);

Gamasutra - Features - "Pool Hall Lessons" [01.18.02]

// Another early escape: Make sure that A is moving // towards B! If the dot product between the movevec and // B.center - A.center is less that or equal to 0, // A isn't isn't moving towards B if(D <= 0){ return false; } // Find the length of the vector C double lengthC = C.Magnitude(); double F = (lengthC * lengthC) - (D * D); // Escape test: if the closest that A will get to B // is more than the sum of their radii, there's no // way they are going collide double sumRadiiSquared = sumRadii * sumRadii; if(F >= sumRadiiSquared){ return false; } // We now have F and sumRadii, two sides of a right triangle. // Use these to find the third side, sqrt(T) double T = sumRadiiSquared - F; // If there is no such right triangle with sides length of // sumRadii and sqrt(f), T will probably be less than 0. // Better to check now than perform a square root of a // negative number. if(T < 0){ return false; } // Therefore the distance the circle has to travel along // movevec is D - sqrt(T) double distance = D - sqrt(T); // Get the magnitude of the movement vector double mag = movevec.Magnitude(); // Finally, make sure that the distance A has to move // to touch B is not greater than the magnitude of the // movement vector. if(mag < distance){ return false; } // Set the length of the movevec so that the circles will just // touch movevec.normalize(); movevec.times(distance); return true; %DQN6KRW&ROOLVLRQEHWZHHQWZRPRYLQJFLUFOHV

7KLVSUREOHPVHHPVHYHQPRUHFRPSOLFDWHGWKDQWKHSUHYLRXVRQH*LYHQWZRPRYLQJFLUFOHV GHWHUPLQHZKHWKHURUQRWWKH\FROOLGH/RRNLQJDWWKHSUREOHPLQILJXUHZHFDQVHHWKDW MXVWEHFDXVHWKHLUSDWKVFURVVGRHVQRWPHDQWKDWWKHFLUFOHVZLOOFRPHLQWRFRQWDFW2QHPD\ KDYHPRYHGRXWRIWKHZD\LQWLPH,WDOVRVKRZVWKDWMXVWEHFDXVHWKHLUSDWKVGRQRWFURVV GRHVQRWPHDQWKDWWKH\GRQ WFROOLGH

Gamasutra - Features - "Pool Hall Lessons" [01.18.02]

)LJXUHWKH'\QDPLF'\QDPLFFROOLVLRQSUREOHP

7KDQNIXOO\WKHVROXWLRQWRDYHU\KDUGSUREOHPFRXOGQRWEHVLPSOHU:KDWZHDUHUHDOO\ LQWHUHVWHGLQLVQRWWKHLUPRYHPHQWYHFWRUVEXWWKHLUPRYHPHQWUHODWLYHWRHDFKRWKHU,IZH WUDQVODWHFLUFOH

$ VPRYHPHQWVXFKWKDW%FDQEHFRQVLGHUHGVWDWLRQDU\ZHFDQXVHWKH

'\QDPLF6WDWLFVROXWLRQGHVFULEHGDERYH

)LUVWRIDOOVXEWUDFWWKHPRYHPHQWYHFWRURIRQHFLUFOHI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DQHTXDWLRQZLWKYDULDEOHVWKDWDUHHLWKHUJLYHQRU FDQEHFDOFXODWHGIURPZKDWZDVJLYHQH[FHSWIRU36RLIZHVROYHIRU3ZHZLOOEHDEOHWR SOXJLQWKHNQRZQYDULDEOHVGHULYH3DQGWKHQXVH3WRFDOFXODWHWKHQHZPRYHPHQWYHFWRUV (TXDWLRQVKRZVHTXDWLRQDIWHUVROYLQJIRU3

6RSOXJJLQJWKLVLQWR(TXDWLRQV 

Gamasutra - Features - "Pool Hall Lessons" [01.18.02]

1RWLFHWKDWIRUERWKY DQGY WKHWHUPLQWKH EUDFNHWVLVWKHVDPHVR\RXRQO\QHHGWR FDOFXODWHWKDWRQFH:LWKWKDWGRQH\RXFDQFDOFXODWHY DQGY 1RZWKDWZH YHGRQHWKH PDWKIRUWKLVWKHFRGHWRLPSOHPHQWWKHUHVXOWVLVYHU\VKRUWDQGTXLFN7KHYDULDEOH RSWLPL]HG3LQWKHFRGHUHIHUVWRWKHWHUPLQEUDFNHWVDERYH

// First, find the normalized vector n from the center of // circle1 to the center of circle2 Vector n = circle1.center - circle2.center; n.normalize(); // Find the length of the component of each of the movement // vectors along n. // a1 = v1 . n // a2 = v2 . n float a1 = v1.dot(n); float a2 = v2.dot(n); // Using the optimized version, // optimizedP = 2(a1 - a2) // ----------// m1 + m2 float optimizedP = (2.0 * (a1 - a2)) / (circle1.mass + circle2.mass); // Calculate v1', the new movement vector of circle1 // v1' = v1 - optimizedP * m2 * n Vector v1' = v1 - optimizedP * circle2.mass * n; // Calculate v1', the new movement vector of circle1 // v2' = v2 + optimizedP * m1 * n Vector v2' = v2 + optimizedP * circle1.mass * n; circle1.setMovementVector(v1'); circle2.setMovementVector(v2'); %DOO&RUQHU3RFNHW

7KHVHWHFKQLTXHVZLOODOORZ\RXXVHVSKHUHVZLWKDKLJKHUGHJUHHRIDFFXUDF\WKDQLVSUREDEO\ QHFHVVDU\LI\RXUVSKHUHVDUHDOOERXQGLQJVSKHUHV3UHFLVHFROOLVLRQVEHWZHHQVSKHUHVEHFRPH LPSRUWDQWZKHQVLPXODWLQJWKLQJVOLNHVSRROEDOOVRUPDUEOHVRUSRWHQWLDOO\URFNVLQD ODQGVOLGH%XWIRUVSKHUHVERXQGLQJFKDUDFWHUVIRUH[DPSOH\RXPLJKWQRWFDUHZKDWDQJOH WZRFROOLGLQJFKDUDFWHUVZRXOGERXQFHDZD\DW+RZHYHUSDUWVRIWKHVHPHWKRGVDUHIDVW HQRXJKWREHXVHIXOLIRQO\WRGHWHUPLQHWKDWDFROOLVLRQZDVDYRLGHG%XWZKRNQRZVXVLQJD SXPSHGXSVSDFHPDULQHDVDFXHEDOOMXVWPLJKWEHKXPRURXVHQRXJKWRGR« 6SHFLDOWKDQNVWR'DYH%DXPIRUKLVKHOSZLWKFROOLVLRQUHVSRQVH

Gamasutra - Features - "Pool Hall Lessons" [01.18.02]

&RS\ULJKW‹&030HGLD,QF$OOULJKWVUHVHUYHG

Related Documents