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

Investigating physics precision loss with increasing distance from world origin. #3034

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions source/main/gfx/GfxActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,15 @@ void RoR::GfxActor::UpdateDebugView()
}
}

// Nodes are drawn directly using RelPosition, so that the debugview is accurate even in great distances.
// To do that, we need a custom modelview matrix.
Ogre::Affine3 nodes_modelmatrix(m_actor->ar_origin, Ogre::Quaternion::IDENTITY);
World2ScreenConverter nodes2screen(
App::GetCameraManager()->GetCamera()->getViewMatrix(true) * nodes_modelmatrix,
App::GetCameraManager()->GetCamera()->getProjectionMatrix(),
Ogre::Vector2(screen_size.x, screen_size.y)
);

// Skeleton display. NOTE: Order matters, it determines Z-ordering on render
if ((m_debug_view == DebugViewType::DEBUGVIEW_SKELETON) ||
(m_debug_view == DebugViewType::DEBUGVIEW_NODES) ||
Expand All @@ -622,8 +631,8 @@ void RoR::GfxActor::UpdateDebugView()
beams[i].p2->nd_tyre_node || beams[i].p2->nd_rim_node))
continue;

Ogre::Vector3 pos1 = world2screen.Convert(beams[i].p1->AbsPosition);
Ogre::Vector3 pos2 = world2screen.Convert(beams[i].p2->AbsPosition);
Ogre::Vector3 pos1 = nodes2screen.Convert(beams[i].p1->RelPosition);
Ogre::Vector3 pos2 = nodes2screen.Convert(beams[i].p2->RelPosition);

if ((pos1.z < 0.f) && (pos2.z < 0.f))
{
Expand Down Expand Up @@ -677,7 +686,7 @@ void RoR::GfxActor::UpdateDebugView()
if (App::diag_hide_wheels->getBool() && (nodes[i].nd_tyre_node || nodes[i].nd_rim_node))
continue;

Ogre::Vector3 pos_xyz = world2screen.Convert(nodes[i].AbsPosition);
Ogre::Vector3 pos_xyz = nodes2screen.Convert(nodes[i].RelPosition);

if (pos_xyz.z < 0.f)
{
Expand All @@ -702,7 +711,7 @@ void RoR::GfxActor::UpdateDebugView()
(nodes[i].nd_tyre_node || nodes[i].nd_rim_node))
continue;

Ogre::Vector3 pos = world2screen.Convert(nodes[i].AbsPosition);
Ogre::Vector3 pos = nodes2screen.Convert(nodes[i].RelPosition);

if (pos.z < 0.f)
{
Expand Down Expand Up @@ -734,8 +743,8 @@ void RoR::GfxActor::UpdateDebugView()
continue;

// Position
Ogre::Vector3 world_pos = (beams[i].p1->AbsPosition + beams[i].p2->AbsPosition) / 2.f;
Ogre::Vector3 pos_xyz = world2screen.Convert(world_pos);
Ogre::Vector3 rel_pos = (beams[i].p1->RelPosition + beams[i].p2->RelPosition) / 2.f;
Ogre::Vector3 pos_xyz = nodes2screen.Convert(rel_pos);
if (pos_xyz.z >= 0.f)
{
continue; // Behind the camera
Expand Down Expand Up @@ -1806,7 +1815,8 @@ void RoR::GfxActor::UpdateCabMesh()
{
if ((m_cab_entity != nullptr) && (m_cab_mesh != nullptr))
{
m_cab_scene_node->setPosition(m_cab_mesh->UpdateFlexObj());
m_cab_scene_node->setPosition(m_simbuf.simbuf_origin);
m_cab_mesh->UpdateFlexObj();
}
}

Expand Down
18 changes: 7 additions & 11 deletions source/main/physics/flex/FlexObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,22 @@ int FlexObj::ComputeVertexPos(int tidx, int v, std::vector<CabSubmesh>& submeshe
return 0;
}

Vector3 FlexObj::UpdateMesh()
void FlexObj::UpdateMesh()
{
RoR::NodeSB* all_nodes = m_gfx_actor->GetSimNodeBuffer();
Ogre::Vector3 center=(all_nodes[m_vertex_nodes[0]].AbsPosition+all_nodes[m_vertex_nodes[1]].AbsPosition)/2.0;
for (size_t i=0; i<m_vertex_count; i++)
{
//set position
m_vertices[i].position=all_nodes[m_vertex_nodes[i]].AbsPosition-center;
//set position (the scene node is at physics origin, so use node relative position)
m_vertices[i].position=all_nodes[m_vertex_nodes[i]].RelPosition;
//reset normals
m_vertices[i].normal=Vector3::ZERO;
}
//accumulate normals per triangle
for (size_t i=0; i<m_index_count/3; i++)
{
Vector3 v1, v2;
v1=all_nodes[m_vertex_nodes[m_indices[i*3+1]]].AbsPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].AbsPosition;
v2=all_nodes[m_vertex_nodes[m_indices[i*3+2]]].AbsPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].AbsPosition;
v1=all_nodes[m_vertex_nodes[m_indices[i*3+1]]].RelPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].RelPosition;
v2=all_nodes[m_vertex_nodes[m_indices[i*3+2]]].RelPosition-all_nodes[m_vertex_nodes[m_indices[i*3]]].RelPosition;
v1=v1.crossProduct(v2);
float s=v1.length();

Expand All @@ -223,15 +222,12 @@ Vector3 FlexObj::UpdateMesh()
{
m_vertices[i].normal = approx_normalise(m_vertices[i].normal);
}

return center;
}

Vector3 FlexObj::UpdateFlexObj()
void FlexObj::UpdateFlexObj()
{
Ogre::Vector3 center = this->UpdateMesh();
this->UpdateMesh();
m_hw_vbuf->writeData(0, m_hw_vbuf->getSizeInBytes(), m_vertices_raw, true);
return center;
}

FlexObj::~FlexObj()
Expand Down
4 changes: 2 additions & 2 deletions source/main/physics/flex/FlexObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FlexObj

~FlexObj();

Ogre::Vector3 UpdateFlexObj();
void UpdateFlexObj();
void ScaleFlexObj(float factor);

private:
Expand All @@ -88,7 +88,7 @@ class FlexObj

/// Compute vertex position in the vertexbuffer (0-based offset) for node `v` of triangle `tidx`
int ComputeVertexPos(int tidx, int v, std::vector<CabSubmesh>& submeshes);
Ogre::Vector3 UpdateMesh();
void UpdateMesh();

Ogre::MeshPtr m_mesh;
std::vector<Ogre::SubMesh*> m_submeshes;
Expand Down