I am writing a program where small 'birds' move around and follow your cursor. The instances don't rotate or anything fancy and I'm having a very unusual problem.
If the instance is on the same X-level or Y-level it oscillates. I've come across this problem with boundaries and movement before so I tried to take a similar approach.
void move() {
if (xPos == mouseX) {
xPos += 0;
} else if (xPos < mouseX) {
xPos += speed;
} else {
xPos -= speed;
}
if (yPos == mouseY) {
yPos += 0;
} else if (yPos < mouseY) {
yPos += speed;
} else {
yPos -= speed;
}
xPos = (xPos > mouseX) ? xPos - speed : xPos + speed;
yPos = (yPos > mouseY) ? yPos - speed : yPos + speed;
}
The movement is only smooth when both versions run at the same time. I've tried rearranging the '>' and '<' signs to no avail.
Is there any part of this code I could remove to stop it repeating itself?
Thanks :)
The bird needs to move less when close to the axis. Try this:
if (abs(xPos - mouseX) < speed) {
xPos = mouseX
} else if (xPos < mouseX) {
xPos += speed;
} else {
xPos -= speed;
}