Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.12] Direct change of entities moving vector due to collision with block


Recommended Posts

Good evening,


it's me again. This time I have something... well, its a bit stupid I think. 


The testing environment: Okay. Again, there is a picture to look at. That red cross is made out of custom blocks, which are no full blocks (as one might have expected; this isn't a texture trick). Their aabb has been resized to fit their visual appearance, aaand... north is up. Oh, these blocks are non solid, so you can walk through. 


The target situation: Every entity that collides with the block should be pushed back (force may vary) in the general direction it came from, or better: at a rough 90° angle from the surface it touches. 


The experiment: I found onEntityCollidedWithBlock() and started fooling around with whats possible with this function. And somehow it condensed towards some kind of repulsing block; like the slime, which you can use for high-jumping. But from all sides. So I could not reuse the onLand() Method, or what its name was, but instead went to look to get this done using onEntityCollidedWithBlock().

I tried out a lot of things; firstly I misunderstood what the vector methods where for. When hearing "vector" I think of some kind of movement, but after I teleported myself around to crazy extends, I realized that it might not be the movement-redirection I was looking for. Than I found the motionXYZ fields, and the moveRelative() method. Last one was foolproof to use, but when I looked sideway and tried to apply negative forward momentum, my player got accelerated alonside the "wall" of repulsing blocks. I used this to send Zombies to the moon, by digging a 3x3 hole with walls out of repulsor blocks and let 1x1 free in the middle, used an egg at the first block and... wheeeeeeeeeeee. :)


Ehem, back to topic. After "moveRelative() didn't yield the result I hoped for I went back to the three motion-fields. Because I still suck working with the blocks facing-stuff, Went for something else and got the coordinates of the block and the entity colliding with it, looked for the difference (block - entity) and got usually a value of 1 or -1 for x, y or z. 

After finding out, that you move with negative preposition when going towards the smaller coordinate value and via versa, I just added entityIn.setVelocity((holoX-entiX)*-1,(holoY-entiY)*-1,(holoZ-entiZ)*-1)


Here is that method - the super-call seems not to have any influence to the success or fail

    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {

        double inputX= Math.round(entityIn.motionX*100)/100;
        double inputY= Math.round(entityIn.motionY*100)/100;
        double inputZ= Math.round(entityIn.motionZ*100)/100;

        double entiX = entityIn.getPosition().getX();
        double entiY = entityIn.getPosition().getY();
        double entiZ = entityIn.getPosition().getZ();

        double holoX =  pos.getX();
        double holoY =  pos.getY();
        double holoZ =  pos.getZ();

        double outputX=(holoX-entiX)*-1;
        double outputY=(holoY-entiY)*-1;
        double outputZ=(holoZ-entiZ)*-1;

        try {
           worldIn.getPlayerEntityByName(entityIn.getName()).sendMessage(new TextComponentString("holo: x: " + holoX +", y: " + holoY + ", z: "+ holoZ + ";"));
           worldIn.getPlayerEntityByName(entityIn.getName()).sendMessage(new TextComponentString("enti: x: " + entiX +", y: " + entiY + ", z: "+ entiZ + ";"));
           worldIn.getPlayerEntityByName(entityIn.getName()).sendMessage(new TextComponentString("dif: x: " + (holoX-entiX) +", y: " + (holoY-entiY) + ", z: "+ (holoZ-entiZ) + ";"));
           worldIn.getPlayerEntityByName(entityIn.getName()).sendMessage(new TextComponentString("input vel: x: " + inputX +", y: " + inputY + ", z: "+ inputZ + ";"));
           worldIn.getPlayerEntityByName(entityIn.getName()).sendMessage(new TextComponentString("output vel: x: " + outputX +", y: " + outputY + ", z: "+ outputZ + ";"));


        }catch(Exception e){
            //just keep on going in case something is going insane

        super.onEntityCollidedWithBlock(worldIn, pos, state, entityIn);


The problem: Okay. That should do the trick, I think. Lets say, in theory that seems to work. In "reality" (or in game) something unexpected does happen: when touching the blocks from north or west (moving to south or east), I just clip though the blocks (that looks kind of strange), while the "debugging output" to the chat-console tells me, that the "new" motionXYZ should be the correct value. 

Even more strange, when comming from south to north, from east to west, from up to down (and from down to up... combine both for a grand shaking) the redirection works perfectly. It will keep drops jumpuing until they despawn, or the player jumping until you went off. 

But these two directions (north->south and west->east) wont work. The best that happens is, that I slowly... warp through the blocks, just to get slinged away at the other side. 


So... I know this is far from being productive work in any form, but I'm afraid that I need to use velocity and vector handling later to a greater extent, so it cannot hurt to know what one can do wrong or understand wrongly. 



Greetings, Merlin 



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.

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.


  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.