मैं थोड़ी देर के लिए इस कृत्रिम बुद्धि विधि पर काम कर रहा हूं। यह मूल रूप से int
है, प्रत्येक दिशा के लिए दुश्मन जा सकता है अगर दीवार खिलाड़ी के लिए अपना रास्ता अवरुद्ध कर रही हो। यह ज्यादातर मामलों में काम नहीं करता है। कभी-कभी दुश्मन दरारों से गुजरता है जो इसे फिट नहीं कर सकता है। अन्य बार यह दीवारों पर फंस जाएगा जो उनमें स्पष्ट अंतराल है। मैं अपना कोड संलग्न करूंगा, लेकिन अगर यह बहुत अक्षम दिखता है या इसे हल करने का तरीका नहीं है तो मैं पूरी तरह से अपना दृष्टिकोण बदलने का विरोध नहीं कर रहा हूं। मैं सिर्फ यह जानना चाहता हूं कि इन चीजों को सामान्य रूप से कैसे किया जाता है, ताकि मैं इसे बेहतर (और कामकाजी!) तरीके से कार्यान्वित कर सकूं।गेम प्रोग्रामिंग एआई: एक खिलाड़ी को खोजने के लिए दीवारों को स्केल करना?
मेरे कोड:
public void update(ArrayList<Wall> walls, Player p){
findPlayer(p.getX(), p.getY());
boolean isCollision = false;
System.out.println(isCollision);
//if movement straight towards the player is blocked, move along the walls
for(Wall w : walls){
if(Helper.isBoundingBoxCollision((int)(x + vectorToPlayer.getDX() * SPEED), (int)(y + vectorToPlayer.getDY() * SPEED), width, height, w.getX(), w.getY(), w.width, w.height)){
isCollision = true;
if(Math.abs(vectorToPlayer.getDX()) > Math.abs(vectorToPlayer.getDY())){
if(vectorToPlayer.getDX() > 0)
WALL_COLLISION = 3;
else
WALL_COLLISION = 1;
}
else if(Math.abs(vectorToPlayer.getDX()) < Math.abs(vectorToPlayer.getDY())){
if(vectorToPlayer.getDY() > 0)
WALL_COLLISION = 0;
else
WALL_COLLISION = 2;
}
}
}
//System.out.println(isCollision);
//set the direction to the straight on vector, to be reset if there is a collision on this path
direction = vectorToPlayer;
if(isCollision){
//reset the variable, don't mind that what this is named is completely opposite = PIMPIN'
isCollision = false;
//scale dem walls son, and see when the path is clear
for(Wall w : walls){
if(WALL_COLLISION == 0 && !Helper.isBoundingBoxCollision(x + SPEED, y, width, height, w.getX(), w.getY(), w.width, w.height)){
WALL_COLLISION = 3;
isCollision = true;
}
else if(WALL_COLLISION == 1 && !Helper.isBoundingBoxCollision(x, y + SPEED, width, height, w.getX(), w.getY(), w.width, w.height)){
WALL_COLLISION--;
isCollision = true;
}
else if(WALL_COLLISION == 2 && !Helper.isBoundingBoxCollision(x - SPEED, y, width, height, w.getX(), w.getY(), w.width, w.height)){
WALL_COLLISION--;
isCollision = true;
}
else if(WALL_COLLISION == 3 && !Helper.isBoundingBoxCollision(x, y - SPEED, width, height, w.getX(), w.getY(), w.width, w.height)){
WALL_COLLISION--;
isCollision = true;
}
}
//if there is NOT a wall on the designated side, set the vector accoridingly
if(isCollision){
if(WALL_COLLISION == 0)
direction = new NVector(0, 1);
else if(WALL_COLLISION == 1)
direction = new NVector(1, 0);
else if(WALL_COLLISION == 2)
direction = new NVector(0, -1);
else if(WALL_COLLISION == 3)
direction = new NVector(-1, 0);
}
}
x += Math.round(direction.getDX()*SPEED);
y += Math.round(direction.getDY()*SPEED);
}
जैसा कि एरिक बी कहते हैं, टकराव का पता लगाने के साथ स्टीयरिंग गंतव्य तक पहुंचने की गारंटी नहीं है। सुझाए गए पथदर्शी एल्गोरिदम पर जाने के साथ समस्या एक को कार्यान्वित करने पर कम होगी ("ए *" एल्गोरिदम कई स्थानों पर प्रलेखित है) लेकिन आपकी दुनिया को अलग करने में ऐसा एल्गोरिदम काम करेगा। आपके isBoundingBoxCollision() कॉल से मुझे संदेह है कि आप दीवारों को खोजने के लिए इंजन का लाभ उठाने का प्रयास कर रहे हैं। कई गेम एक आंदोलन जाल के साथ स्तर ज्यामिति को बढ़ाते हैं जो ज्यामिति को पार्स करने की जटिलता से बचने के लिए आंदोलन प्रणाली के लिए पूछताछ करना आसान है। – Godeke
ठीक है, स्पष्टीकरण – ceptno