[solved] Split facedir param2 into mounted wall and rotation

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

[solved] Split facedir param2 into mounted wall and rotation

by addi » Sun Sep 11, 2016 08:29

Hi,

Is there a way to split the numbers for param2 of 6d facedir nodes (numbers 0 -23) into the mounted wall and rotation?
And manipulate the rotation and put it again together to a valid param2?

To be more precise: I want to split the param2 eg. 19 to split into 0 ( = mounted on ground (-Y)) and 1 (=rotated about 90 degree).
Is this possible to calculate this, or do I have to write huge tables and a lot of if else?

I currently use this table, but it only lists the next param2 if you want to rotate clockwise:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
{
    -- Mounted on Ground (-Y)
    [10]=19,
    [19]=4,
    [4]=13,
    [13]=10,

    -- Mounted on Top (+Y)
    [8]=17,
    [17]=6,
    [6]=15,
    [15]=8,

    -- Mounted at South (-Z)
    [2]=18, -- down
    
[18]=22, -- left
    
[22]=14, -- up
    
[14]=2, -- right

    
-- Mounted at North (+Z)
    [0]=12, -- down
    
[12]=20, -- left
    
[20]=16, -- up
    
[16]=0, -- right

    
-- Mounted at West (-X)
    [3]=7, -- down
    
[7]=21, -- left
    
[21]=11, -- up
    
[11]=3, -- right

    
-- Mounted at East (+X)
    [1]=9, -- down
    
[9]=23, -- left
    
[23]=5, -- up
    
[5]=1, -- right
}

Thanks for any help
Last edited by addi on Tue Sep 13, 2016 17:38, edited 1 time in total.
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: Split param2 (6d facedir) into mounted wall and rotation

by kaeza » Sun Sep 11, 2016 18:37

I don't recall if the param2 first iterates "mounted wall" (0=floor, 1="front" wall, ..., 6=floor/rotate90, 7=front/rotate90, ...), or "rotation" (0=floor, 1=floor/rotate90, 2=floor/rotate180, ..., 4="front" wall, 5="front" wall/rotate90, ...), and too lazy to look at the code right now.

To decompose, you just need division and modulo (remainder) operations. Assuming first case above:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local NWALLS = 6 -- Number of possible "walls" (4 walls+floor+ceiling)
local wall = param2 % NWALLS
local rot = math.floor(param2/NWALLS)

-- Example:
-- param2 = 17
-- wall = param2 % NWALLS = 17 % 6 = 5
-- rot = param2 / NWALLS = 17 / 6 = 2 (floor division, actually)


To re-create a param2 from wall and rot, you just multiply and add:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local NWALLS = 6 -- Number of possible "walls" (4 walls+floor+ceiling)
local param2 = rot*NWALLS + wall

-- Continuing example from above:
-- wall = 5
-- rot = 2
-- param2 = rot*NWALLS + wall = 2*6 + 5 = 17


If it counts the other way around (rotation first), just replace NWALLS=6 by NROTS=4.

Hope that makes any sense.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: Split param2 (6d facedir) into mounted wall and rotation

by addi » Tue Sep 13, 2016 17:37

Thanks a lot! It seems it first iterates over rotation.
I got now some piece of valid code. maybe its also usefull for others:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
    on_punch = function(pos, node, player)
        local param2 = node.param2
        local NROT 
= 4 -- Number of possible "rotations" (4 Up down left right)
        local rot = param2 % NROT
        local wall 
= math.floor(param2/NROT)
        minetest.chat_send_player(player:get_player_name(), "Wall: "..wall.." Rot: "..rot)
        -- rotate clockwise
        if rot 
>=3 then
            rot 
= 0
        else 
            rot 
= rot +1
        end
        
-- save to node
        node
.param2 = wall*NROT+rot
        minetest
.swap_node(pos,node)
    end,
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: [solved] Split facedir param2 into mounted wall and rota

by kaeza » Wed Sep 14, 2016 08:00

You're quite welcome.

Instead of:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
if rot >= 3 then
  rot = 0
else
  rot = rot + 1
end

...you could compress it like so:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
rot = (rot + 1) % NROT
-- Example of wraparound: (3 + 1) % 4 = 0 (or (0 - 1) % 4 = 3)

This also allows you to e.g. rotate 180 degrees by using rot+2 instead (or rotate 90 degrees the other way with either rot+3 [270 degrees, I assume clockwise] or rot-1 [90 degrees counter-clockwise]).
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: [solved] Split facedir param2 into mounted wall and rota

by addi » Wed Sep 14, 2016 08:35

Oh thanks again! Does modulo work with negative numbers? Yes rot+x is clockwise.
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: [solved] Split facedir param2 into mounted wall and rota

by kaeza » Wed Sep 14, 2016 10:08

Yes it does ((-1)%4=3).
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

cron