#Vector Math Help
1 messages · Page 1 of 1 (latest)
I wanna know what math i have to apply to this so the blocks always go to the right direction of where im facing like this (the orange is the block i placed manually) and the red ones are placed by the script.
the problem im having is that if im facing to where im facing in the pic, you see the orange block, the red blocks are going to the wrong direction, they should go to the right side like the first image no matter where im facing.
and this is also happening for horizontals.
when doing for example a table, this is horizontal is on the floor it should look like this
but sometimes it might do this
or this depending where im looking at.
when it should always do like the first image of the horizontals, go to the top right which is not happening
sorry for too much messages but im making sure it's as explained as possible so people knows what's my problem
Will you be using a custom block?
first of all you have to get the stripped vector of the placement direction, basicly check dominant vector of the view for example [0.2, 0.6, -0.5], dominant vector axis is Y positive, so base direction vector would be [0, 1, 0]. Then use cross product function to found relative coords for next blocks to right and top relative to you base vector you have now and place the blocks to the results.
Ask ChatGPT with this message for better context
use this function from herobrine
function cardinal_direction({ x: pitch, y: yaw }) {
if (pitch > 60) return 0.0; //Down
if (pitch < -60) return 1.0; //Up
if (Math.abs(yaw) > 112.5) return 2.0; //North
if (Math.abs(yaw) < 67.5) return 3.0; //South
if (yaw < 157.5 && yaw > 22.5) return 4.0; //West
if (yaw > -157.5 && yaw < -22.5) return 5.0; //East
return 6.0; //Undefined
};```
pass the player rotation to it and you will get a different result depending on the player rotation
use that to determine what placement to trigger
I would suggect using ViewVector but this also works
i mean
damn i dont know how to explain it
but i dont know what to do with that cardinal direction
so the code does what i want it to do\
If so if it were me, I would just be lazy. Blocks have built in block rotations made by mojang so you can specify if your block blocks rotates like a furnace, log, etc. So I would just use that and then in your script get that block state and have an array or object with 4-16 hard coded block positions. And while it is more work, it will match with the vanilla blocks perfectly and will require close to no math.
Yes, I know, but they are the same concept.
import { world, BlockPermutation} from '@minecraft/server';
const blocksInfosFromDirection = {
Down: [
{
blockId: "minato:block1",
relativePosition: {x:0,y:0,z:1},
states: {"state:name1":"value","state:name2":"value" }
},
{
blockId: "minato:block2",
relativePosition: {x:1,y:0,z:1},
states: {"state:name1":"value","state:name2":"value" }
},
{
blockId: "minato:block3",
relativePosition: {x:1,y:0,z:0},
states: {"state:name1":"value","state:name2":"value" }
}
],
//...
//the rest
//...
}
world.afterEvents.playerPlaceBlock.subscribe((event)=>{
const {block,player,dimension} = event
if(block.typeId !== 'your block') return
const dir = cardinal_direction(player.getRotation())
if(!dir) return
const data = blocksInfosFromDirection[dir]
for(const info of data){
const permutation = BlockPermutation.resolve(info.blockId, info.states)
dimension.setBlockPermutation(
{
x: block.location.x + info.relativePosition.x,
y: block.location.y + info.relativePosition.y,
z: block.location.z + info.relativePosition.z
},
permutation
)
}
})
function cardinal_direction({ x: pitch, y: yaw }) {
if (pitch > 60) return "Down";
if (pitch < -60) return "Up";
if (Math.abs(yaw) > 112.5) return "North";
if (Math.abs(yaw) < 67.5) return "South";
if (yaw < 157.5 && yaw > 22.5) return "West";
if (yaw > -157.5 && yaw < -22.5) return "East";
return undefined;
};
something like this
Maybe you don't see it but it is the same thing that you were looking for.
You are looking to figure out a blocks placement based on where the player is looking for a special block right? Well the normal block rotation code already calculates the players correct rotation for a blocks placement. You would just need to make the two systems work together.
No...
-# Uhm
If you don't want to go with a block state then don't. I was just providing an option that requires no math since that sounded like your original issue. Anyway Minato posted a solution that looks like it will work fine for your needs.