# Solved Calculate player y acceleration

Discussion in 'Plugin Development' started by xpaintall, Apr 3, 2022.

Not open for further replies.
1. Offline

### xpaintall

I am trying to apply the newton's second law of motion into minecraft (i am not that good at physics), so is there an already implemented way to calculate the player's y acceleration or am I going to have to do it manually (g ~ 32.656m/s²)? I dont have a lot of knowledge on physics, so please correct mistakes I make

#1
2. Offline

### CraftCreeper6

Player#getVelocity() returns a Vector, you can fetch the y value from it.

#2
3. Offline

### Dai_Kunai

Do different players have different masses, out of curiosity?

#3
4. Offline

### xpaintall

@CraftCreeper6 I managed to get something to work, but since acceleration is measured in m/s I'm getting relatively low values with:
Code:
```        if(event.getFrom().getY() > event.getTo().getY()) {
playerVel = player.getVelocity().getY();
player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + playerVel);
}```
is Player#getVelocity()#getY() measured in ticks?

in the example you can see that the player is speeding up, but once hitting the ground the value is relatively low (don't mind the last message in chat since it's when the player already hit the ground)

File size:
367.5 KB
Views:
3
#4
5. Offline

### CraftCreeper6

getY just returns the Y component of the velocity vector.
Also, velocity != acceleration.
To get the acceleration you can use a=Δv/t

Although you're bound to get even smaller values with this, what's wrong with having small values?

#5
6. Offline

### xpaintall

@CraftCreeper6 no nothing I just messed up, I'm tryna calculate the f = ma, so it might seem a bit unrealistic to some people, but if I try to measure the time, wouldn't the finishing result be in minecraft ticks?
Edit: nvm im dumb lol

#6
7. Offline

### CraftCreeper6

@xpaintall
I don't know if velocity is calculated per tick or per frame. My guess would be per tick for synchronization across clients.
Events fire every tick though so you're capped at that either way. We all know that there are 20 ticks to a second, so your time will be 0.05 seconds per velocity reading. So you'll end up with
Code:
`acceleration = Δv / 0.05`
Or:
Code:
```acceleration = ~0.1 / 0.05
= ~2ms^-2```

#7
8. Offline

### xpaintall

@CraftCreeper6 sorry for the misconception, in my language velocity is said a bit different, so yeah.
The code works just fine, no errors, no nothing, but the only problem that I have is that no matter the speed or time, once I hit the ground, the acceleration becomes this (view image) automatically

File size:
1.1 MB
Views:
4
#8
9. Offline

### CraftCreeper6

@xpaintall
Well in Minecraft you don't continue falling through the ground so is that not expected behaviour?

#9
10. Offline

### xpaintall

@CraftCreeper6 oh wait i'm stupid xD
Edit: but how would you get the infinith second in which the player hit the ground (last tick before they did)?

#10
11. Offline

### CraftCreeper6

@xpaintall
Keep track of their last velocity and if they land on the ground (you'll have to find a way to do this) then use the last velocity just before hitting the ground.

#11
12. Offline

### xpaintall

@CraftCreeper6 Thanks! Got it working, although it does glitch sometimes.
Code:
`if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR)`
i'm marking this as solved

#12