I am trying to complete an assignment that simulates the micromouse challenge, where I need to map walls and then find the best path to the goal. I have been building out my blocks to move and to detect collision with a wall and was having success until my screen froze. I had my code saved and so restarted my laptop and pasted my code back in but now one of my If statements doesn't execute and I can't see what is wrong. Each function (the in-built isColllisionDetected() and my function offWall()) works separately for the same move input. But together I now get nothing.
This is my first post here so not sure if you need more information thn the below, please let me know. Any guidance on what I am missing would be hugely appreciated. Key points:
edited to show full code
full code
// function for directional movement
function direction() {
if ((getHeading() >= 1.47) && (getHeading() <= 1.66)) {
return "South";
}
if ((getHeading() >= 4.62) && (getHeading() <= 4.80)) {
return "North";
}
if ((getHeading() >= 6.19) && (getHeading() <= 0.87)) {
return "East";
}
if ((getHeading() >= 3.05) && (getHeading() <= 3.23)) {
return "West";
}
}
// function for tile coordinates
function tileX() {
return (Math.floor(getX() / 64));
}
function tileY() {
return (Math.floor(getY() / 64));
}
// truncate decimal places to 4
const formatter = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 4,
maximumFractionDigits: 4,
});
// calculate diagonal distance
function magnitude(x, y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
// velocity calculation
velocity = magnitude(getVelocityX(), getVelocityY());
// create turn function with degrees conversion
function turn(angleDeg) {
angleDeg = angleDeg * (formatter.format(Math.PI / 180));
let delta = 0;
do {
delta = angleDeg - getHeading();
let v = Math.min(1 , Math.abs(delta));
if (Math.abs(delta) > 0.002) {
if (delta > 0) {
setLeftPower(v);
setRightPower(-v);
} else {
setLeftPower(-v);
setRightPower(v);
}
} else {
setLeftPower(0);
setRightPower(0);
}
} while (Math.abs(delta) > 0.002);
}
// move foreward function with distance in tiles
function forward(distance) {
clearCollision();
distance = distance * 64;
const startX = getX();
const startY = getY();
const leeway = 5;
let travelled = 0;
let delta = 0;
let velocity = magnitude(getVelocityX(), getVelocityY());
do {
travelled = magnitude(
getX() - startX, getY() - startY
);
delta = distance - travelled;
if (Math.abs(delta > leeway)) {
let v = Math.min(1 , Math.abs(delta) / 64);
if (delta > 0) {
setLeftPower(v);
setRightPower(v);
} else {
setLeftPower(-v);
setRightPower(-v);
}
} else {
setLeftPower(0);
setRightPower(0);
}
} while (
!isCollisionDetected() && (
Math.abs(delta) > 3 ||
Math.abs(velocity) > 3
)
);
setLeftPower(0);
setRightPower(0);
}
function offWall() {
const d = 5;
const startX = getX();
const startY = getY();
let travelled = 0;
do {
travelled = magnitude(
getX() - startX, getY() - startY
);
if (travelled < d) {
setLeftPower(-0.1);
setRightPower(-0.1);
}
} while (travelled < d);
setLeftPower(0);
setRightPower(0);
}
// wall locator functions for Y and X
function wallLocY() {
if (direction("South")) {
return (tileY() + 1);
}
if (direction("North")) {
return (tileY() - 1);
}
}
function wallLocX() {
if (direction("East")) {
return (tileX() + 1);
}
if (direction("West")) {
return (tileX() - 1);
}
}
This is all followd by my If statement:
if (isCollisionDetected()) {
println("wall found");
offWall();
setLeftPower(0);
setRightPower(0);
clearCollision();
}
Which is then followed by me calling a movement function, eg forward(9);
Just answering this formally, thanks to @Jaromanda X.
I needed to move the If statement to after calling my movement function so it can run at the right time in my code block. thanks for helping me, I was feeling crazy :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.