Jump to content

[1.11] Knock back entities within radius of player?


Eria8

Recommended Posts

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 

 

Link to comment
Share on other sites

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 by Eria8
Link to comment
Share on other sites

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 by TheMasterGabriel
Link to comment
Share on other sites

I found on the internet the distance between two objects is  distance AB=(x2x1)2+(y2y1)2+(z2z1)2  

for more info check out: http://www.intmath.com/vectors/7-vectors-in-3d-space.php

 

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

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)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}

 

Link to comment
Share on other sites

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);
			}
		}

 

Link to comment
Share on other sites

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 by Leomelonseeds

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

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.