मुझे एक आयत को एक से अधिक आयतों से टकराने में कठिनाई हो रही है।
मैं एसएफएमएल का उपयोग कर रहा हूं और इसका एक आसान कार्य है, जिसे intersects2 आयत लगते हैं और चौराहों को वापस करते हैं। मेरे पास आयतों से भरा एक वेक्टर है जिसे मैं चाहता हूं कि मेरी जंगम आयत टकरा जाए। मैं निम्नलिखित कोड का उपयोग करके इसके माध्यम से लूप कर रहा हूं (p गतिपूर्ण आयत है)।
IsCollidingWithintersectsचौराहों पर काम करने के लिए एसएफएमएल का उपयोग करने के लिए एक बूल लौटाता है ।
while(unsigned i = 0; i!= testRects.size(); i++){
if(p.IsCollidingWith(testRects[i]){
p.Collide(testRects[i]);
}
}
और वास्तविक Collide()कोड:
void gameObj::collide( gameObj collidingObject ){
printf("%f %f\n", this->colliderResult.width, this->colliderResult.height);
if (this->colliderResult.width < this->colliderResult.height) {
// collided on X
if (this->getCollider().left < collidingObject.getCollider().left ) {
this->move( -this->colliderResult.width , 0);
}else {
this->move( this->colliderResult.width, 0 );
}
}
if(this->colliderResult.width > this->colliderResult.height){
if (this->getCollider().top < collidingObject.getCollider().top ) {
this->move( 0, -this->colliderResult.height);
}else {
this->move( 0, this->colliderResult.height );
}
}
और IsCollidingWith()कोड है:
bool gameObj::isCollidingWith( gameObj testObject ){
if (this->getCollider().intersects( testObject.getCollider(), this->colliderResult )) {
return true;
}else {
return false;
}
यह ठीक है जब Rectदृश्य में केवल 1 है। हालाँकि, जब एक से अधिक बार Rectयह एक समस्या का कारण बनता है जब 2 टकरावों को एक बार में पूरा किया जाता है।
किसी भी विचार कैसे इस के साथ सही ढंग से निपटने के लिए? मैंने अपनी समस्या दिखाने के लिए youtube पर एक वीडियो अपलोड किया है। दूर-दाईं ओर कंसोल चौराहों की चौड़ाई और ऊंचाई को दर्शाता है। आप कंसोल पर देख सकते हैं कि यह एक साथ 2 टकरावों की गणना करने की कोशिश कर रहा है, मुझे लगता है कि यह वह जगह है जहां समस्या पैदा हो रही है।
अंत में, नीचे दी गई छवि मेरी समस्या को अच्छी तरह से समझाती है:

colliderवस्तुओं को this->getCollider()अपडेट करके लौटाया जाता है this->move()??