AceZephyr
AceZephyr

Reputation: 31

RayCast algorithm in Bukkit

I've been wondering if it would be possible in a Bukkit plugin to create a RayCast system that I could use to, off a Vector, create a ray that I could, for instance, summon particles every so many blocks along it. I don't have much experience with math; I am only in eighth grade geometry and haven't gotten to trig yet. I didn't want to use player.getTargetBlock() and player.getLineOfSight() because they are deprecated and getLineOfSight() only gives me a List of Blocks, and I was looking for something that i could get a List of Locations or something like that and to be able to change the distance interval between each Location in the list.

Thanks. I hope I can get a solution for this. I know people have done it before, but when I search up RayCast Algorithms, it mainly gives me ways to write a Doom-like game engine.

Upvotes: 0

Views: 3074

Answers (2)

Jojodmo
Jojodmo

Reputation: 23616

To iterate threw blocks, you can use a BlockIterator:

LivingEntity from;//set this to the living entity (or player) that you would like to send the particles from
int distance;//set this to the distance, in blocks, that you would like the particles to go

BlockIterator blocksToAdd = new BlockIterator(from.getEyeLocation(), 1, distance);
while(blocksToAdd.hasNext()){
  Location loc = blocksToAdd.next().getLocation();
  if(!loc.getBlock().getType().isSolid()){
    //stop playing particles if the next block is not solid
    break;
  }
  else{
    //play the particle here using the location loc
  }
}

Upvotes: 0

AceZephyr
AceZephyr

Reputation: 31

Looks like I have found a solution, here it is if you want to know it. It is a test plugin, so, for testing, i added a /vec command to check your vector and the /shoot command shoots a particle. The args are /shoot [length of ray] [space between particles in blocks]. The length cannot be a decimal (it probably could with some code changes) but the space between the particles can. To stop the particle at a solid block, I would just break the loop when lastParticle.getBlock().getType().isSolid() is false.

http://pastebin.com/CvE5vG8U

P.S. I used darkbladee12's ParticleEffect library for this, I could just use the Effect class, but that only has a couple of effects.

Upvotes: 0

Related Questions