Jump to content

[1.7.10] [SOLVED]getting entities in rayTrace


knokko

Recommended Posts

hello guys,

I am trying to get all entities in a raytrace.

I have made a class called Line and a working method to get all the entities in it.

But there is a problem with the raytrace of the player:

if the player is looking down, the list is glitching on the server side, but not on the client side.

If the player is look upwards, the method works great.

 

Here are the important methods:

public class TestItem extends Item {
public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player){
	player.swingItem();
	Line line = Line.fromRaytrace(player);
	Line line2 = new Line(new Position(10,10,10), new Position(0,0,0));
	line.spawnParticles(world, 0.1, 10, 10, 0.001);
	System.out.println(line.getEntities(world, Entity.class) + "   " + world.isRemote);
	//System.out.println(player.height * 0.8 + player.boundingBox.minY);
	return item;
}
}

public List getEntities(World world, Class entityClass){
	refresh();
	double minX = Math.min(position1.x, position2.x);
	double maxX = Math.max(position1.x, position2.x);
	double minY = Math.min(position1.y, position2.y);
	double maxY = Math.max(position1.y, position2.y);
	double minZ = Math.min(position1.z, position2.z);
	double maxZ = Math.max(position1.z, position2.z);
	//System.out.println("minX = " + minX + " maxX = " + maxX + " minY = " + minY + " maxY = " + maxY + " minZ = " + minZ + " maxZ = " + maxZ + " isRemote = " + world.isRemote);
	List entities = world.getEntitiesWithinAABB(entityClass, AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ));
	//System.out.println("entities = " + entities + " isRemote = " + world.isRemote);
	double factorXY = ExtraFeatures.divineAccurate(distanceYT, distanceXT);
	double factorXZ = ExtraFeatures.divineAccurate(distanceZT, distanceXT);
	double bufferXY = position1.y - (position1.x * factorXY);
	double bufferXZ = position1.z - (position1.x * factorXZ);
	//System.out.println("factorXY = " + factorXY + " factorXZ = " + factorXZ + " bufferXY = " + bufferXY + " bufferXZ = " + bufferXZ + " isRemote = " + world.isRemote);
	List entities2 = new ArrayList();
	int times = 0;
	while(times < entities.size()){
		Entity entity = (Entity) entities.get(times);
		AxisAlignedBB aabb = entity.boundingBox;
		//System.out.println("boundingbox = " + aabb + "   entity = " + entity + "   isRemote = " + world.isRemote);
		//System.out.println("rotationYaw = " + entity.rotationYaw + " rotationPitch = " + entity.rotationPitch + " entity = " + entity);
		double d = (aabb.minX * factorXY) + bufferXY;
		double e = (aabb.maxX * factorXY) + bufferXY;
		double m = (aabb.minX * factorXZ) + bufferXZ;
		double n = (aabb.maxX * factorXZ) + bufferXZ;
		//System.out.println("d = " + d + " e = " + e + " m = " + m + " n = " + n + " entity = " + entity + "isRemote = " + world.isRemote);
		boolean flag1 = m <= aabb.maxZ && n >= aabb.minZ || m >= aabb.minZ && n <= aabb.maxZ;
		boolean flag2 = d <= aabb.maxY && e >= aabb.minY || d >= aabb.minY && e <= aabb.maxY;
		if(flag1 && flag2){
			entities2.add(entity);
		}
		++times;
	}
	System.out.println("line = " + this + "   isRemote = " + world.isRemote);
	return entities2;
}
public static Line fromRaytrace(EntityLivingBase entity){
	Position pos1 = new Position(entity.posX, entity.height * 0.8 + entity.boundingBox.minY, entity.posZ);
	Position pos2 = new Position(entity.rayTrace(100, 1));
	return new Line(pos1, pos2);
}

 

Here is the whole line class:

public class Line {
public Line(Position pos1, Position pos2){
	position1 = pos1;
	position2 = pos2;
	refresh();
}
private Position position1;
private Position position2;

public double distanceX;
public double distanceY;
public double distanceZ;

protected double distanceXT;
protected double distanceYT;
protected double distanceZT;
public double distance;

public void refresh(){
	distanceX = position2.x - position1.x;
	distanceY = position2.y - position1.y;
	distanceZ = position2.z - position1.z;
	distanceXT = distanceX;
	distanceYT = distanceY;
	distanceZT = distanceZ;
	if(distanceX < 0){
		distanceX *= -1;
	}
	if(distanceY < 0){
		distanceY *= -1;
	}
	if(distanceZ < 0){
		distanceZ *= -1;
	}
	double distanceXZ = Math.hypot(distanceX, distanceZ);
	distance = Math.hypot(distanceXZ, distanceY);
}
public Position getPosition(int i){
	if(i <= 1){
		return position1;
	}
	else {
		return position2;
	}
}

public void setPosition(int i, Position pos){
	if(i <= 1){
		position1 = pos;
	}
	else {
		position2 = pos;
	}
	refresh();
}

/**
 * 
 * @param world the world to spawn the particles.
 * @param red how red the particles will be.
 * @param green how green the particles will be.
 * @param blue how blue the particles will be.
 * @param dbp distance between particles.
 */
public void spawnParticles(World world, double red, double green, double blue, double dbp){
	double speedX = distanceXT * dbp;
	double speedY = distanceYT * dbp;
	double speedZ = distanceZT * dbp;
	double x = position1.x;
	double y = position1.y;
	double z = position1.z;
	int times = 0;
	int particles = ExtraFeatures.fromDouble(ExtraFeatures.divineAccurate(position1.getSquaredDistance(position2), dbp));
	while(times <= particles){
		world.spawnParticle("reddust", x, y, z, red, green, blue);
		x += speedX;
		y += speedY;
		z += speedZ;
		++times;
	}

}
public String toString(){
	return "Line: distance = " + distance + "  position1 = [" + position1 + "], position2 = [" + position2 + "]";
}
public List getEntities(World world, Class entityClass){
	refresh();
	double minX = Math.min(position1.x, position2.x);
	double maxX = Math.max(position1.x, position2.x);
	double minY = Math.min(position1.y, position2.y);
	double maxY = Math.max(position1.y, position2.y);
	double minZ = Math.min(position1.z, position2.z);
	double maxZ = Math.max(position1.z, position2.z);
	//System.out.println("minX = " + minX + " maxX = " + maxX + " minY = " + minY + " maxY = " + maxY + " minZ = " + minZ + " maxZ = " + maxZ + " isRemote = " + world.isRemote);
	List entities = world.getEntitiesWithinAABB(entityClass, AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ));
	//System.out.println("entities = " + entities + " isRemote = " + world.isRemote);
	double factorXY = ExtraFeatures.divineAccurate(distanceYT, distanceXT);
	double factorXZ = ExtraFeatures.divineAccurate(distanceZT, distanceXT);
	double bufferXY = position1.y - (position1.x * factorXY);
	double bufferXZ = position1.z - (position1.x * factorXZ);
	//System.out.println("factorXY = " + factorXY + " factorXZ = " + factorXZ + " bufferXY = " + bufferXY + " bufferXZ = " + bufferXZ + " isRemote = " + world.isRemote);
	List entities2 = new ArrayList();
	int times = 0;
	while(times < entities.size()){
		Entity entity = (Entity) entities.get(times);
		AxisAlignedBB aabb = entity.boundingBox;
		//System.out.println("boundingbox = " + aabb + "   entity = " + entity + "   isRemote = " + world.isRemote);
		//System.out.println("rotationYaw = " + entity.rotationYaw + " rotationPitch = " + entity.rotationPitch + " entity = " + entity);
		double d = (aabb.minX * factorXY) + bufferXY;
		double e = (aabb.maxX * factorXY) + bufferXY;
		double m = (aabb.minX * factorXZ) + bufferXZ;
		double n = (aabb.maxX * factorXZ) + bufferXZ;
		//System.out.println("d = " + d + " e = " + e + " m = " + m + " n = " + n + " entity = " + entity + "isRemote = " + world.isRemote);
		boolean flag1 = m <= aabb.maxZ && n >= aabb.minZ || m >= aabb.minZ && n <= aabb.maxZ;
		boolean flag2 = d <= aabb.maxY && e >= aabb.minY || d >= aabb.minY && e <= aabb.maxY;
		if(flag1 && flag2){
			entities2.add(entity);
		}
		++times;
	}
	System.out.println("line = " + this + "   isRemote = " + world.isRemote);
	return entities2;
}
public static Line fromRaytrace(EntityLivingBase entity){
	Position pos1 = new Position(entity.posX, entity.height * 0.8 + entity.boundingBox.minY, entity.posZ);
	Position pos2 = new Position(entity.rayTrace(100, 1));
	return new Line(pos1, pos2);
}
}

 

Here is the position class:

public class Position {
public double x;
public double y;
public double z;
/**
 * Creates a new position on the given location.
 * @param posX The x location of the position.
 * @param posY The y location of the position.
 * @param posZ The z location of the given position.
 */
public Position(double posX, double posY, double posZ){
	x = posX;
	y = posY;
	z = posZ;
}
/**
 * Creates a new position on the given location, this constructor works with integers.
 * @param posX The x location of the position.
 * @param posY The y location of the position.
 * @param posZ The z location of the given position.
 */
public Position(int posX, int posY, int posZ){
	x = posX;
	y = posY;
	z = posZ;
}
/**
 * Creates a new position on the given location, this constructor works with floats.
 * @param posX The x location of the position.
 * @param posY The y location of the position.
 * @param posZ The z location of the given position.
 */
public Position(float posX, float posY, float posZ){
	x = posX;
	y = posY;
	z = posZ;
}
/**
 * Creates a new position at the given entity.
 * @param entity The entity where the position has to be created.
 */
public Position(Entity entity){
	x = entity.posX;
	y = entity.posY;
	z = entity.posZ;
}

public Position(MovingObjectPosition mop){
	x = mop.hitVec.xCoord;
	y = mop.hitVec.yCoord;
	z = mop.hitVec.zCoord;
}
public Position(Vec3 vec3){
	x = vec3.xCoord;
	y = vec3.yCoord;
	z = vec3.zCoord;
}
/**
 * Returns this position as string.
 */
public String toString(){
	String string = "Position: x = " + x + ", y = " + y + ", z = " + z;
	return string;
}
/**
 * Makes a string for the given position.
 * @param p
 * @return the string.
 */
public static String makeString(Position p){
	return p.toString();
}
/**
 * Gives the x of this position as integer.
 * @return The integer the double is the closest to.
 */
public int intX(){
	double t = x - (int)x;
	if(t >= 0.5){
		t = 1;
	}
	else {
		t = 0;
	}
	return (int) ((int)x + t);
}
/**
 * Gives the y of this position as integer.
 * @return The integer the double is the closest to.
 */
public int intY(){
	double t = y - (int)y;
	if(t >= 0.5){
		t = 1;
	}
	else {
		t = 0;
	}
	return (int) ((int)y + t);
}
/**
 * Gives the z of this position as integer.
 * @return The integer the double is the closest to.
 */
public int intZ(){
	double t = z - (int)z;
	if(t >= 0.5){
		t = 1;
	}
	else {
		t = 0;
	}
	return (int) ((int)z + t);
}
/**
 * this method gives the x of the position as float.
 * @return The x of the position as float.
 */
public float floatX(){
	return (float) x;
}
/**
 * this method gives the y of the position as float.
 * @return The y of the position as float.
 */
public float floatY(){
	return (float)y;
}
/**
 * this method gives the z of the position as float.
 * @return The z of the position as float.
 */
public float floatZ(){
	return (float)z;
}
/**
 * Gives the squared distance to another position. It will use Math.hypot for getting it.
 * @param p The other position.
 * @return The squared distance from this position to the given position.
 */
public double getSquaredDistance(Position p){
	double distanceX;
	double distanceY;
	double distanceZ;
	if(x >= p.x){
		distanceX = x - p.x;
	}
	else {
		distanceX = p.x - x;
	}
	if(y >= p.y){
		distanceY = y - p.y;
	}
	else {
		distanceY = p.y - y;
	}
	if(z >= p.z){
		distanceZ = z - p.z;
	}
	else {
		distanceZ = p.z - z;
	}
	double distanceXZ = Math.hypot(distanceX, distanceZ);
	return Math.hypot(distanceXZ, distanceY);
}
/**
 * Gives the distance to the given position. This will just use distanceX + distanceY + distanceZ.
 * @param p The other position.
 * @return The distance to the given position.
 */
public double getIndirectDistance(Position p){
	double distanceX;
	double distanceY;
	double distanceZ;
	if(x >= p.x){
		distanceX = x - p.x;
	}
	else {
		distanceX = p.x - x;
	}
	if(y >= p.y){
		distanceY = y - p.y;
	}
	else {
		distanceY = p.y - y;
	}
	if(z >= p.z){
		distanceZ = z - p.z;
	}
	else {
		distanceZ = p.z - z;
	}
	return distanceX + distanceY + distanceZ;
}
/**
 * Gives the squared distance between two positions. It will use Math.hypot.
 * @param a The first position.
 * @param p The second position.
 * @return The squared distance between the two positions.
 */
public static double getSquaredDistance(Position a, Position p){
	double distanceX;
	double distanceY;
	double distanceZ;
	if(a.x >= p.x){
		distanceX = a.x - p.x;
	}
	else {
		distanceX = p.x - a.x;
	}
	if(a.y >= p.y){
		distanceY = a.y - p.y;
	}
	else {
		distanceY = p.y - a.y;
	}
	if(a.z >= p.z){
		distanceZ = a.z - p.z;
	}
	else {
		distanceZ = p.z - a.z;
	}
	double distanceXZ = Math.hypot(distanceX, distanceZ);
	return Math.hypot(distanceXZ, distanceY);
}
/**
 * Gives the distance between the given positions. This will just use distanceX + distanceY + distanceZ.
 * @param p The first position.
 * @param a The second position.
 * @return The distance between the given positions.
 */
public static double getIndirectDistance(Position a, Position p){
	double distanceX;
	double distanceY;
	double distanceZ;
	if(a.x >= p.x){
		distanceX = a.x - p.x;
	}
	else {
		distanceX = p.x - a.x;
	}
	if(a.y >= p.y){
		distanceY = a.y - p.y;
	}
	else {
		distanceY = p.y - a.y;
	}
	if(a.z >= p.z){
		distanceZ = a.z - p.z;
	}
	else {
		distanceZ = p.z - a.z;
	}
	return distanceX + distanceY + distanceZ;
}
/**
 * This method will write itself in the given NBTTagCompound.
 * It will set a tag with the given key, and set the doubles there.
 * This method is not called automatically, so save it where you need it.
 * Be sure to use the same key at readFromNBt and to use another key for every position.
 * @param nbt The NBTTagCompound where it will save its position.
 * @param key The key it will use to create a new tag. 
 */
public void writeToNBT(NBTTagCompound nbt, String key){
	NBTTagCompound a = new NBTTagCompound();
	a.setDouble("x", x);
	a.setDouble("y", y);
	a.setDouble("z", z);
	nbt.setTag(key, a);
}
/**
 * This method is made to read the position from the given NBTTagCompound.
 * Be sure to use the same NBTTagCompound and string as writeToNBT.
 * @param nbt The NBTTagCompound it will use to read the position.
 * @param key The tag of the NBTTagCompound it will check.
 */
public void readFromNBT(NBTTagCompound nbt, String key){
	x = nbt.getCompoundTag(key).getDouble("x");
	y = nbt.getCompoundTag(key).getDouble("y");
	z = nbt.getCompoundTag(key).getDouble("z");
}
/**
 * Spawns an entity at the given position.
 * @param p The position the entity has to spawn.
 * @param entity The entity to spawn.
 * @param world The world to spawn the entity.
 */
public static void spawnEntity(Position p, Entity entity, World world){
	entity.posX = p.x;
	entity.posY = p.y;
	entity.posZ = p.z;
	world.spawnEntityInWorld(entity);
}
/**
 * Spawns an entity at this position.
 * @param entity The entity to spawn.
 * @param world The world to spawn the entity.
 */
public void spawnEntity(Entity entity, World world){
	entity.posX = x;
	entity.posY = y;
	entity.posZ = z;
	world.spawnEntityInWorld(entity);
}
/**
 * Sets a block at the given position.
 * @param p The position to place the block.
 * @param world The world to set the block.
 * @param block The block to set.
 */
public static void setBlock(Position p, World world, Block block){
	world.setBlock(p.intX(), p.intY(), p.intZ(), block);
}
/**
 * Sets a block at this position.
 * @param world The world to place the block.
 * @param block The block to place.
 */
public void setBlock(World world, Block block){
	world.setBlock(intX(), intY(), intZ(), block);
}
/**
 * Spawns a particle at the given position with the given colors.
 * @param world The world to spawn the particle.
 * @param p The position to spawn the particle.
 * @param color1 The amount red.
 * @param color2 The amount green.
 * @param color3 The amount purple/blue.
 */
public static void spawnParticle(World world, Position p, double color1, double color2, double color3){
	world.spawnParticle("reddust", p.x, p.y, p.z, color1, color2, color3);
}
/**
 * Spawn a particle at this position with the given colors.
 * @param world The world to spawn the particle.
 * @param color1 The amount red.
 * @param color2 The amount green.
 * @param color3 The amount purple/blue.
 */
public void spawnParticle(World world, double color1, double color2, double color3){
	world.spawnParticle("reddust", x, y, z, color1, color2, color3);
}
}

 

The lines in the console are like this:

if I am looking upwards:

 

line = Line: distance = 99.74725627990941  position1 = [Position: x = -4.495057316005686, y = 5.439999961853028, z = -10.195982839251545], position2 = [Position: x = -25.012235769700702, y = 57.0, z = 72.69024593088521]  isRemote = true

[EntityClientPlayerMP['ForgeDevName'/31, l='MpServer', x=-4,50, y=5,62, z=-10,20], EntityGhast['Ghast'/2209, l='MpServer', x=-6,94, y=8,66, z=0,07]]  true

line = Line: distance = 99.03257273283333  position1 = [Position: x = -4.495057316005686, y = 5.439999961853028, z = -10.195982839251545], position2 = [Position: x = -24.143300547076613, y = 55.43825432565619, z = 73.0]  isRemote = false

[EntityPlayerMP['ForgeDevName'/31, l='Test', x=-4,50, y=4,00, z=-10,20], EntityGhast['Ghast'/2209, l='Test', x=-6,28, y=8,53, z=0,73]]  false

 

if I am looking down:

 

line = Line: distance = 12.117373369229384  position1 = [Position: x = -8.74619552137722, y = 5.439999961853028, z = -9.583500283127876], position2 = [Position: x = -20.477986926741583, y = 4.0, z = -6.9147537795813845]  isRemote = true

[EntityCreeper['Creeper'/32, l='MpServer', x=-16,13, y=4,00, z=-7,78], EntityClientPlayerMP['ForgeDevName'/31, l='MpServer', x=-8,75, y=5,62, z=-9,58]]  true

line = Line: distance = 1.4399999618530277  position1 = [Position: x = -8.74619552137722, y = 5.439999961853028, z = -9.583500283127876], position2 = [Position: x = -8.74619552137722, y = 4.0, z = -9.583500283127876]  isRemote = false

[]  false

 

 

Can anybody explain why the raytrace is doing so strange on the server?

Link to comment
Share on other sites

Hi

 

Could you explain your algorithm a bit?  I'm not clear what it's supposed to do (and how it's supposed to do it)

 

By the way, did you know that the player y position on the server corresponds to player feet, but the player y position on the client corresponds to the player eyes?

 

There is a Entity method called eyeHeight or something similar which helps you correct for that.

 

-TGG

Link to comment
Share on other sites

I allready know that a player has another posY on the server and client.

I have allready solved this by using the boundingbox.minY + height * 0.8, this is the same on client and server.

The strangest I found was that the rayTrace was acting very stange when a player looks down and on the server side, if you look at the console messages what the line is.

The rayTrace method of EntityLivingBase seems to look directly at the ground when it is called on server side AND the entity is looking down a little bit.

 

The way I check the entity in the line is not very difficult:

I make a sort of formula with the line first, and than I check if the formula works on the entity.

I make 2 of them because it is a 3D world.

Link to comment
Share on other sites

I allready know that a player has another posY on the server and client.

I have allready solved this by using the boundingbox.minY + height * 0.8, this is the same on client and server.

The strangest I found was that the rayTrace was acting very stange when a player looks down and on the server side, if you look at the console messages what the line is.

The rayTrace method of EntityLivingBase seems to look directly at the ground when it is called on server side AND the entity is looking down a little bit.

 

The way I check the entity in the line is not very difficult:

I make a sort of formula with the line first, and than I check if the formula works on the entity.

I make 2 of them because it is a 3D world.

 

    /**

    * Performs a ray trace for the distance specified and using the partial tick time. Args: distance, partialTickTime

    */

    @SideOnly(Side.CLIENT)

    public MovingObjectPosition rayTrace(double p_70614_1_, float p_70614_3_)

 

It is not valid to call rayTrace on the server, and will probably cause random crashes.  I'd suggest you copy the code to your own class, it's not complicated.

 

Use Entity.eyeHeight() to compensate for the eye height difference on the client and server

 

-TGG

 

 

Link to comment
Share on other sites

I have found the problem, the rayTrace method in entityLivingBase uses the posX field op EntityPlayer, and this field has a different value on the serverside. I have read rayTrace and used the good parts of it to let my method work. Thank you for the tip.

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.