Realistic tree falling

Discussion in 'Plugin Development' started by Tecno_Wizard, Aug 27, 2014.

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

    Tecno_Wizard

    Hey guys,
    I am making a plugin that makes trees fall when cut down and lay to the side of the stump. The problem is, I want this to be realistic, and making trees fall realistically when you are dealing with slopes made of blocks is HARD. Per say, a tree falls towards a cliffside. Can't really make a diagonal tree in MC...
    I also really don't want a tree with one block at the cliff edge, and the rest underground, or worse, 50 blocks up. Does anyone have any ideas on how I could get the trees to fall with realism in these situations?
    Thanks!
     
  2. Offline

    NathanWolf

    One thing you could try that might look really cool...
    1. Record directional vector between player and stump, for the direction the tree will "fall".
    2. Force a downward angle to the above vector - e.g. direction.setY(-1).normalize()
    3. Replace all of the remaining log and leaf blocks in this "tree" (note below) with air
    4. Spawn FallingBlock entities of the appropriate type at the location of each block you removed
    5. Apply the directional vector above as velocity to the falling blocks, so they fall in a specific direction.
    6. You may need to tweak the speed of the vector so they don't "shoot" towards the ground.
    Basically, let MC physics handle the problem of the thing falling down.
    The other part of this that will be difficult (and is a common problem) is how you detect a "tree". You could do this recursively, but you will have issues dealing with jungle trees and roofed forests. You'd need a large recursion depth for jungle trees, but that will cause half of your covered forest to fall down in one axe chop.
    You may need a combination of area searching, max distance (with separate horizontal/vertical thresholds) and maybe some recursion.
    Now that I've spelled all this out, I want to go modify my "chop" spell to do this (it currently just breaks all the tree blocks and drops items... how dull!) :D
     
  3. Offline

    Tecno_Wizard

    NathanWolf, I was planning on leaving out jungle trees for a later version if I could get the first working. The acadia ones might also be a problem, because you can't really detect a tree well when it's curved. (I'm using a system where it checks for 2 or more wood blocks of the same type, then at some point a kind of leaf)

    Also, I've never worked with animation vectors before. Know of a good tutorial?
     
  4. Offline

    NathanWolf

    I don't know of a good tutorial- but it's easier than it sounds!

    You could start by skipping the vector part and see how it looks- just spawn a FallingBlock entity at each tree block, when you fill that block with air. Here's some code from my plugin where I do something similar:

    https://github.com/elBukkit/MagicPl...e/bukkit/block/batch/ConstructBatch.java#L502

    You spawn the FallingBlock with a given Material at a Location. Unless you apply a velocity to it, it will just fall straight down. This will be a great place to start if you can get that working!

    From there, you can also refer to my example code for setting a velocity- it's pretty easy to get a vector that points from one direction to another, just do something like

    Code:
    // Get the direction between two locations by subtracting them as vectors
    Vector direction = playerLocation.toVector().subtract(targetLocation.toVector()).normalize();
    
    Subtracting one vector (which can be a location) from another gives you the direction from the one to the other. The "normalize" call makes the result a "unit vector" (length=1), while maintaining the direction. This is important because when you apply a velocity to an entity, you're applying direction as well as speed (the length of the vector is the speed).

    A speed of 1 is a good place to start, you may find it's too fast for "falling" - you can start tweaking from there.

    Code:
    // Multiply a unit vector to set the speed
    float speed = 0.2f;
    Vector direction = direction.multiply(speed);
    I hope that helps! This could be a really cool effect if you can get it working well. I think FallingBlocks are the way to go here, since block animation would be really jumpy, even if you could do it well.
     
    TheMintyMate likes this.
  5. Offline

    TheMintyMate

    Tecno_Wizard
    If you want to see how other Developers have done it, you could look at sethblings "blingtrees" plugin.
    In his plugin he has his trees "falling" in a negative X fashion. You could change this as NathanWolf
    suggested and set the velocity of the falling block entity's away from the player, to make it more
    realistic.

    Hope this helped,
    - Minty

    Edit: You could open Sethbling's plugin in http://jd.benow.ca/ to decompile the jar and view how he went about coding it.
     
  6. Offline

    Tecno_Wizard

    TheMintyMate, I don't like decompiling other people's work. It's just wrong. The problem is, the blocks on his are completely independent, and the tree will literally split into 2 on a cliff.
     
  7. Offline

    TheMintyMate

    Yeah, the ethics behind decompiling other people/persons work can be debatable. I have only suggested decompiling his work, as anyone can freely use his plugin. If you did copy any of his code, then you may want to give credit to him when distributing your plugin. If the file/s are closed source, it is more likely to be illegal than not to decompile said file/s (depends on whether the Software license directly forbids reverse engineering of their software). However that said, Wikipedia has stated that "In the United States and many other countries, even if an artifact or process is protected by trade secrets, reverse-engineering the artifact or process is often lawful as long as it is obtained legitimately."
    As Sethblings plugin has no license, and given out freely, one can only assume that the code is for public domain. Also, Sethblings website (where he distributes his plugins) confers to no licensing upon his plugins.
    Sources:
    http://www.coderanch.com/t/503905/java/java/Decompiling-Java-Archieves-JAR-WAR
    http://programmers.stackexchange.com/questions/26548/what-is-the-default-software-license
    http://sethbling.com/downloads/bukkit-plugins/
    ---------------------------------------------------------------------------------------------------------------

    -Minty
     
  8. Offline

    Tecno_Wizard

    TheMintyMate, I don't think I will, but at least I can get an idea of how I want it to look
     
  9. Offline

    _Filip

    There's nothing wrong with decompiling code, as long as you don't copy/paste. I decompile plugins all the time if I'm curious to see how they work and get ideas on how to make future plugins based on my newly acquired knowledge.
    Bukkit is designed to be a place for free plugins that are for the sole purpose of helping the community, uploading your files for free use, you should expect others to decompile and read your code, as that is what the uploader should have had in mind when developing their plugin.
     
Thread Status:
Not open for further replies.

Share This Page