Jump to content

[1.7.10] How to detect if an entity is moving?


TheRealMcrafter

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • plz help me fix this!!: it wasn't doing this before, whenever I try to join my world it just stucks on the options screen and the 0% never ever shows up, and the game is now frozen! help me, please HELP! https://paste.gg/p/anonymous/0b4686cd036d496db16ec497b1ec7fb9
    • Internal ExeptIon server bug Image LInk  l V https://lens.google.com/search?ep=gsbubb&hl=en-CA&re=df&p=AbrfA8p0hRZLHI5ozxFtMWh8xA21sqBQ71eivErBLG_oF8j-5G7yFOjJQP7DxnD3oOFBAYE4ajAvyOag8ykwGITxwfBg-8CpFUB0plaWJyrGKiw28bj9LohjoyyI07OsFTE5vJa1o3aKF80ocbEG8U_v5QhX_B5B3k370goGoohHkTodvClNPrBATvS6rYMKO43iTr_QbdYL_78wxQ%3D%3D#lns=W251bGwsbnVsbCxudWxsLG51bGwsbnVsbCxudWxsLG51bGwsIkVrY0tKR1E1T1RjeU5ETTVMV05pTVRVdE5HWXdNeTA1TmpWbUxXUXdOekZtWVdZeE9EWTJZaElmYTNoV05VWmFVREJJYTFGVWMwNXBYM3AxVm1GWlRWVTNSRnBoTFVob2F3PT0iLG51bGwsbnVsbCxudWxsLG51bGwsbnVsbCxudWxsLG51bGwsWyI4ZDdmNDE1Yi00ZWViLTQ2NzItOWQyOS05MTA2MWNmMzYyNjciXV0=
    • Ensure your system is running the latest version of Java. Sodium requires Java 17 or later for newer Minecraft versions (like 1.17+). 
    • Hi I wanted to my custom mob to hold any sword item, but didn’t rendered properly.   In entity class, make entity hold items as below: @Override public InteractionResult mobInteract(Player pPlayer, InteractionHand pHand) { //… ItemStack itemstack = pPlayer.getItemInHand(pHand); if (this.isTame()) { if ( (itemstack.is(Items.MELON_SLICE) || itemstack.is(Items.HONEY_BOTTLE)) && this.getHealth() < this.getMaxHealth() ) { //… } /* Handle holding sword */ else if (itemstack.getItem() instanceof SwordItem) { pPlayer.displayClientMessage(Component.literal("Clicked with item: " + itemstack.getDisplayName().getString()).withStyle(ChatFormatting.GOLD), true); //Return sword //pPlayer.getInventory().add(this.getItemInHand(InteractionHand.MAIN_HAND)); pPlayer.getInventory().add(this.getItemBySlot(EquipmentSlot.MAINHAND)); //The entity holds item //this.setItemInHand(InteractionHand.MAIN_HAND, itemstack); this.setItemSlot(EquipmentSlot.MAINHAND, itemstack.copy()); //Give copy of itemstack //If player is not in creative mode. From mobInteract() in wolf. if (!pPlayer.getAbilities().instabuild) { //Decrement sword count in hand pPlayer.getItemInHand(pHand).shrink(1); } return InteractionResult.SUCCESS; } else { //If player is sneaking pPlayer.displayClientMessage(getItemInHand(InteractionHand.MAIN_HAND).getDisplayName(), false); if (pPlayer.isShiftKeyDown()) { //Return sword //pPlayer.getInventory().add(this.getItemInHand(InteractionHand.MAIN_HAND)); pPlayer.getInventory().add(this.getItemBySlot(EquipmentSlot.MAINHAND)); //The entity holds nothing this.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); return InteractionResult.SUCCESS; } else { //… } } else { return interactionresult; } }   And in render class, render the item as below: @Override public void render(RanaEntity pEntity, float pEntityYaw, float pPartialTicks, PoseStack pMatrixStack, MultiBufferSource pBuffer, int pPackedLight) { if(pEntity.isBaby()) { pMatrixStack.scale(0.5f, 0.5f, 0.5f); } model.setupAnim(pEntity, 0, 0, 0, pEntityYaw, 0); // //Get location and rotation of arm bone ModelPart rightArm = model.rightArm(); //Get right arm //Get location and rotation of item according to arm bone pMatrixStack.pushPose(); pMatrixStack.translate(rightArm.x, rightArm.y, rightArm.z); //Move to bone location pMatrixStack.mulPose(Axis.XP.rotationDegrees(rightArm.xRot)); //Rotate X pMatrixStack.mulPose(Axis.YP.rotationDegrees(rightArm.yRot)); //Rotate Y pMatrixStack.mulPose(Axis.ZP.rotationDegrees(rightArm.zRot)); //Rotate Z //Draw item //ItemStack itemStack = pEntity.getItemInHand(InteractionHand.MAIN_HAND); ItemStack itemStack = pEntity.getItemBySlot(EquipmentSlot.MAINHAND); if (!itemStack.isEmpty()) { //Offset pMatrixStack.translate(0.0, 0.0, 0.1); // Render the item //Minecraft.getInstance().getItemRenderer().renderStatic(itemStack, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, pPackedLight, OverlayTexture.NO_OVERLAY, pMatrixStack, pBuffer, pEntity.level(), pEntity.getId()); //itemRenderer.renderStatic(itemStack, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, pPackedLight, OverlayTexture.NO_OVERLAY, pMatrixStack, pBuffer, pEntity.level(), pEntity.getId()); itemInHandRenderer.renderItem(pEntity, itemStack, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, false, pMatrixStack, pBuffer, pEntity.getId()); } pMatrixStack.popPose(); super.render(pEntity, pEntityYaw, pPartialTicks, pMatrixStack, pBuffer, pPackedLight); }   I confirmed the entity can properly hold item(logically) but the item which the entity holds is not rendered at all.   Full code: https://github.com/sakiiiiika/ranamod   Thanks.
  • Topics

×
×
  • Create New...

Important Information

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