Collision C# Sample
Source code for Collision sample is available in Samples\NET\C# folder of
the installation directory. It is recommended to copy the folder to location
outside of the installation folder.
See also Collision detection,
Metrics Sample,
All samples
Implementation
All DG Kernel related functionality is implemented inside the CollisionForm
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 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 with a line animationTimer.Enabled = False and
calls CalculateDist Sub to process collision situation.
Inside the CalculateDist Sub, identical to one from
Metrics Sample, exact distance between the objects and two
nearest points are calculated using IMetrics.Dist. The calculated nearest points are displayed in 3D view
with red points. To improve performance of rendering
IRelation
obtained in CalculateDist is converted once to a 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 OnFormLoad(), 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.
|