Skip to content

Commit

Permalink
UPBGE: Fix bounding box of duplicated game objects.
Browse files Browse the repository at this point in the history
Previously three issues were noticed:
Firstly the game object bounding box wasn't updated for an added object,
only the original bounding box was copied, this original bounding box
could be fake if the original object doesn't have any replications which
update the bounding box.
To solve this issue a call to KX_GameObject::UpdateBounds is added in
AddNodeReplica. This call must be after the scene graph node and the
graphic controller was set.

The second issue encountered when appliying the first fix was that the
original bounding box from the original game object was copied after
AddNodeReplica call. This copy was proceed in multiple places and destroyed
the just updated bounding box. To avoid this effect the box copy is set
into AddNodeReplica function.

Finally the bounding box was wrongly update because the "force" argument
passed to KX_GameObject::UpdateBound was used to hijack auto update test
but not the test checking if the bounding box is not modified.
A condition checking "force" is added.

Fix issue #385.
  • Loading branch information
panzergame committed Feb 4, 2017
1 parent e9b8f9e commit cc1f990
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ void KX_GameObject::UpdateBounds(bool force)
}

RAS_BoundingBox *boundingBox = m_meshUser->GetBoundingBox();
if (!boundingBox || !boundingBox->GetModified()) {
if (!boundingBox || (!boundingBox->GetModified() && !force)) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion source/gameengine/Ketsji/KX_GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,9 @@ class KX_GameObject : public SCA_IObject
/** Update the game object bounding box (AABB) by using the one existing in the
* mesh or the mesh deformer.
* \param force Force the AABB update even if the object doesn't allow auto update or if the mesh is
* not modified like in the case of mesh replacement.
* not modified like in the case of mesh replacement or object duplication.
* \warning Should be called when the game object contains a valid scene graph node
* and a valid graphic controller (if it exists).
*/
void UpdateBounds(bool force);
void SetBoundsAabb(MT_Vector3 aabbMin, MT_Vector3 aabbMax);
Expand Down
7 changes: 5 additions & 2 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ KX_GameObject* KX_Scene::AddNodeReplicaObject(class SG_Node* node, class CValue*
m_rootnode->SetLocalScale(orgnode->GetLocalScale());
m_rootnode->SetLocalPosition(orgnode->GetLocalPosition());
m_rootnode->SetLocalOrientation(orgnode->GetLocalOrientation());
m_rootnode->SetBBox(orgnode->BBox());

// define the relationship between this node and it's parent.
KX_NormalParentRelation * parent_relation =
Expand Down Expand Up @@ -594,6 +595,9 @@ KX_GameObject* KX_Scene::AddNodeReplicaObject(class SG_Node* node, class CValue*
newctrl->SuspendDynamics();
}

// Always make sure that the bounding box is valid.
newobj->UpdateBounds(true);

return newobj;
}

Expand Down Expand Up @@ -807,7 +811,6 @@ void KX_Scene::DupliGroupRecurse(CValue* obj, int level)
replica->NodeSetLocalOrientation(newori);
// update scenegraph for entire tree of children
replica->GetSGNode()->UpdateWorldData(0);
replica->GetSGNode()->SetBBox(gameobj->GetSGNode()->BBox());
// we can now add the graphic controller to the physic engine
replica->ActivateGraphicController(true);

Expand Down Expand Up @@ -930,7 +933,6 @@ SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
}

replica->GetSGNode()->UpdateWorldData(0);
replica->GetSGNode()->SetBBox(originalobj->GetSGNode()->BBox());
// the size is correct, we can add the graphic controller to the physic engine
replica->ActivateGraphicController(true);

Expand Down Expand Up @@ -1302,6 +1304,7 @@ void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj, bool use_gfx, bool u
if (gameobj->GetPhysicsController())
gameobj->GetPhysicsController()->ReinstancePhysicsShape(NULL, use_gfx?NULL:mesh);
}
// Always make sure that the bounding box is updated to the new mesh.
gameobj->UpdateBounds(true);
}

Expand Down

1 comment on commit cc1f990

@youle31
Copy link
Collaborator

@youle31 youle31 commented on cc1f990 Feb 5, 2017

Choose a reason for hiding this comment

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

👍

Please sign in to comment.