add vec3.random_normal and fix entities cleanup moment

This commit is contained in:
MihailRis 2024-07-10 00:39:41 +03:00
parent d8c9fa1fe2
commit 257ba86183
4 changed files with 39 additions and 11 deletions

View File

@ -33,7 +33,7 @@ vecn.add(a: vector, b: vector)
vecn.add(a: vector, b: number)
-- записывает результат сложения двух векторов в dst
vec.add(a: vector, b: vector, dst: vector)
vecn.add(a: vector, b: vector, dst: vector)
```
#### Вычитание - *vecn.sub(...)*
@ -46,7 +46,7 @@ vecn.sub(a: vector, b: vector)
vecn.sub(a: vector, b: number)
-- записывает результат вычитания двух векторов в dst
vec.sub(a: vector, b: vector, dst: vector)
vecn.sub(a: vector, b: vector, dst: vector)
```
#### Умножение - *vecn.mul(...)*
@ -66,7 +66,7 @@ vecn.mul(a: vector, b: number)
vecn.inverse(a: vector)
-- записывает инвертированный вектор в dst
vec.inverse(v: vector, dst: vector)
vecn.inverse(v: vector, dst: vector)
```
#### Деление - *vecn.div(...)*
@ -79,7 +79,7 @@ vecn.div(a: vector, b: vector)
vecn.div(a: vector, b: number)
-- записывает результат деления двух векторов в dst
vec.div(a: vector, b: vector, dst: vector)
vecn.div(a: vector, b: vector, dst: vector)
```
#### Нормализация - *vecn.norm(...)*
@ -89,7 +89,7 @@ vec.div(a: vector, b: vector, dst: vector)
vecn.normalize(a: vector)
-- записывает нормализованный вектор в dst
vec.normalize(v: vector, dst: vector)
vecn.normalize(v: vector, dst: vector)
```
#### Длина вектора - *vecn.len(...)*
@ -107,7 +107,7 @@ vecn.length(a: vector)
vecn.abs(a: vector)
-- записывает абсолютное значение вектора в dst
vec.abs(v: vector, dst: vector)
vecn.abs(v: vector, dst: vector)
```
#### Округление - *vecn.round(...)*
@ -117,7 +117,7 @@ vec.abs(v: vector, dst: vector)
vecn.round(a: vector)
-- записывает округленный вектор в dst
vec.round(v: vector, dst: vector)
vecn.round(v: vector, dst: vector)
```
#### Степень - *vecn.pow(...)*
@ -127,7 +127,7 @@ vec.round(v: vector, dst: vector)
vecn.pow(a: vector, b: number)
-- записывает вектор, возведенный в степень, в dst
vec.pow(v: vector, exponent: number, dst: vector)
vecn.pow(v: vector, exponent: number, dst: vector)
```
#### Скалярное произведение - *vecn.dot(...)*
@ -144,6 +144,18 @@ vecn.dot(a: vector, b: vector)
vecn.tostring(a: vector)
```
## Специфические функции
Функции относящиеся к конкретным размерностям векторов.
```lua
-- возвращает случайный вектор, координаты которого равномерно распределены на сфере заданного радиуса
vec3.spherical_rand(radius: number)
-- записывает случайный вектор, координаты которого равномерно распределены на сфере заданного радиуса в dst
vec3.spherical_rand(radius: number, dst: vec3)
```
## Пример
```lua

View File

@ -26,7 +26,8 @@ function on_grounded()
block.set(ix, iy, iz, block.index(blockid))
else
local picking_item = block.get_picking_item(block.index(blockid))
entities.spawn("base:drop", pos, {item={id=picking_item, count=1}})
local drop = entities.spawn("base:drop", pos, {item={id=picking_item, count=1}})
drop.rigidbody:set_vel(vec3.spherical_rand(5.0))
end
entity:despawn()
end

View File

@ -31,7 +31,6 @@ void LevelController::update(float delta, bool input, bool pause) {
settings.chunks.padding.get() * 2);
chunks->update(settings.chunks.loadSpeed.get());
level->entities->clean();
if (!pause) {
// update all objects that needed
for (const auto& obj : level->objects) {
@ -44,6 +43,7 @@ void LevelController::update(float delta, bool input, bool pause) {
level->entities->updatePhysics(delta);
level->entities->update();
}
level->entities->clean();
player->postUpdate(delta, input, pause);
// erease null pointers

View File

@ -2,6 +2,7 @@
#include <sstream>
#include <glm/glm.hpp>
#include <glm/gtc/random.hpp>
template<int n, template<class> class Op>
static int l_binop(lua::State* L) {
@ -135,6 +136,19 @@ static int l_inverse(lua::State* L) {
}
}
static int l_spherical_rand(lua::State* L) {
int argc = lua::gettop(L);
switch (argc) {
case 1:
return lua::pushvec3_arr(L, glm::sphericalRand(lua::tonumber(L, 1)));
case 2:
return lua::setvec(L, 2,
glm::sphericalRand(static_cast<float>(lua::tonumber(L, 1))));
default:
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
}
}
template<int n>
static int l_tostring(lua::State* L) {
auto vec = lua::tovec<n>(L, 1);
@ -182,6 +196,7 @@ const luaL_Reg vec3lib [] = {
{"inverse", lua::wrap<l_inverse<3>>},
{"pow", lua::wrap<l_pow<3>>},
{"dot", lua::wrap<l_dot<3>>},
{"spherical_rand", lua::wrap<l_spherical_rand>},
{NULL, NULL}
};
@ -199,4 +214,4 @@ const luaL_Reg vec4lib [] = {
{"pow", lua::wrap<l_pow<4>>},
{"dot", lua::wrap<l_dot<4>>},
{NULL, NULL}
};
};