Jump to content

Recommended Posts

Posted

Hey guys, I want to check if an entity is moving from inside my Tile Entity updateEntity method. I have already checked if entity.prevPosX != entity.posX, I've tried using entity.motionX, and I've tried storing the old coordinates in a variable and checking against them. Entity.motion* only works if the player is sprinting, and all of the other methods I've tried don't work. Does anyone know how to do this?

Posted

I'm checking on the server, and it is not just for the player, it is for any living entity. I am making a motion detector, so if any entity in the list moves, isDetected turns to true.

 

 

 if (!worldObj.isRemote){
		List list = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(this.xCoord - 10, this.yCoord - 10, this.zCoord - 10, this.xCoord + 10, this.yCoord + 10, this.zCoord + 10));
		if (list.isEmpty()){
			this.isDetecting = false;
		} else {
			for (int i = 0; i < list.size(); i ++){
				EntityLivingBase entity = (EntityLivingBase) list.get(i);

				if (entity.motionX != 0){
					this.isDetecting = true;
				} else {
					this.isDetecting = false;
				}					
			}
		}

 

Right now I'm focusing only on motionX, but it doesn't work for Y and Z also. A System.out.println() always prints out 0.0

Posted

Does anyone have a workaround for this? It appears coolAlias had troubles with this before, did you ever find a way to do it?

 

http://www.minecraftforge.net/forum/index.php?topic=13721.0

 

In that thread, TheGreyGhost gives the solution. Create your own field to remember the previous tick's position. Then each tick you compare current position to that previous position, do what you need to do, and then always update the previous position field with the current position.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

How could I do this when I have more than one entity to track, and I dont know how many entities that are being tracked? An ArrayList?

 

Yes, that would work.

 

I think I would do it like this in your tile entity:

1) create a hashmap field for mapping entities to their previous position

2) scan for entities in a bounding box around the tile entity with a getEntititesWithinAABB() type function and store that in a List or ArrayList.

3) update the hashmap keys by removing any entities that are no longer within range, and adding any new ones in range.

4) process the hashmap values (representing the previous position) as I mentioned above -- i.e. compare with current value, take action as needed.

5) update the hashmap values by setting the previous position to value of current position.

 

Note that you can store all three dimensions (x, y, z) of a position in a Vec3 so your hashmap could map entity to Vec3.

 

Definitely a bit of coding needed with this approach, but it should work.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

By the way, this is bad:

entity.motionX != 0

since motion fields are all floats, they suffer from approximation errors. See here, it is a really good article on that topic: http://www.theregister.co.uk/2006/08/12/floating_point_approximation/

So I'd suggest you check for "approximately" 0, like

entity.motionX > 0.0001 || entity.motionX < -0.0001

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

By the way, this is bad:

entity.motionX != 0

since motion fields are all floats, they suffer from approximation errors. See here, it is a really good article on that topic: http://www.theregister.co.uk/2006/08/12/floating_point_approximation/

So I'd suggest you check for "approximately" 0, like

entity.motionX > 0.0001 || entity.motionX < -0.0001

 

Yeah, I just quickly did that to show my point. I figured that out when I was testing for motionY, which is about 1.17. Thanks for reminding me though.

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.