• Hello Guest! Did you know that ProjectKorra has an official Discord server? A lot of discussion about the official server, development process, and community discussion happens over there. Feel free to join now by clicking the link below.

    Join the Discord Server

How to create rebounding blast ?

StrangeOne101

Staff member
Plugin Developer
Moderator
Verified Member
Shouldn't you just be inverting the axis of the side that hit it? I don't understand what you've currently got, but if you invert all the axis then it will just go backwards.

Code:
Location prevLoc = location.clone();

location.add(direction);

if (!isTransparent(location.getBlock())) {
    if (location.getBlockY() != prevLoc.getBlockY()) direction.setY(-direction.getY());
    else if (location.getBlockX() != prevLoc.getBlockX()) direction.setX(-direction.getX());
    else direction.setZ(-direction.getZ());

    location = prevLoc; //So we actually haven't collided and can still move next tick. Can't do that if we're in the ground
    rebounds++;
}
I typed this up on my phone so sorry if there are any errors. But you probably want something like that.

Also, why do you have a HashMap containing all the people who are using it? CoreAbility does this for you, so you shouldn't need that at all ;)
 

SkepsonSk

Verified Member
Shouldn't you just be inverting the axis of the side that hit it? I don't understand what you've currently got, but if you invert all the axis then it will just go backwards.

Code:
Location prevLoc = location.clone();

location.add(direction);

if (!isTransparent(location.getBlock())) {
    if (location.getBlockY() != prevLoc.getBlockY()) direction.setY(-direction.getY());
    else if (location.getBlockX() != prevLoc.getBlockX()) direction.setX(-direction.getX());
    else direction.setZ(-direction.getZ());

    location = prevLoc; //So we actually haven't collided and can still move next tick. Can't do that if we're in the ground
    rebounds++;
}
I typed this up on my phone so sorry if there are any errors. But you probably want something like that.

Also, why do you have a HashMap containing all the people who are using it? CoreAbility does this for you, so you shouldn't need that at all ;)
Thanks for your answer, but this code doesn't work correctly - coordinates are the same seldom ;( I will try to round tchem a bit.
 

YourAverageNerd

Verified Member
Code:
    // Directions, I know I could've used BlockFace but this is simply personal preference
    private final int[][] sides = new int[][] {
            // up
            new int[] {
                    0,
                    1,
                    0 },
            // down
            new int[] {
                    0,
                    -1,
                    0 },
            // north
            new int[] {
                    1,
                    0,
                    0 },
            // south
            new int[] {
                    -1,
                    0,
                    0 },
            // east
            new int[] {
                    0,
                    0,
                    1 },
            // west
            new int[] {
                    0,
                    0,
                    -1 } };
    /**
      *  A method to move the location forward.
      */
    private void execute(Location loc, Vector dir) {
        Location to = loc.clone().add(dir); // get the target location
    
        if (to.getBlock().getType() == Material.AIR) { // should also check for non-solid blocks to pass through like torches
            loc.add(dir); // if nothing was hit, move the location forward and stop the method
            return;
        }
        // we hit a block, let's see on which block the surface is.
        for (int i = 0; i < sides.length; i++) { // for loop to check which side we hit
            int[] addition = sides;
            loc.add(addition[0], addition[1], addition[2]); // move location one block in the current iteration's direction
            if (loc.getBlock().getType() == Material.AIR) { // check if there is a solid block in the current iteration's direction
                loc.subtract(addition[0], addition[1], addition[2]); // if not, move the location back to it's original location and continue with the next iteration
                continue;
            }
            // if statements to invert vector axis depending on what surface was hit.
            if (i < 2) { if i is lower than 2, which means the side was either up or down
                dir.setY(-dir.getY()); // invert respective axis
                loc.subtract(addition[0], addition[1], addition[2]); // revert location back to original position
            } else if (i < 5) { // if i is greater or equal to 2 and lower than 5, which means the side was either north or south
                dir.setX(-dir.getX()); // invert respective axis
                loc.subtract(addition[0], addition[1], addition[2]); // revert location back to original position
            } else { // anything higher or equal to 5, which means the side was either east or west
                dir.setZ(-dir.getZ()); // invert respective axis
                loc.subtract(addition[0], addition[1], addition[2]); // revert location back to original position
            }
        }
        loc.add(dir); // move location forward
    }
I tried to be as descriptive as possible, sorry if anything isn't clear. :)
 

SkepsonSk

Verified Member
Code:
    // Directions, I know I could've used BlockFace but this is simply personal preference
    private final int[][] sides = new int[][] {
            // up
            new int[] {
                    0,
                    1,
                    0 },
            // down
            new int[] {
                    0,
                    -1,
                    0 },
            // north
            new int[] {
                    1,
                    0,
                    0 },
            // south
            new int[] {
                    -1,
                    0,
                    0 },
            // east
            new int[] {
                    0,
                    0,
                    1 },
            // west
            new int[] {
                    0,
                    0,
                    -1 } };
    /**
      *  A method to move the location forward.
      */
    private void execute(Location loc, Vector dir) {
        Location to = loc.clone().add(dir); // get the target location
 
        if (to.getBlock().getType() == Material.AIR) { // should also check for non-solid blocks to pass through like torches
            loc.add(dir); // if nothing was hit, move the location forward and stop the method
            return;
        }
        // we hit a block, let's see on which block the surface is.
        for (int i = 0; i < sides.length; i++) { // for loop to check which side we hit
            int[] addition = sides;
            loc.add(addition[0], addition[1], addition[2]); // move location one block in the current iteration's direction
            if (loc.getBlock().getType() == Material.AIR) { // check if there is a solid block in the current iteration's direction
                loc.subtract(addition[0], addition[1], addition[2]); // if not, move the location back to it's original location and continue with the next iteration
                continue;
            }
            // if statements to invert vector axis depending on what surface was hit.
            if (i < 2) { if i is lower than 2, which means the side was either up or down
                dir.setY(-dir.getY()); // invert respective axis
                loc.subtract(addition[0], addition[1], addition[2]); // revert location back to original position
            } else if (i < 5) { // if i is greater or equal to 2 and lower than 5, which means the side was either north or south
                dir.setX(-dir.getX()); // invert respective axis
                loc.subtract(addition[0], addition[1], addition[2]); // revert location back to original position
            } else { // anything higher or equal to 5, which means the side was either east or west
                dir.setZ(-dir.getZ()); // invert respective axis
                loc.subtract(addition[0], addition[1], addition[2]); // revert location back to original position
            }
        }
        loc.add(dir); // move location forward
    }
I tried to be as descriptive as possible, sorry if anything isn't clear. :)
Thanks for answer but there is one little problem - you can't parse multi-dimensional table as one-dimensional.

EDIT: I changed int[] addition = sides to int[][] addition = sides and I changed all addition[0], addition[1], addition[2] to addition[0][0], addition[1][1], addition[2][2]. X and Z axises work great but there is problem with Y axis, do you happen to know why ? :)
 
Last edited:

YourAverageNerd

Verified Member
Thanks for answer but there is one little problem - you can't parse multi-dimensional table as one-dimensional.

EDIT: I changed int[] addition = sides to int[][] addition = sides and I changed all addition[0], addition[1], addition[2] to addition[0][0], addition[1][1], addition[2][2]. X and Z axises work great but there is problem with Y axis, do you happen to know why ? :)
Oh I see, that's just a typo on my end. Change
Java:
int[] addition = sides;
to
Java:
int[] addition = sides[i];
And make sure to revert the other changes you made, because it'll mess it up.
 
Top