Graphics Reference
In-Depth Information
void
CModel::UpdateSimulation() {
Source file.
Model.cpp
file in the
Model
folder of the
D3D
_
BallShoot
project.
// compute elapsed second since previous update
float
elapsed
_
seconds = ...
//check for collisions between all of the existing balls
foreach
test
_
ball
in m
_
AllBalls
foreach
other
_
ball
in m
_
AllBalls
if
(test
_
ball.Intersects(other
_
ball))
// if the two balls intersect
// computer new velocities for both
// detail description can be found in
Section B.1
// not shown code identical to Listing
5.24
(Tutorial
5.5
)
}
Listing 5.25.
CModel::UpdateSimulation()
function (Tutorial 5.6).
Figure 5.11 is a screenshot from interacting with the ball-shooting program im-
plemented in Tutorial 5.6. When comparing to Tutorial 5.5 we notice two major
differences: interball collision and slider-bar control of ball velocity.
Figure 5.11.
Tutorial
5.6.
Interball Collision
In order to compute interball collision, we must collide each ball with every other
ball in the world. Listing 5.25 shows the nested for loops required to compute in-
tersections between every ball in the world. Notice that this is an
O
n
2
operation,
where
n
is the number of balls in the
m
_
AllBalls
array. As the number of balls
increases in the world, this collision routine would quickly cause sub-real-time
responses. In this case, because the user must click and drag to create a ball and
because balls free fall out of the application window rapidly, the total number of
balls in the
m
_
AllBalls
array does not grow very large (e.g., less than 100). For
these reasons, we can still experience real-time response.
(
)
Slider-Bar Controls
As discussed in Tutorial 2.3, if registered, MFC will call the
OnHScroll()
func-
tion for servicing of the horizontal scroll bar events. Listing 5.26 shows that the
servicing of the scroll-bar events involves setting the velocity of the currently se-
lected ball. In this case, the user changes the selected ball velocity in between
timer events, and during the servicing of timer event the
UpdateSimulation()
function will move the selected ball by its new velocity. Notice that the
Update
Simulation()
and
OnHScroll()
functions will
never
execute concurrently.
Search WWH ::
Custom Search