Defold: Add more common functions to math, vmath library

Created on 23 Aug 2020  路  6Comments  路  Source: defold/defold

Is your feature request related to a problem? Please describe (REQUIRED):
I'm always frustrated when i have to reprogram usual mathematic functions like

  • vector.magnitude
  • the nearest point on a finite or infinite line
  • mathf.clamp

Describe the solution you'd like (REQUIRED):
Implement those functions into defolds common libraries math and vmath.

Describe alternatives you've considered (REQUIRED):
You can program those yourself but its quite tiring after a while.

Additional context (OPTIONAL):
example implementations:

function module.vector3magnitude(x,y,z)
    return math.sqrt((x*x+y*y+z*z))
end

function module.clamp(x,y,z)
    return math.max(y, math.min(z,x))
end

function module.nearestPointOnVectorInfinite(start, finish, point)
    finish = vmath.normalize(finish)
    local v = point - start
    local d = vmath.dot(v, finish)
    return start + finish * d
end

function module.nearestPointOnVectorFinite(start, finish, point)
    line_direction = finish-start
    line_length = module.vector3magnitude(line_direction.x, line_direction.y, line_direction.z)
    line_direction = vmath.normalize(line_direction)
    project_length = module.clamp(vmath.dot(point-start, line_direction),0,line_length)
    return start + line_direction * project_length
end
engine feature request good first issue

Most helpful comment

Oh, but hold on, the magnitude of a vector is the length right? vmath.length(v3|v4|q) is the function you are looking for.

All 6 comments

Thank you for the suggestions! There's probably a few core functions which are missing from the API which we provide. A function to clamp a value is probably one. Maybe also vector magnitude. But I'm not at all certain about things such as the nearest point on a line.

This is where Lua module comes in and help you reuse this kind of functionality and perhaps also share it with others in the asset portal? https://defold.com/tags/stars/math/

Oh, but hold on, the magnitude of a vector is the length right? vmath.length(v3|v4|q) is the function you are looking for.

@britzl You're absolutely right. I must have been looking for magnitude because its the more common term in vector mathematics.

I'd be happy to see the clamp function in the math lib.

The Nearest Point on Line functions are quite specific, i admit.
But i dont think it is enough to provide a whole asset for it.
If anybody needs the code though it is here. 馃憤

Thanks Bj枚rn.

https://github.com/subsoap/defmath/blob/master/defmath/defmath.lua You are welcome to add more specific math functions to this project, there are already a few.

I'm moving this from any planned release to the backlog. This is a "good first issue" for a newcomer. For anyone picking this up:

  • Start by creating a design document with proposed new function
  • Share here and possibly also on the forum

@britzl It would be good to post on the forum about the "good first issue" tag and highlight some of them for people to consider helping with.

Was this page helpful?
0 / 5 - 0 ratings