Eria8 Posted February 21, 2017 Share Posted February 21, 2017 Like the title says, I'm trying to knock back all living entities near the player. But I don't understand how to get the x and z vectors for the knockback. Can someone help with this? // Get all entities within a 5 block [horizontal] radius of the player AxisAlignedBB aoe = player.getEntityBoundingBox().expand(5, 2, 5); List<Entity> entities = world.getEntitiesWithinAABBExcludingEntity(player, aoe); for (Entity e : entities) { // Ignore any entity that isn't living or that is an NPC if (e instanceof EntityLiving && !(e instanceof INPC)) { // These I don't understand how to get: int x = ???; // int z = ???; // ((EntityLiving) e).knockBack(e, 4, x, z); } } The knockback() description for reference if needed: void net.minecraft.entity.EntityLivingBase.knockBack(Entity entityIn, float strenght, double xRatio, double zRatio) Constructs a knockback vector from the given direction ratio and magnitude and adds it to the entity's velocity. If it is on the ground (i.e. this.onGround), the Y-velocity is increased as well, clamping it to .4. The entity's existing horizontal velocity is halved, and if the entity is on the ground the Y-velocity is too. Parameters: strenght Magnitude of the knockback vector, and also the Y-velocity to add if the entity is on the ground. xRatio The X part of the direction ratio of the knockback vector. zRatio The Z part of the direction ratio of the knockback vector. entityIn Quote Link to comment Share on other sites More sharing options...
Leomelonseeds Posted February 21, 2017 Share Posted February 21, 2017 Isint the vector the distance between the player position x minus entity position x?(same for y) Quote Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps. Link to comment Share on other sites More sharing options...
Eria8 Posted February 21, 2017 Author Share Posted February 21, 2017 (edited) 11 minutes ago, Leomelonseeds said: Isint the vector the distance between the player position x minus entity position x?(same for y) No, it's something to do with rotation. Like, as if the player is facing that direction, the entity is knocked backwards in that direction. I assume it's a 360 degree circle converted into a float. No idea if that would make 0.0f the world's North or the direction the player is facing at the time, if that's even correct. But I cannot figure out how to get that rotation in comparison to any entity the player isn't directly facing. Someone do correct me if I'm wrong. Edited February 21, 2017 by Eria8 Quote Link to comment Share on other sites More sharing options...
TheMasterGabriel Posted February 22, 2017 Share Posted February 22, 2017 (edited) I think it's the x component and y component of the vector as a whole. You can calculate each with trigonometry. You already have the magnitude of the vector (you set it to 4), so you can calculate the x and y ratios with (4*cos(angle)) and (4*sin(angle)) respectively. The angle would typically be bound between 0 and 360, but I think Minecraft interprets angles as being from -180 to 180, so you can clamp it via this method in MathHelper (I think this is the method, I can't check right now): public static int clampAngle(int angle) From a physics standpoint, this makes sense. I'm not sure that it corresponds to Minecraft though. Also worth mentioning that Math.cos and Math.sin accept the angle in radians. So first clamp it via the method above, then convert it to radians with Math.toRadians. Then use that angle in your calculations. Edited February 22, 2017 by TheMasterGabriel Quote Link to comment Share on other sites More sharing options...
Leomelonseeds Posted February 22, 2017 Share Posted February 22, 2017 Wow... Didnt know trigonometry was a thing here even. Quote Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps. Link to comment Share on other sites More sharing options...
TheMasterGabriel Posted February 22, 2017 Share Posted February 22, 2017 2 minutes ago, Leomelonseeds said: Wow... Didnt know trigonometry was a thing here even. It might not be, I'm just using intuition. I would check, but I'm not at my IDE right now. Quote Link to comment Share on other sites More sharing options...
Leomelonseeds Posted February 22, 2017 Share Posted February 22, 2017 I found on the internet the distance between two objects is distance AB=√(x2−x1)2+(y2−y1)2+(z2−z1)2 for more info check out: http://www.intmath.com/vectors/7-vectors-in-3d-space.php Quote Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps. Link to comment Share on other sites More sharing options...
Eria8 Posted February 22, 2017 Author Share Posted February 22, 2017 2 minutes ago, Leomelonseeds said: I found on the internet the distance between two objects is distance AB=√(x2−x1)2+(y2−y1)2+(z2−z1)2 for more info check out: http://www.intmath.com/vectors/7-vectors-in-3d-space.php Distance is simple. And irrelevant to the problem... Quote Link to comment Share on other sites More sharing options...
TheMasterGabriel Posted February 22, 2017 Share Posted February 22, 2017 3 hours ago, Eria8 said: But I don't understand how to get the x and z vectors for the knockback. Can someone help with this? I searched through the code a bit. Have you tried implementing the method vanilla uses to knockback an entity when it is hit? You can find the code in EntityLivingBase on lines 1057-1066. (I have no idea if line numbers correspond, they probably don't). It's in this method: public boolean attackEntityFrom(DamageSource source, float amount) Quote Link to comment Share on other sites More sharing options...
Eria8 Posted February 22, 2017 Author Share Posted February 22, 2017 Just now, TheMasterGabriel said: I searched through the code a bit. Have you tried implementing the method vanilla uses to knockback an entity when it is hit? You can find the code in EntityLivingBase on lines 1057-1066. (I have no idea if line numbers correspond, they probably don't). It's in this method: public boolean attackEntityFrom(DamageSource source, float amount) The vanilla method just knocks them toward the direction the player is facing. Quote Link to comment Share on other sites More sharing options...
TheMasterGabriel Posted February 22, 2017 Share Posted February 22, 2017 Just now, Eria8 said: The vanilla method just knocks them toward the direction the player is facing. It takes advantage of the fact that you cannot attack an entity without facing it directly. The attacker's rotation isn't actually considered when calculating how the entity should be moved, just the X and Z coordinates. This is why it is probably helpful to you if you want the surrounding entities to be knocked back as if they were being hit head-on. (I think this is what you want, correct me if I'm wrong). Also, perhaps Leomelonseeds wasn't entirely wrong in his presumption, as the shield knockback code reads: protected void blockUsingShield(EntityLivingBase p_190629_1_) { p_190629_1_.knockBack(this, 0.5F, this.posX - p_190629_1_.posX, this.posZ - p_190629_1_.posZ); } Quote Link to comment Share on other sites More sharing options...
Eria8 Posted February 22, 2017 Author Share Posted February 22, 2017 5 minutes ago, TheMasterGabriel said: It takes advantage of the fact that you cannot attack an entity without facing it directly. The attacker's rotation isn't actually considered when calculating how the entity should be moved, just the X and Z coordinates. This is why it is probably helpful to you if you want the surrounding entities to be knocked back as if they were being hit head-on. (I think this is what you want, correct me if I'm wrong). Also, perhaps Leomelonseeds wasn't entirely wrong in his presumption, as the shield knockback code reads: protected void blockUsingShield(EntityLivingBase p_190629_1_) { p_190629_1_.knockBack(this, 0.5F, this.posX - p_190629_1_.posX, this.posZ - p_190629_1_.posZ); } Nope, if this is what you mean, it still doesn't work. They still all get knocked back in one same direction. for (Entity e : entities) { if (e instanceof EntityLiving && !(e instanceof INPC)) { double x = e.posX - player.posX; // ??? double z = e.posZ - player.posZ; // ??? ((EntityLiving) e).knockBack(e, 4, x, z); } } Quote Link to comment Share on other sites More sharing options...
Leomelonseeds Posted February 22, 2017 Share Posted February 22, 2017 (edited) That was what I was saying in the second post... 4 hours ago, TheMasterGabriel said: Also, perhaps Leomelonseeds wasn't entirely wrong in his presumption I was taking a coding class about moving objects at a certian velocity towards the mouse.. Edited February 22, 2017 by Leomelonseeds Quote Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.