[solved]getBounds()

I tried to search for the exact explanation of this method and how to get the (x, y) of the rectangle.

and I found out getX() and getY() as double.

But the one I want to get is the Y-axis of the hitted blocked when the BALL hitted it.

i tried to get the TOP and BOTTOM side of the BLOCK.

public boolean TOP() {
        boolean top = true;
        if (game.blocks.getTopY() == getTopY())
            top = true;
        return top;
    }
    
    public boolean BTM() {
        boolean btm = true;
        if (game.blocks.getLowY() == getLowY())
            btm = true;
        return btm;
    }

    public int getLowY() {
        int LowY = 0;
        if (hit())
            LowY = y + DIAMETER;
        return LowY;
    }

    public int getTopY() {
        int TopY = 0;
        if (hit())
            TopY = y;
        return TopY;
    }

If my information is not enough, please inform me so that I can add more.

Thanks.

These methods always return true, so… as for the ‘exact explanation’ of these methods… they are pretty much useless :slight_smile:

LOL. did not notice it.

Thanks.

Hello guys, I am trying to tell to the ball if what side of the block it hitted.

 public boolean TOP() {
        boolean top = false;
        if (hit() && y <= game.blocks.getTopY())
            top = true;
        return top;
    }
    
    public boolean BTM() {
       boolean btm = false;
       if (hit() && y <= game.blocks.getBtmY())
           btm = true;
       return btm;
    }

    public boolean LEFT() {
        boolean left = false;
        if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
                && x == game.blocks.getLeftX() - DIAMETER)
            left = true;
        return left;
    }

    public boolean RIGHT() {
        boolean right = false;
        if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
                && x == game.blocks.getRightX())
            right = true;
        return right;
    }

If there`s anything that I should do so that I can improve it, please tell me.
I am willing to learn more.

If there`s anything wrong with my code please tell me.

Thanks.

Minor, but


 public boolean TOP() {

        if (hit() && y <= game.blocks.getTopY())
            top = true;
        else
            top = false;
        return top;  
    }

is better, I think.

In this case,

I don`t need the BTM() anymore, right?

Making the code shorter, is better.

Thanks dude.

If you still need to check for bottom collision then keep it. I’m just saying you don’t need to initialize a boolean every time you call TOP() BTM() LEFT() or RIGHT().

This will allow you to get all collisions in one method call:


static final int TOP = 1, BOTTOM = 2, LEFT = 4, RIGHT = 8;

public int collisionResult() {
	int ret = 0;
	if (hit() && y <= game.blocks.getTopY()) ret |= TOP;
	if (hit() && y <= game.blocks.getBtmY()) ret |= BOTTOM;
	if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
			&& x == game.blocks.getLeftX() - DIAMETER) ret |= LEFT;
	if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
			&& x == game.blocks.getRightX()) ret |= RIGHT;
			
	return ret;
}

First time to see this

ret |= TOP;

What is the explanation of it?

Thanks.


static final int TOP = 1, BOTTOM = 2, LEFT = 4, RIGHT = 8;

public int collisionResult() {
	int ret = 0;
	if (hit() && y <= game.blocks.getTopY()) ret |= TOP;
	if (hit() && y <= game.blocks.getBtmY()) ret |= BOTTOM;
	if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
			&& x == game.blocks.getLeftX() - DIAMETER) ret |= LEFT;
	if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
			&& x == game.blocks.getRightX()) ret |= RIGHT;
			
	return ret;
}

It made my code shorter.
Thanks for the idea.

This is my new code:

  
        private static final TOP = 1, BTM = 2, LEFT = 3; RIGHT = 4;
        public int Side() {
        int side = 0;
        if (hit() && y <= game.blocks.getTopY())
            side = TOP;
        else if (hit() && y >= game.blocks.getBtmY() - DIAMETER)
            side = BTM;
        else if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
                && x == game.blocks.getLeftX() - DIAMETER)
            side = LEFT;
        else if (y < game.blocks.getBtmY() && y > game.blocks.getTopY()
                && x == game.blocks.getRightX())
            side = RIGHT;
        return side;
    }   

Movement of the ball

 public void moveBall() {
        if (x + xa < 0)
            xa = game.speed;
        else if (x + xa > game.getWidth() - DIAMETER)
            xa = -game.speed;
        else if (y + ya < 0)
            ya = game.speed;
        else if (y + ya > game.getHeight() - DIAMETER)
            game.gameOver();
        else if (collision()) {
            ya = -game.speed;
            y = game.racquet.getTopY() - DIAMETER;
        }
        if (Side() == TOP) {
            ya = -game.speed;
            y = game.blocks.getTopY() - DIAMETER;
            System.out.println("ytop: "+y);
        }
        else if (Side() == BTM) {
            ya = game.speed;
            y = game.blocks.getBtmY();
            System.out.println("ybtm: "+y);
        }
        else if (Side() == LEFT) {
            xa = -game.speed;
            x = game.blocks.getLeftX() - DIAMETER;
            System.out.println("yleft: "+y);
        }
        else if (Side() == RIGHT) {
            xa = game.speed;
            x = game.blocks.getRightX();
            System.out.println("yryt: "+y);
        }
        x = x + xa;
        y = y + ya;        
    }

*Dont mind the System.out.print().
I put it to monitor the (x, y).

Basically playing around with bits like legos.

Looking at the 4 least significant bits it becomes quite clear:

static final int TOP = 1, BOTTOM = 2, LEFT = 4, RIGHT = 8;

0 = 0....0000 // 0x0 (HEX)
1 = 0....0001 // 0x1
2 = 0....0010 // 0x2
4 = 0....0100 // 0x4
8 = 0....1000 // 0x8

Bitwise OR is like stacking the bits on top of each other and adding up all the 1’s.

Therefore

ret |= TOP;

is


 0....0000 // ret
+0....0001 // TOP
=0....0001

and if you add BOTTOM or LEFT or RIGHT you’ll just be adding that single 1 bit to a specific position.

OMG! It is too complicated. LOL

I can`t use this thing, I think.

Ok, can you explain the difference of

ret = TOP to ret |= TOP?

Thanks.

the diffrence is this:

when ret = 2:
ret = TOP: ret = 1
ret |= TOP: ret = 3

when ret = 1:
ret = TOP: ret = 1
ret |= TOP: ret = 1

Just think in bits, an int is in people language 1, 2, 3, 4, 5, 6, 7 etc.
A computer can only be on or off, so an 2 does not exist in memory.
It counts like 0001, 0010, 0011, 0100, 0101, 0110, 0111, etc

With bit shifting you just paste these numbers on top of each other.

00 | 00 = 00
00 | 01 = 01
00 | 10 = 10
00 | 11 = 11
01 | 00 = 01
01 | 01 = 01
01 | 10 = 11
01 | 11 = 11
10 | 00 = 10
10 | 01 = 11
10 | 10 = 10
10 | 11 = 11
11 | 00 = 11
11 | 01 = 11
11 | 10 = 11
11 | 11 = 11

Oh, I see.

That is why the value of sides is 1, 2, 4, 8 respectively.

Thanks for the explanation sir.

To add on to what everyone else has said, the bitwise OR operation allows you to create bit flags. Since each bit has it’s own position, and a boolean value is simply a single bit, you can think of it as creating several booleans within a byte.

One byte can contain eight boolean values:
0000 0001 = 1
0000 0010 = 2
0000 0100 = 4
0000 1000 = 8
0001 0000 = 16
0010 0000 = 32
0100 0000 = 64
1000 0000 = 128

The bitwise OR operator has the following truth table:
0 | 0 = 0
0 | 1 = 1
1 | 1 = 1

This means that:…
0000 0000 | 0000 0000 = 0000 0000
0000 0001 | 0000 0000 = 0000 0001
0000 0100 | 0000 0010 = 0000 0110
0000 0110 | 0000 0100 = 0000 0110

So, to sum it all up, bit flags allow you to place several booleans within a single byte. You can read the binary value of the byte, with a 0 meaning false and a 1 meaning true.
I really recommend this article.

Edit: It might be easier to read if you specify the values as binary. You can do this like so:


byte a = 0b00000000; // 0
byte b = 0b00000001; // 1
byte c = 0b00000010; // 2
byte d = 0b00000100; // 4
byte e = 0b00001000; // 8

byte x = a | c | d; // 0110, 6

Thank you for the informations guys.

[quote="Troubleshoots,post:15,topic:46549"]
-snip-

[/quote]
You made a pretty fatal mistake there.
A byte has 8 bits, or ‘booleans’ in this matter, not just 4!

Have a nice day.

  • Longor1996

Oh wow, I can’t have been thinking consciously. I edited the post. :expressionless: