Collision Sample
Collision is an MFC dialog based application. Source code for Collision sample is available in Samples\VC folder of
the installation directory. It is recommended to copy the folder to location
outside of the installation folder.
See also Metrics Sample, All C++ Samples,
All samples
Implementation
All DG Kernel related functionality is implemented inside the CollisionDlg
class. The DG Kernel related data is initialized inside InitModelInfo()
method, which uses GetModel
method of the DG Kernel control to obtain the root
interface of the hierarchy. ISection
interfaces, representing generic DG Kernel object, are obtained for the first
two top level objects of the model with the help of
IModel.GetEntity and stored in
m_iEntityFrom and m_iEntityTo member variables.
OnMoveObject() Move button handler sets up timer for animation and calculates
step vector for each animation step and stores it in m_dAnimationStep
variable. On every animation step CollisionDlg::OnTimer timer event handler
increments the current object position and calls
IMetrics.IsCloserThan to check
whether the collision situation occurred. If
IMetrics.IsCloserThan has retuned
true, the handler stops animation by calling CWnd::KillTimer() and posts a
user-defined notification message to itself to process collision situation.
The collision message is processed by CollisionDlg::OnCollision, which calls OnCalculate()
function identical to one from Metrics Sample. OnCalculate()
calculates exact distance between the objects at the current position and two
nearest points using IMetrics.Dist.
The calculated nearest points are displayed in 3D view
with red points. To improve performance of rendering
IRelation
obtained in OnCalculate()
is converted once to a pointer list and stored in m_pairList member.
This work is done by CompileNearestData method, where pairs of the relation
are iterated through, correspondent elements of the each per object list are
retrieved and converted to the actual type of IVertex.
The application uses ClientDraw
event, requested in OnInitDialog(), to add custom drawing of nearest
points to the scene. The drawing is performed inside
OnClientDraw handler. In the OnClientDraw method the coordinates of each
vertex are obtained via IVertex.GetVertexCoord.
|