मैं टाइल्स का उपयोग कर जावा गेम बना रहा हूं। मुझे टकराव तत्व में परेशानी हो रही है। मैं मानचित्र पर प्रत्येक टाइल के लिए आयताकार परिभाषित कर रहा हूं, और खिलाड़ी के लिए एक और आयत। मुझे क्या परेशानी हो रही है यह जानकर कि खिलाड़ी आयत को कब मारता है, और उसके बाद प्लेयर को उस दिशा में बंद कर देता है जिससे खिलाड़ी आया था। मैंने पहले ही एक विधि बनाई है जो यह जांचता है कि चरित्र आयत के अंदर कितना है, इसलिए यह जान सकता है कि इसे कितना धक्का देना है, लेकिन मैं यह नहीं समझ सकता कि चरित्र किस पक्ष से आ रहा है। टिप्पणी rect1 चरित्र है और rect2 टाइलजावा टाइल गेम - टकराव का पता लगाने
public void collision(Rectangle rect1, Rectangle rect2) {
float xAdd;
float xAdd2;
float yAdd;
float yAdd2;
boolean hitRight = false;
boolean hitLeft = false;
boolean hitTop = false;
boolean hitBot = false;
Vector2f rect1Origin = new Vector2f(rect1.x, rect1.y);
Vector2f rect2Origin = new Vector2f(rect2.x, rect2.y);
Vector2f rect1Mid = new Vector2f((rect1.x + rect1.width)/2,(rect1.y + rect1.height)/2);
Vector2f rect2Mid = new Vector2f((rect2.x + rect2.width)/2,(rect2.y + rect2.height)/2);
Vector2f rect1A = new Vector2f(rect1Origin.x + rect1.width, rect1.y);
Vector2f rect1B = new Vector2f(rect1Origin.x, rect1Origin.y+ rect1.height);
Vector2f rect1C = new Vector2f(rect1Origin.x + rect1.width,rect1Origin.y + rect1.height);
Vector2f rect2A = new Vector2f(rect2Origin.x + rect2.width, rect2.y);
Vector2f rect2B = new Vector2f(rect2Origin.x, rect2Origin.y
+ rect2.height);
Vector2f rect2C = new Vector2f(rect2Origin.x + rect2.width,
rect2Origin.y + rect2.height);
xAdd = rect2C.x - rect1B.x;
xAdd2 = rect1C.x - rect2B.x;
yAdd = rect2A.y - rect1B.y;
yAdd2 = rect2C.y - rect1A.y;
if (rect1Mid.y < rect2Mid.y) {
if (rect1.intersects(rect2)) {
y_pos += yAdd;
}
}
if (rect1Mid.y > rect2Mid.y) {
if (rect1.intersects(rect2)) {
System.out.println(yAdd2);
y_pos += yAdd2;
}
}
if(rect1Mid.x > rect2Mid.x){
if(rect1.intersects(rect2)){
hitRight = true; x_pos += xAdd;
}
}
if(rect1Mid.x< rect2Mid.x){
if(rect1.intersects(rect2)) {
x_pos += -xAdd2;
}
}
}
किसी भी मदद की बहुत सराहना
धन्यवाद
यह समय अंतराल के आधार पर अजीब स्थिति का कारण बन जाएगा। – Matzi
@Matzi वास्तव में कुछ उपयोगकर्ता कमांड निष्पादित करने से पहले इसकी पुष्टि की बात नहीं है - अगर यह मान्य नहीं है, तो आप कस्टम हैंडलिंग कर सकते हैं। – jmruc
@ किरीलरेचेव मैं वास्तव में समझ नहीं पा रहा हूं कि आप – grimrader22