Jump to content

Recommended Posts

Posted

Hello. I have a block that iterates through a 16x16 area for EntityItems, however, I want to add motion towards these entities that 'pull' them towards the said block. I have been trying to do this for quite a while, however, I cannot figure out the math behind it. Currently, I'm finding the relative position of the items from the block and trying to add motion based on this. However, this ultimately failed and I just get items that fly everywhere and look very buggy. Any help would be appreciated.

 

Thanks.

Posted (edited)
        for(EntityItem item : items)
        {
            double moveFactor = 0.5f;
            double distX = this.pos.getX() - item.posX;
            double distZ = this.pos.getZ() - item.posZ;
            double distY = item.posY+1.5D - this.getPos().getY();
            double dir = MathHelper.atan2(distZ, distX);
            double speed = 1f / item.getDistance(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()) * moveFactor;

            if(distY<0)
            {
                item.motionY += speed;
            }

            item.motionX = MathHelper.cos((float)dir) * speed;
            item.motionZ = MathHelper.sin((float)dir) * speed;
        }

After more testing, this seems to work, however, the items teleport around in random directions. I'm guessing this is a syncing issue, however, I don't know how to actually fix that. Also, would there be any way to stop the items from stacking with each other, if so, how could I achieve it? 

Edited by unassigned
Posted
7 hours ago, unassigned said:

double dir = MathHelper.atan2(distZ, distX);

 

7 hours ago, unassigned said:

item.motionX = MathHelper.cos((float)dir) * speed;

item.motionZ = MathHelper.sin((float)dir) * speed;

Why are you using trig? If you just want to pull items to a spot then motion is [blockPos - itemPos] / someNumber. 

 

7 hours ago, unassigned said:

Also, would there be any way to stop the items from stacking with each other, if so, how could I achieve it? 

Doesn't look like there is a way to do this.

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.