I've got a common situation where you have two variables (xSpeed and ySpeed) which I want to independently set to zero when they fall below minSpeed, and exit when they're both zero.
What would be the most efficient method? Currently I have two methods (Method 2 being cleaner), but I was wondering if you guys knew a better way....
Method1:
bool isZeroX = Math.abs(xSpeed)< minSpeed;
bool isZeroY = Math.abs(ySpeed)< minSpeed;
if(isZeroX && isZeroY) return -1;
else if(isZeroX) xSpeed = 0;
else if(isZeroY) ySpeed = 0;
Method2:
if(Math.abs(xSpeed)< minSpeed) xSpeed = 0;
if(Math.abs(ySpeed)< minSpeed) ySpeed = 0;
if(ySpeed==0 && xSpeed==0) return -1;
I prefer your second example because it is the most readable. Prefer readability over efficiency unless you can prove that you should be optimising.
You can do
if(-minSpread < xSpeed && xSpeed < minSpeed) {
xSpeed = 0;
if(-minSpread < ySpeed && ySpeed < minSpeed) {
ySpeed = 0;
return -1;
}
} else if(-minSpread < ySpeed && ySpeed < minSpeed) {
ySpeed = 0;
}
Perhaps make an elegant boolean method to see if the object is moving/stopped:
boolean isStopped() {
if(Math.abs(xSpeed)< minSpeed) xSpeed = 0;
if(Math.abs(ySpeed)< minSpeed) ySpeed = 0;
return (ySpeed==0 && xSpeed==0);
}
Related
I'm coding Conway's Game of Life on Processing 3 and I want to store x and y so that the squares don't change right away, but I don't know how to store it to use later. Any help is appreciated!
void keyPressed() {
for (int x = 0; x < 30; x++) {
for (int y = 0; y < 30; y++) {
int numNeighbours = numNeighbours(x,y);
if (cells[x][y] == true) {
if (numNeighbours > 3 || numNeighbours <= 1) { //underpopulation or overpopulation
}
}
else if (cells[x][y] == false) {
if (numNeighbours == 3) {
}
}
}
}
}
Based on my understanding of your code (and the Game of Life), you don't need to store x and y. What you actually need to do is to store the (x, y) pairs where the cell changes state.
You could do this by creating pairs and adding them to lists.
But a another idea is to use a second array representing the next generation of the game and put all of the new values there; e.g.
for (int x = 0; x < 30; x++) {
for (int y = 0; y < 30; y++) {
int numNeighbours = numNeighbours(x,y);
if (cells[x][y] == true) {
if (numNeighbours > 3 || numNeighbours <= 1) {
cellsNext[x][y] = false;
} else {
cellsNext[x][y] = true;
}
}
else if (cells[x][y] == false) {
if (numNeighbours == 3) {
cellsNext[x][y] = true;
} else {
cellsNext[x][y] = false;
}
}
}
}
Note: that could be simplified / written better, but I have written it as above so that you can see clearly what I have done.
I am solving n-queen, but i have a problem for some reason the while loop keeps looping without iterating, tempx and tempy doesn't go up by i/j. and the result keeps outputting 0,0
public static boolean isSafe(Board board, int row, int col){
int tempx;
int tempy;
for(int i = -1; i <= 1; i ++){
for(int j = -1; j <= 1; j ++){
try {
tempx = row + i;
tempy = col + j;
while(tempx >= 0 && tempx < board.getRow() && tempy >= 0 && tempy < board.getRow()) {
if(board.getTile(tempx, tempy).isOccupied())
return false;
tempx += i;
tempy += j;
}
} catch(Exception e){}
}
}
return true;
}
EDIT:
Ok i figured it out and it seems to work fine, for anyone that wants to know here it is, please correct me if there is a better way of doing this
public static boolean isSafe(Board board, int row, int col){
int tempx;
int tempy;
for(int i = -1; i <= 1; i ++){
for(int j = -1; j <= 1; j ++){
try {
tempx = row + i;
tempy = col + j;
for(int k = 0; k < board.getRow(); k++){
if(tempx >= 0 && tempx < 8 && tempy >= 0 && tempy < 8) {
if(board.getTile(tempx, tempy).isOccupied() )
return false;
tempx += i;
tempy += j;
}
}
} catch(Exception e){}
}
}
return true;
}
Do you realize that the loop
while(tempx >= 0 || tempx < 8 || tempy >= 0 || tempy < 8) {
is infinite as for every number tempx and tempy it is satisfied?
You probably wanted
while(tempx >= 0 && tempx < 8 && tempy >= 0 && tempy < 8) {
So I have this piece of code
if (x >= gameView.getWidth()) { //if gone of the right side of screen
x = x - gameView.getWidth(); //Reset x
y = random.nextInt(gameView.getHeight());
xSpeed =
ySpeed =
}
but I need to get both xSpeed and ySpeed to choose between two values, either '10' or '-10' and only those two numbers, nothing in between.
Everywhere I've looked says use the random.nextInt but that also chooses from the numbers in between -10 and 10...
You may try using
xSpeed = (random.nextInt() % 2 == 0) ? 10 : -10;
ySpeed = (random.nextInt() % 2 == 0) ? 10 : -10;
Good luck
How about this:
if(random.nextBoolean()){
xSpeed = 10;
}
else xSpeed = -10;
What about this? Math.random() returns values between 0.0 (include) and 1.0 (exclude).
public class RandomTest {
public static void main(String[] args) {
int xSpeed = 0;
int ySpeed = 0;
if (Math.random() >= 0.5) {
xSpeed = -10;
} else {
xSpeed = 10;
}
if (Math.random() >= 0.5) {
ySpeed = -10;
} else {
ySpeed = 10;
}
}
}
Lets suppose your random.nextInt(gameView.getHeight()); distributes evenly between even and odd numbers, then you could write something like this:
y = random.nextInt(gameView.getHeight()) % 2 == 0 ? 10 : -10;
I am making a space shooting game for Android and I am experiencing some problems with the collision detection between enemy and player ship(// Detect enemy collision with player). I 'ld appreciate if anyone can help me with the best way to approach this. check out my detectCollisions() below:
**private void detectCollisions(){
// Detect enemy collision with player; to be revised
for (int z = 0; z < SSEngine.TOTAL_INTERCEPTORS + SSEngine.TOTAL_SCOUTS + SSEngine.TOTAL_WARSHIPS - 1; z++ ){
if(enemies[z].posX <= SSEngine.playerBankPosX
&& enemies[z].posY <= SSEngine.playerBankPosX )
{
player1.applyDamage();
}
}
//Detect Player's fire
for (int y = 0; y < 3; y ++)
if (playerFire[y].shotFired){
for (int x = 0; x < SSEngine.TOTAL_INTERCEPTORS + SSEngine.TOTAL_SCOUTS + SSEngine.TOTAL_WARSHIPS - 1; x++ ){
if(!enemies[x].isDestroyed && enemies[x].posY < 4.25 ){
if ((playerFire[y].posY >= enemies[x].posY - 1
&& playerFire[y].posY <= enemies[x].posY )
&& (playerFire[y].posX <= enemies[x].posX + 1
&& playerFire[y].posX >= enemies[x].posX - 1)){
int nextShot = 0;
enemies[x].applyDamage();
playerFire[y].shotFired = false;
if (y == 3){
nextShot = 0;
}else{
nextShot = y + 1;
}
if (playerFire[nextShot].shotFired == false){
playerFire[nextShot].shotFired = true;
playerFire[nextShot].posX = SSEngine.playerBankPosX;
playerFire[nextShot].posY = 1.25f;
}
}
}
}
}
}**
Your check in if is wrong-
if,
playerFire[y].posY >= enemies[x].posY - 1 is true
how come
playerFire[y].posY <= enemies[x].posYwill hold true?
And you are ANDING true && false, so it will always be false.
I am writing a bouncing ball class (I'm just starting to use Java) and it works but it looks like every time the ball hits an edge the containing box gets bigger
Code for the bounce trigger
int i = 0;
int j = 0;
int k = 0;
double x = 3;
double y = 3;
while(i < 200){
if(j == 0){
x += 2;
} else {
x -= 2;
}
if(k == 0){
y += 4;
} else {
y -= 4;
}
if(thisEye.getX() > 400){
j = 1;
}
if(thisEye.getX() < 0) {
j = 0;
}
if(thisEye.getY() > 200){
k = 1;
}
if(thisEye.getY() < 0) {
k = 0;
}
Color someShade = rGen.nextColor(); // var for the color of the eyes
Color someOtherShade = rGen.nextColor();// var for the color of the pupules
moveThis(thisEye,x,y,someShade); // Go over all the shapes with the same X,Y offset - var x,y
moveThis(thisPupul,x,y,someOtherShade);
moveThis(thisEye2,x,y,someShade);
moveThis(thisPupul2,x,y,someOtherShade);
moveThis(face,x,y,rGen.nextColor());
moveThis(nose,x,y,rGen.nextColor());
pause(150.0); // wait for next cycle to slow down the images
//i++; this was to see if it just got farther off with each loop, it dose.
}
}
private void moveThis(GOval subject, double x, double y, Color newShade){
subject.setFillColor(newShade);
subject.move(x, y);
}
the 'face' will bounce off the right spot once then each time it bounces it gets farther and farther off the screen tell is so far of the it only is on screen for a short time then off the other side tell it comes back on screen for a bit.
I marked it as home work but I'm a html/php coder in the day and am just using the iTunesU Stanford videos at night.
but I'm trying to learn so pointers would be great.
The problem could be (though very unlikely) with your theEye.getX() and theEye.getY() method. What if you change your boundary condition check from
if(thisEye.getX() > 400){
j = 1;
}
if(thisEye.getX() < 0) {
j = 0;
}
if(thisEye.getY() > 200){
k = 1;
}
if(thisEye.getY() < 0) {
k = 0;
}
TO
if(x > 400){
j = 1;
}
if(x < 0) {
j = 0;
}
if(y > 200){
k = 1;
}
if(y < 0) {
k = 0;
}
Otherwise, please post the complete source code so we can see where else could the problem be.