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
```

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

Here is a simple code that can be added into the lua code to return an absolute vector
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 ¯\_(ツ)_/¯

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
```

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

Right here: https://github.com/minetest/minetest/bl ... vector.luaOil_boi wrote:I can't find where the vector library is stored in the game engine

- Hume2
- Member
**Posts:**631**Joined:**Tue Jun 19, 2018 08:24**GitHub:**Hume2**In-game:**Hume2**Location:**Czech Republic

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.sfan5 wrote:Right here: https://github.com/minetest/minetest/bl ... vector.luaOil_boi wrote:I can't find where the vector library is stored in the game engine

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:**

This can be used for collision detectionHume2 wrote: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.sfan5 wrote:Right here: https://github.com/minetest/minetest/bl ... vector.luaOil_boi wrote:I can't find where the vector library is stored in the game engine

Code: Select all

```
pos = vector.new(0,0,0)
pos2 = vector.new(0.5,0,0)
```

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
```

Code: Select all

```
diff = vector.abs(vector.subtract(pos,pos2))
if diff.x < 0.5 and diff.z < 0.5 then
```

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.

- Hume2
- Member
**Posts:**631**Joined:**Tue Jun 19, 2018 08:24**GitHub:**Hume2**In-game:**Hume2**Location:**Czech Republic

This can be actually done even easier by using the maximum norm:
Then it reduces to:

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
```

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:**

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 haveHume2 wrote:This can be actually done even easier by using the maximum norm:Then it reduces to: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`

Code: Select all

`norm = vector.maxnorm(vector.multiply(vector.subtract(pos, pos2), {x=2, y=0, z=2})) if norm < 1 then`

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

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

- Hume2
- Member
**Posts:**631**Joined:**Tue Jun 19, 2018 08:24**GitHub:**Hume2**In-game:**Hume2**Location:**Czech Republic

This calculates the Eucleidan distance, not the maximum norm distance. Please, read the OP toughly to see what the vector.abs actually does.AspireMint wrote:what about vector.distance(p1, p2) < 0.5

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:**

I just decided to make a video on it: https://youtu.be/2GOsXSeNgQw

- GreenXenith
- Member
**Posts:**1335**Joined:**Wed Oct 28, 2015 01:26**GitHub:**GreenXenith**IRC:**GreenXenith**In-game:**GreenXenith**Location:**The singularity-
**Contact:**

vector.apply(v, math.abs) but ok.

Hey, you. You're finally awake.

You were trying to view their profile, right? Found the rest of their signature, same as us, and that guest over there.

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

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

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
```

Code: Select all

`vector.round(v) `

Code: Select all

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

and also

Code: Select all

`vector.add(a, b)`

Code: Select all

`local new_vector = {x=a.x+b.x,y=a.y+b.y,z=a.z+b.z}`

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}`

Code: Select all

`v = vector.new(5,1,4)`

Code: Select all

`vector.abs(v)`

I dunno man ¯\_(ツ)_/¯

Users browsing this forum: No registered users and 1 guest