add new model and "optimizations"

This commit is contained in:
MihailRis 2024-06-30 18:24:30 +03:00
parent 1ec5f0b170
commit df39a9a8f3
7 changed files with 125 additions and 30 deletions

View File

@ -1,3 +1,3 @@
{ {
"hitbox": [0.2, 0.15, 0.2] "hitbox": [0.2, 0.2, 0.2]
} }

View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

View File

@ -0,0 +1,62 @@
# Blender v2.79 (sub 0) OBJ File: ''
# www.blender.org
mtllib item.mtl
o Cube
v -0.282501 -0.015663 -0.282501
v -0.282501 -0.015663 0.282500
v 0.282500 -0.015663 0.282501
v 0.282501 -0.015663 -0.282500
v 0.282501 0.021634 -0.282500
v 0.282500 0.021634 0.282501
v -0.282501 0.021634 0.282500
v -0.282501 0.021634 -0.282501
v -0.282501 0.021634 -0.282501
v -0.282501 0.021634 0.282500
v 0.282500 0.021634 0.282501
v 0.282501 0.021634 -0.282500
v 0.282501 -0.015663 -0.282500
v 0.282500 -0.015663 0.282501
v -0.282501 -0.015663 0.282500
v -0.282501 -0.015663 -0.282501
v 0.282501 0.002840 -0.282500
v 0.282500 0.002840 0.282501
v -0.282501 0.002840 0.282500
v -0.282501 0.002840 -0.282501
v -0.282501 0.002840 -0.282501
v -0.282501 0.002840 0.282500
v 0.282500 0.002840 0.282501
v 0.282501 0.002840 -0.282500
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 0.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 0.000000
vn 0.0000 1.0000 0.0000
vn 0.0000 -1.0000 0.0000
usemtl items/bazalt_breaker
s off
f 4/1/1 1/2/1 2/3/1 3/4/1
f 5/5/1 8/6/1 7/7/1 6/8/1
f 12/9/2 11/10/2 10/11/2 9/12/2
f 13/13/2 14/14/2 15/15/2 16/16/2
f 17/17/1 20/18/1 19/19/1 18/20/1
f 24/21/2 23/22/2 22/23/2 21/24/2

View File

@ -4,6 +4,7 @@
"blocks/door_close" "blocks/door_close"
], ],
"models": [ "models": [
"cube" "cube",
"item"
] ]
} }

View File

@ -22,7 +22,7 @@ end
function on_update() function on_update()
if inair then if inair then
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, math.random()*12)) tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, math.random()*4))
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, math.random()*12)) tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, math.random()*4))
end end
end end

View File

@ -78,15 +78,20 @@ void Entities::clean() {
} }
} }
void Entities::updatePhysics(float delta){ void Entities::preparePhysics() {
static uint64_t frameid = 0;
frameid++;
auto view = registry.view<EntityId, Transform, Rigidbody>(); auto view = registry.view<EntityId, Transform, Rigidbody>();
auto physics = level->physics.get(); auto physics = level->physics.get();
{
std::vector<Trigger*> triggers; std::vector<Trigger*> triggers;
for (auto [entity, eid, transform, rigidbody] : view.each()) { for (auto [entity, eid, transform, rigidbody] : view.each()) {
if (!rigidbody.enabled) { if (!rigidbody.enabled) {
continue; continue;
} }
// TODO: temporary optimization until threaded solution
if ((eid.uid + frameid) % 3 != 0) {
continue;
}
for (size_t i = 0; i < rigidbody.triggers.size(); i++) { for (size_t i = 0; i < rigidbody.triggers.size(); i++) {
auto& trigger = rigidbody.triggers[i]; auto& trigger = rigidbody.triggers[i];
for (auto oid : trigger.prevEntered) { for (auto oid : trigger.prevEntered) {
@ -102,7 +107,13 @@ void Entities::updatePhysics(float delta){
} }
} }
physics->setTriggers(std::move(triggers)); physics->setTriggers(std::move(triggers));
} }
void Entities::updatePhysics(float delta) {
preparePhysics();
auto view = registry.view<EntityId, Transform, Rigidbody>();
auto physics = level->physics.get();
for (auto [entity, eid, transform, rigidbody] : view.each()) { for (auto [entity, eid, transform, rigidbody] : view.each()) {
if (!rigidbody.enabled) { if (!rigidbody.enabled) {
continue; continue;
@ -110,11 +121,15 @@ void Entities::updatePhysics(float delta){
auto& hitbox = rigidbody.hitbox; auto& hitbox = rigidbody.hitbox;
auto prevVel = hitbox.velocity; auto prevVel = hitbox.velocity;
bool grounded = hitbox.grounded; bool grounded = hitbox.grounded;
float vel = glm::length(prevVel);
int substeps = static_cast<int>(delta * vel * 20);
substeps = std::min(100, std::max(2, substeps));
physics->step( physics->step(
level->chunks.get(), level->chunks.get(),
&hitbox, &hitbox,
delta, delta,
10, substeps,
false, false,
1.0f, 1.0f,
true, true,
@ -139,7 +154,7 @@ void Entities::update() {
} }
} }
void Entities::renderDebug(LineBatch& batch, Frustum& frustum) { void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) {
batch.lineWidth(1.0f); batch.lineWidth(1.0f);
auto view = registry.view<Transform, Rigidbody>(); auto view = registry.view<Transform, Rigidbody>();
for (auto [entity, transform, rigidbody] : view.each()) { for (auto [entity, transform, rigidbody] : view.each()) {
@ -157,9 +172,12 @@ void Entities::renderDebug(LineBatch& batch, Frustum& frustum) {
} }
} }
void Entities::render(Assets* assets, ModelBatch& batch, Frustum& frustum) { void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum) {
auto view = registry.view<Transform>(); auto view = registry.view<Transform>();
auto model = assets->get<model::Model>("cube"); auto model = assets->get<model::Model>("cube");
if (model == nullptr) {
return;
}
for (auto [entity, transform] : view.each()) { for (auto [entity, transform] : view.each()) {
const auto& pos = transform.pos; const auto& pos = transform.pos;
const auto& size = transform.size; const auto& size = transform.size;

View File

@ -104,6 +104,8 @@ class Entities {
Level* level; Level* level;
std::unordered_map<entityid_t, entt::entity> entities; std::unordered_map<entityid_t, entt::entity> entities;
entityid_t nextID = 1; entityid_t nextID = 1;
void preparePhysics();
public: public:
Entities(Level* level); Entities(Level* level);
@ -111,8 +113,8 @@ public:
void updatePhysics(float delta); void updatePhysics(float delta);
void update(); void update();
void renderDebug(LineBatch& batch, Frustum& frustum); void renderDebug(LineBatch& batch, const Frustum& frustum);
void render(Assets* assets, ModelBatch& batch, Frustum& frustum); void render(Assets* assets, ModelBatch& batch, const Frustum& frustum);
entityid_t spawn(EntityDef& def, glm::vec3 pos); entityid_t spawn(EntityDef& def, glm::vec3 pos);