# Solved Projectile collision code

Discussion in 'Plugin Development' started by Anrza, Sep 2, 2015.

Not open for further replies.
1. Offline

### Anrza

I could use the projectile collision code used in MC. Preferably just a method something like
Code:
`    public void Double checkProjectilePath(Location startLocation, Vector direction, double reach, Float xSize, Float ySize, Float zSize);`
Where
startLocation is the location from which the projectile is fired
direction is the direction it will go
reach is how far it will check
nSize is the hitbox size of the projectile.
And the Double it returns is how far it can go without collision.

Does anyone have this or something of equal value?

Thanks in advance, help much appreciated.

#1
2. Offline

### teej107

@Anrza You can use a BlockIterator

#2
3. Offline

### Anrza

I can to get blocks, but not to check for collisions.

#3
4. Offline

### mine-care

@Anrza Please be a litle more descriptive on your objective, do you want to calculate where the projectile will colide?
with what it will colide?
or the distance from initial position and colision?

Thanks.

#4
5. Offline

### teej107

Collisions with other blocks? Just check to see if the block is air or not.

#5
mine-care likes this.
6. Offline

### Anrza

Well, it would collide with non-occluding blocks too, like vines, cobwebs, torches, buttons, et cetera. And a simple !isOccluding() check would still allow it to pass through slabs, doors and stairs.

I want to know the distance from initial position and collision.

#6
7. Offline

### mine-care

@Anrza You can use the Location#distance(Location) method
So record the initial position and the colision location and then use the distance method that returns a double to find the distance :- )

#7
8. Offline

### Anrza

Yes, but Location#distance(Location) is useless to me, since I only know the initial position and not the collision location.

#8
9. Offline

### mythbusterma

@Anrza

There really isn't anything like this because every type of entity in MC has different physics (i.e. gravity affects things at different rates, due to some excellent 10/10 programming from Mojang).

Projectile flight implies that there will be gravity involved.

If you aren't concerned with drop, you aren't looking for projectile velocity.

Otherwise, you're going to have to some very annoying math to figure out if something is going to collide, or just launch a test projectile that's invisible.

#9
10. Offline

### Anrza

I really said no word about projectile flight

Nay, I'm not concerned with drop, and I can confirm, I'm not looking for projectile velocity.

Yea, it's that annoying math I'd like to get. I do not consider launching a test projectile an option. Not "clean" enough for my taste.

I'd like the code they use for two reasons:
a) I don't have to do it myself
b) Theirs would be better than anything I could do

I'm just hoping someone else have felt the way I do now and is willing to share what they made or found

Thanks

#10
11. Offline

### teej107

Well maybe you can try to explain what you want again because now I'm not sure what you want. If you want to find out where a projectile will land/hit, use a BlockIterator.

#11
12. Offline

### mythbusterma

@Anrza

There is no code that the server uses, as all flight on the server is projectile. Yes, you did, look at the title of the thread.

#12
13. Offline

### Anrza

There MUST be some code that is used to check if a projectile collides with something.
And the title is "Projectile Collision Code". Not "Projectile Flight Code".

BlockIterator doesn't solve it. Let me explain why:

You have these blocks: [A][T][SL][D]
A = air, T = Torch, SL = Slab, D = Dirt

If I use a BlockIterator to get those, I'll have a List<Block>.

Code:
```public boolean passThroughBlocks(List<Block> blocks) {
for (Block block : blocks) {
if (block.getType() != Material.AIR) {
//Will allow it to pass through air blocks
if (block.getType().isOccluding() {
//Occluding blocks, such as dirt, stone, log, et cetera, go here
return false;
} else {
//Non-occluding blocks, such as torches and cobwebs but more importantly SLABS and STAIRS go here
//No good way to stop it from going through stairs
}
}
}
return true;
}```
It would go through the air, the torch and the SLAB, but not the dirt block.
If it should go through the slab should depend on the projectile's hitbox and location within the slab block.

So a BlockIterator is PERFECT if I want to know which blocks stand in the projectile's path, but insufficient if I want to know if it collides with a block here or not.

#13
14. Offline

### teej107

@Anrza You are going to have to write a solution to whether it'll collide yourself. Get the current block, let's say AIR, it won't collide. Take the next block, let's say TORCH, it won't collide. Next block could be a stone, well that will collide. Just keep a Set of Blocks that won't collide with the projectile and check against that.

I'm sure you can find another solution somewhere though. Maybe around where a ProjectileHitEvent gets fired.

#14
15. Offline

### mythbusterma

@Anrza

Again, the word "projectile" in and of itself implies under the influence of gravity.

And again, you're going to have to do this yourself, because nowhere in the server is there any entity that flies on a non-projectile path.

The way the server does collision is by adding to velocity as per acceleration every tick. It then takes the velocity and adds it to the position of the entity, then checks for collision against other objects. It does not determine the flight path ahead of time, as that is indeterminate.

#15
16. Offline

### Anrza

You don't even play dirty well.

projectile |prə(ʊ)ˈdʒɛktʌɪl, -tɪl|
noun
a missile designed to be fired from a gun.
• an object propelled through the air, especially one thrown as a weapon

Look up projectile next time, not projectile motion.

#16
17. Offline

### mythbusterma

@Anrza

According to Wikipedia: A projectile is any object thrown into space (empty or not) by the exertion of a force. Although any object in motion through space (for example a thrown baseball) may be called a projectile, the term more commonly refers to a ranged weapon.

The implication of which being that gravity affects it, since the space in the real world and on the server is affected by gravity. Furthermore, what you're talking about is not projectile at all, since it will not be flying over a period time.

What you mean is a "Ray."

Now if you'll stop arguing semantics, there isn't anything in the code that checks for what you're looking for, because, as I've stated before, under normal circumstances the solution is indeterminate.

Last edited: Sep 4, 2015
#17
18. Offline

### Xerox262

You want to get the distance where a projectile launched and hit, when do you need to use this? And why can you not just store the Projectile in a hashmap with the key as it's launched location then get the distance in a hit event?

#18
19. Offline

### Anrza

Well, it exists and I found it. Thanks for nothing at all.

#19
20. Offline

### mythbusterma

@Anrza

Do enlighten me, where is it?

#20
21. Offline

### Anrza

net.minecraft.server.Entity and net.minecraft.server.EntityProjectile

#21