Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear interpolation for vectors. #570

Merged
merged 3 commits into from
May 4, 2024

Conversation

Demigod916
Copy link
Contributor

@Demigod916 Demigod916 commented May 4, 2024

math.lerpVectors is a standard linear interpolation between 2 vectors. This will allow developers to smoothy transition between 2 vectors for rotations or coords over a set duration.

this function is currently blocking. optional async is something that we can add if desired.

example:

RegisterCommand('testLerp', function(source, args, raw)
	lib.requestModel(`gr_prop_gr_target_04b`)

	local offset = GetOffsetFromEntityInWorldCoords(cache.ped, 0, 3.0, 0)
	local object = CreateObject(`gr_prop_gr_target_04b`, offset.x, offset.y, offset.z, false, false, false)

	PlaceObjectOnGroundProperly_2(object)
	math.lerpVectors(vec3(0, 0, 0), vec3(-90.0, 0, 0), 500, function(interpolated)
		SetEntityRotation(object, interpolated.x, interpolated.y, interpolated.z, 0, false)
	end)
end)
lerp.-.Trim.mp4

@thelindat
Copy link
Member

An iterator might look a bit nicer in this situation.

for interpolated in math.lerpVectors(vec3(0, 0, 0), vec3(-90.0, 0, 0), 500) do
    SetEntityRotation(object, interpolated.x, interpolated.y, interpolated.z, 0, false)
end)

I would also prefer math.lerp that can handle interpolation of scalars, vectors, or even tables (both arrays and hashmaps, to a limit).

@Demigod916
Copy link
Contributor Author

so for the iterator method, it would use a coroutine?

@thelindat
Copy link
Member

You basically just return a function instead of having a function argument; it calls the function until it gets a nil value.

@Demigod916
Copy link
Contributor Author

Demigod916 commented May 4, 2024

okay updated.

for value in math.lerp(0, 100, 500) do
    print(value)
end

for vec in math.lerp(vec3(0, 0, 0), vec3(10, 10, 10), 1000) do
    print(vec.x, vec.y, vec.z)
end

for tbl in math.lerp({x=0, y=0}, {x=10, y=20}, 1000) do
    print(tbl.x, tbl.y)
end

Copy link
Member

@thelindat thelindat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind making these changes myself, but here's some nitpicks.

imports/math/shared.lua Outdated Show resolved Hide resolved
imports/math/shared.lua Outdated Show resolved Hide resolved
imports/math/shared.lua Outdated Show resolved Hide resolved
imports/math/shared.lua Outdated Show resolved Hide resolved
@thelindat thelindat merged commit 851df5b into overextended:master May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants