vector.abs

Post Reply
Oil_boi
Member
Posts: 139
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

vector.abs

by Oil_boi » Post

Here is a simple code that can be added into the lua code to return an absolute vector

Code: Select all

--this converts a vector into absolute value
function vector.abs(vector)
	vector.x = math.abs(vector.x)
	vector.y = math.abs(vector.y)
	vector.z = math.abs(vector.z)
	return(vector)
end
I can't find where the vector library is stored in the game engine so I'll just post this here and hope someone makes a github pull request to add it in ¯\_(ツ)_/¯
This account is no longer active

sfan5
Moderator
Posts: 4095
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5
Location: Germany

Re: vector.abs

by sfan5 » Post

Oil_boi wrote:I can't find where the vector library is stored in the game engine
Right here: https://github.com/minetest/minetest/bl ... vector.lua
Mods: Mesecons | WorldEdit | Nuke & Minetest builds for Windows (32-bit & 64-bit)

User avatar
Hume2
Member
Posts: 710
Joined: Tue Jun 19, 2018 08:24
GitHub: Hume2
In-game: Hume2
Location: Czech Republic

Re: vector.abs

by Hume2 » Post

sfan5 wrote:
Oil_boi wrote:I can't find where the vector library is stored in the game engine
Right here: https://github.com/minetest/minetest/bl ... vector.lua
I think, you confused that with the function vector.length. What OP says is not any kind of norm. It only makes all coordinates positive.
If you lack the reality, go on a trip or find a job.

Oil_boi
Member
Posts: 139
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

Re: vector.abs

by Oil_boi » Post

Hume2 wrote:
sfan5 wrote:
Oil_boi wrote:I can't find where the vector library is stored in the game engine
Right here: https://github.com/minetest/minetest/bl ... vector.lua
I think, you confused that with the function vector.length. What OP says is not any kind of norm. It only makes all coordinates positive.
This can be used for collision detection

Code: Select all

pos = vector.new(0,0,0)
pos2 = vector.new(0.5,0,0)
instead of checking like:

Code: Select all

diff = vector.subtract(pos,pos2)
if diff.x < 0.5 and diff.x > -0.5 and diff.z < 0.5 and diff.z > -0.5 do
you can check like

Code: Select all

diff = vector.abs(vector.subtract(pos,pos2))
if diff.x < 0.5 and diff.z < 0.5 then
I specifically utilize this function in these locations:
https://github.com/oilboi/Crafter/blob/ ... on.lua#L56
https://github.com/oilboi/Crafter/blob/ ... te.lua#L61
https://github.com/oilboi/Crafter/blob/ ... te.lua#L74

Also I did a pull request
https://github.com/minetest/minetest/pull/9551
Last edited by Oil_boi on Wed Mar 25, 2020 20:49, edited 1 time in total.
This account is no longer active

User avatar
Hume2
Member
Posts: 710
Joined: Tue Jun 19, 2018 08:24
GitHub: Hume2
In-game: Hume2
Location: Czech Republic

Re: vector.abs

by Hume2 » Post

This can be actually done even easier by using the maximum norm:

Code: Select all

function vector.maxnorm(v)
    return math.max(math.abs(v.x), math.max(math.abs(v.y), math.abs(v.z)))
end
Then it reduces to:

Code: Select all

norm = vector.maxnorm(vector.multiply(vector.subtract(pos, pos2), {x=2, y=0, z=2}))
if norm < 1 then
If you lack the reality, go on a trip or find a job.

Oil_boi
Member
Posts: 139
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

Re: vector.abs

by Oil_boi » Post

Hume2 wrote:This can be actually done even easier by using the maximum norm:

Code: Select all

function vector.maxnorm(v)
    return math.max(math.abs(v.x), math.max(math.abs(v.y), math.abs(v.z)))
end
Then it reduces to:

Code: Select all

norm = vector.maxnorm(vector.multiply(vector.subtract(pos, pos2), {x=2, y=0, z=2}))
if norm < 1 then
Yes that is very good, but it's not modular as you can't recover the actual value of Y as it was before maxnorming it, but that would be a nice function to have
This account is no longer active

User avatar
AspireMint
Member
Posts: 415
Joined: Mon Jul 09, 2012 12:59
GitHub: AspireMint
IRC: AspireMint
In-game: AspireMint
Location: Stuck at spawn

Re: vector.abs

by AspireMint » Post

what about vector.distance(p1, p2) < 0.5

User avatar
Hume2
Member
Posts: 710
Joined: Tue Jun 19, 2018 08:24
GitHub: Hume2
In-game: Hume2
Location: Czech Republic

Re: vector.abs

by Hume2 » Post

AspireMint wrote:what about vector.distance(p1, p2) < 0.5
This calculates the Eucleidan distance, not the maximum norm distance. Please, read the OP toughly to see what the vector.abs actually does.
If you lack the reality, go on a trip or find a job.

Oil_boi
Member
Posts: 139
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

Re: vector.abs

by Oil_boi » Post

I just decided to make a video on it: https://youtu.be/2GOsXSeNgQw
This account is no longer active

User avatar
GreenXenith
Member
Posts: 1356
Joined: Wed Oct 28, 2015 01:26
GitHub: GreenXenith
Location: UTC-8:00
Contact:

Re: vector.abs

by GreenXenith » Post

vector.apply(v, math.abs) but ok.
YouTube | Mods | Patreon | Minetest Discord @greenxenith

You should not be able to read this message.

Oil_boi
Member
Posts: 139
Joined: Mon Jan 28, 2019 13:12
GitHub: oilboi
IRC: oilboi
In-game: oilboi
Contact:

Re: vector.abs

by Oil_boi » Post

GreenDimond wrote:vector.apply(v, math.abs) but ok.
Well I see a lot of people saying this is redundant with the vector.apply function, but, as they say that, we also have:

Code: Select all

function vector.floor(v)
	return {
		x = math.floor(v.x),
		y = math.floor(v.y),
		z = math.floor(v.z)
	}
end

function vector.round(v)
	return {
		x = math.floor(v.x + 0.5),
		y = math.floor(v.y + 0.5),
		z = math.floor(v.z + 0.5)
	}
end
function vector.add(a, b)
	if type(b) == "table" then
		return {x = a.x + b.x,
			y = a.y + b.y,
			z = a.z + b.z}
	else
		return {x = a.x + b,
			y = a.y + b,
			z = a.z + b}
	end
end
Why do we have these? Because it's easier to type

Code: Select all

vector.round(v) 
rather than

Code: Select all

vector.apply(v, function(x) return(math.floor(x + 0.5)) end)

and also

Code: Select all

vector.add(a, b)
instead of

Code: Select all

local new_vector = {x=a.x+b.x,y=a.y+b.y,z=a.z+b.z}
It makes more logical sense for the library to contain functions to apply a basic bit of math to each vector coordinate rather than each programmer to define their own functions.

We could even boil it down even further using the logic that is applied in the responses to this. Why even have vector.new when we could run:

Code: Select all

v = {x=5,y=1,z=4}
Very simply, because it's much easier to type out:

Code: Select all

v = vector.new(5,1,4)
And along with the logic of what I've been trying to provide here, it is also easier to type and remember:

Code: Select all

vector.abs(v)
I dunno how to explain it any further, it's only 6 lines of codes. It doesn't cause any performance changes to the engine. It makes your life slightly easier. If someone doesn't want it, they could keep doing vector.apply(v, math.abs). I also thought everyone would like the small addition.

I dunno man ¯\_(ツ)_/¯
This account is no longer active

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests