Solved Slope Help

Discussion in 'Plugin Development' started by messageofdeath, Jul 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    messageofdeath

    How would I be able to get the blocks within two points like this.
    The Green squares being the points and the red ones are the ones that the method gets.
    I have no idea where to start.

    Also this will be on a flat surface like the ground

    [​IMG]
     
  2. Offline

    DannyDog

    messageofdeath
    We'll what you can do is start with a method that gets the two point's location.
    Then you'll need some good maths wiz to get those red blocks.
     
    JBoss925 likes this.
  3. Offline

    messageofdeath

    DannyDog
    I already have the two locations in my saved class, but I just need to get the red blocks.
     
  4. Offline

    Dragonphase

    messageofdeath
    I think the process you want to achieve is known as Pathfinding. There's a pretty decent resource here, which allows you to find a path from a start location and a target location.
     
  5. Offline

    TheHandfish

    Get the vector between two locations, then try using a block iterator.
     
  6. Offline

    JBoss925

    This is very easy.
    delta z
    ----------
    delta x
    In order to do this, store 3 vars, the basex, basey, and basez.

    Then find the delta z divide it by whatever the x is and multiply it by the ratio.

    Example:

    basex/basey/basez are the lower values.

    2 points:
    point 1: 10, 60, 10
    point 2: 20, 60, 15

    basex = 10
    basey = 60
    basez = 10

    change in z over change in x is:
    5/10 = 1/2

    Now iterate through the x's going up by 1 each time and get the relative x and then the z from the relative x. An example is provided below:

    Now, get the point at a certain x, let's say it's x = 15.

    We do x - basex(which is 10) = 5 let's call this value newx

    Now we do 1/2 * newx(newx is 5) which is 2.5 + basez(which is 10 also) = 12.5 lets call this value newz

    world.getBlockAt(newx, basey, newz) and it returns the block when x = 15;

    code example:
    Code:java
    1. loc1 = your location;
    2. loc2 = your other location;
    3.  
    4. double basex = Math.min(loc1.getX(), loc2.getX());
    5. double basey = Math.min(loc1.getY(), loc2.getY());
    6. double basez = Math.min(loc1.getZ(), loc2.getZ());
    7. double overx = Math.max(loc1.getX(), loc2.getX());
    8. double overy = Math.max(loc1.getY(), loc2.getY());
    9. double overz = Math.max(loc1.getZ(), loc2.getZ());
    10.  
    11. double changeinz = overz - basez;
    12. double changeinx = overx - basex;
    13.  
    14. ArrayList<Block> blocks = new ArrayList<Block>();
    15.  
    16. for(int x = basex; x <= overx; x++){
    17. double currx = x - basex;
    18. double slope = changeinz/changeinx;
    19. double z = slope * currx + basez;
    20. double newx = currx + basex;
    21. blocks.add(loc1.getWorld().getBlockAt(newx, basey, z));
    22. }
     
    Giraffeknee likes this.
  7. Offline

    messageofdeath

    I will try the PathFinder solution first.
     
  8. Offline

    JBoss925

    Added a code example.
    EDIT: Ok it's all good.
     
  9. Offline

    messageofdeath

    I'll try this because of effort :D Thank you very much, I will try it now
     
    JBoss925 likes this.
  10. Offline

    JBoss925

    Ok cool, just make sure you use doubles for better calculation.:D
     
  11. Offline

    Dragonphase

    JBoss925
    Show Spoiler


    How does this compensate for changes in the Y axe though?
     
  12. Offline

    JBoss925

    Lol he said it was gonna be flat, no changes in y. Plus changes in y is as simple as another for loop.
    Show Spoiler
     
  13. Offline

    messageofdeath

    It seems I will have to put more calculations into it due to it not calculating properly, but it is so close. Thank you.
     
    JBoss925 likes this.
  14. Offline

    Dragonphase

    JBoss925
    Fair do; I like the concept of little effort where required.
     
  15. Offline

    JBoss925

    Why are you arguing with me about this. I did what he asked, sorry I didn't want to confuse him with more intense mathematics. In a 3D plane you must calculate z too and it get's messy.
     
  16. Offline

    messageofdeath

    JBoss925 Dragonphase
    It's really close, this is all thats wrong with it. I'll fix the rest.

    DiamondBlock = Your method
    GoldBlock = Pathfinder

    [​IMG]
     
  17. Offline

    JBoss925

    Hmmmm, what results are you getting? I think I know why this is. I can very quickly redo the correct code.
     
  18. Offline

    messageofdeath

    JBoss925
    At the top left hand corner. The diamond blocks are in the wrong place. It goes through the gold blocks which is the correct path. The left and right of the track don't show anything. Probably due to dividing by 0
     
  19. Offline

    JBoss925

    Code:java
    1. loc1 = your location;
    2. loc2 = your other location;
    3.  
    4. double basex = loc1.getX()
    5. double basey = loc1.getY()
    6. double basez = loc1.getZ()
    7. double overx = loc2.getX();
    8. double overy = loc2.getY();
    9. double overz = loc2.getZ();
    10.  
    11. double changeinz = overz - basez;
    12. double changeinx = overx - basex;
    13.  
    14. ArrayList<Block> blocks = new ArrayList<Block>();
    15.  
    16. if(basez<= overx){
    17. for(int x = basex; x <= overx; x++){
    18. double currx = x - basex;
    19. double slope = changeinz/changeinx;
    20. double z = slope * currx + basez;
    21. double newx = currx + basex;
    22. blocks.add(loc1.getWorld().getBlockAt(newx, basey, z));
    23. }
    24. }
    25.  
    26. if(basez > overx){
    27. for(int x = basex; x >= overx; x--){
    28. double currx = x - basex;
    29. double slope = changeinz/changeinx;
    30. double z = slope * currx + basez;
    31. double newx = currx + basex;
    32. blocks.add(loc1.getWorld().getBlockAt(newx, basey, z));
    33. }
    34. }
    35.  
    36. }


    I think I fixed it. Probably cuz of my auto assigning I picked up from my cuboidsapi. Should've fixed it above.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  20. Offline

    messageofdeath

    Spins into infinite loop, looking at loops now.

    Second if statement, thats where the infinite loop happens

    JBoss925
    Changed second for loop to " for(double x = overx; x >= basex; x--) {"

    Same result as before

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  21. Offline

    JBoss925

    Fixed it.
     
  22. Offline

    messageofdeath

    JBoss925
    for(double x = basex; x >= overx; x--) {

    Doesn't work because it will never go into the loop.

    because basex will always be lower than overx which in turn doesn't do anything.

    Add me on skype messageofdeath
     
  23. Offline

    Dragonphase

    JBoss925

    I wasn't arguing, I was just wondering. When I said little effort where required, I meant I liked how you recognized how he only needed a solution for a flat surface, and you didn't give him more than what he asked for.
     
  24. Offline

    JBoss925

    Yes it will? basex is greater than overx in that instance. Alright I'll add you.
     
Thread Status:
Not open for further replies.

Share This Page