combining kinematics with collision events - Excel...
Transcript of combining kinematics with collision events - Excel...
<www.excelunusual.com> 1
Excel PONG Tutorial #4– combining kinematics with collision events
by George Lungu
- In this tutorial (which is a continuation of part#3) the kinematics of
the ball is further analyzed.
- three different of collision effects are introduced. These effects are
the wall collision effect, the bat collision effect and the bat miss effect
- These effects play an important role in the play, scoring and sound
effects involved in the game
<www.excelunusual.com> 2
25. Create a new copy of the worksheet and macros
- Copy the last worksheet into a new one. Rename the new worksheet
“Pong_Tutorial_4”.
- Insert a new module, Module3, using the VBA editor
- Copy both macros from Module2 into the new module (including the
RunPause variable declaration)
- Change the name of the macros:
“Serve_3” => “Serve_4”
“Play_Tutorial_3” => “Play_Tutorial_4”
Dim RunPause As Boolean
----------------------------------------------------------------
Sub Serve_4()
Range("R28:Y30").Clear
Range("R28:U28") = Range("R23:U23").Value
End Sub
----------------------------------------------------------------
Sub Play_Tutorial_4()
RunPause = Not RunPause
Dim Pt0 As POINTAPI
Dim Pt1 As POINTAPI
GetCursorPos Pt0
Do While RunPause = True
DoEvents
GetCursorPos Pt1
[S6] = [P8] * (-Pt1.Y + Pt0.Y)
On Error Resume Next
Range("R28:Y29") = Range("R27:Y28").Value
Loop
End Sub
26. Inserting the “collision event table”
- In the range R15:T19, create a table with logical
collision information formulas
- We need this information later in the Kinematics
formulas to see when and how we need to take
trajectory change decision
- In later models there will be various sounds
triggered by the collisions
- Populating this table with formulas will be
explained in the following pages
- Reassign the new macros to the buttons in the new worksheet
<www.excelunusual.com> 3
27. Horizontal (upper or lower) wall collision formulas
- The ball movement is created by changing the position of the ball in time. Therefore the ball trajectory is a
string of dots.
- A natural way to detect collisions is finding “wall straddles” of the ball trajectory, which means looking for two
consecutive points along the ball trajectory when the ball is on one side of the wall at a certain time and on the
other side of the wall in the next time step.
- When this condition occurs we will use it to revert the sign of the perpendicular (to the wall) speed component
while leaving the speed component parallel to the wall unchanged.
incidentv
iparallelv _
eparalleliparallel
elarperpendicuilarperpendicu
vv
vv
__
__
emergentv
eparallelv _
elarperpendicuv _
ilarperpendicuv _
t’n
tn-1tn
tn-2 tn+1
Dn-1
Dncollision
collision
If (Dn-1* Dn) < 0 a collision is
detected. The sign of the
perpendicular speed component
needs to change in order to turn
the ball away from the wall (red)
<www.excelunusual.com> 4
The above formula works well because the system of coordinate is placed exactly in the center of symmetry of
the court (both x and y). We also assumed that the x-axis of this system of coordinates is parallel to the length
of the court and the y-axis is parallel to the width of the court. Translated in spreadsheet formulas the above
expression becomes:
Let’ see how we write the collision detection formula taking into consideration the width and length of the court and
the present (x0,y0) and past (x-1,y-1) coordinates of the ball (BR stands for ball radius):
TRUEBRwidth
yandBRwidth
yandlength
xlength
2222100
The ball must be
within the court
horizontal boundaries
The absolute value of the
current y-coordinate of the ball
exceeds half of the width of
the court minus the ball radius
The absolute value of the past
y-coordinate of the ball is less
than half of the width of the
court minus the ball radius
T15: “=AND(R27>-V9/2,R27<V9/2,ABS(S27)>V10/2-V11,ABS(S28)<=V10/2-V11)”
28. Ball-bat collision formulas
- A similar logic applies to the ball-bat collision formulas
- In this case however, there are two different event detections:
1. The ball is hit and it must bounce back into the court reversing the sign of the horizontal speed
component (the “collision with bat” event)
2. The ball is not hit and it must continue its flight - one party wins a point (the “missed bat” event)
<www.excelunusual.com> 5
The ball must be within the vertical
boundaries of the bat (with an added 10%
bonus – without that it would just be
Bat_size/2. By playing it I found out that 10%
over-extension gives the player a better feel
for the game)
The current x-coordinate of the ball
exceeds the coordinate of Bat#1 (in
the negative direction) plus the ball
radius
The past x-coordinate of the
ball is within the coordinate
of Bat#1 plus the ball radius
How do we write the ball-bat collision detection formula for Bat #1 taking into consideration the length of the court,
the distance of the bat from the x-end of the court (Players_in), the ball radius (shorthand BR) the present (x0,y0)
and past (x-1,y-1) coordinates of the ball?
TRUEBRPlayers_inlength
xandBRPlayers_inlength
xandsizeBat
yysizeBat
y batbat
228.1
_
8.1
_101_01_
TRUEBRPlayers_inlength
xandBRPlayers_inlength
xandsizeBat
yysizeBat
y batbat
228.1
_
8.1
_102_02_
How do we write the ball-bat collision detection formula for Bat #2 (which is located to the right of the court)?
Translated in spreadsheet formulas the above expressions
become:
T16: “=AND(S5-P12/1.8<=S27,S27<=S5+P12/1.8,R27<-
V9/2+V8+V11,R28>=-V9/2+V8+V11))”
T18: “=AND(S6-P12/1.8<=S27,S27<=S6+P12/1.8,R27>V9/2-
V8-V11,R28<=V9/2-V8-V11)”
Pla
ye
rs_
in
Pla
ye
rs_
in
Wid
thLength
ybat_2
y0
x0(0,0)
Ba
t_1
Bat_
2
<www.excelunusual.com> 6
29. The missed-bat event formulas– This event is important since it is a score changing event, moreover when the bat misses the ball there will be a
special sound triggered by the Play macro
- How do we write the missed-bat event detection formula for Bat #1 taking into consideration the length of the
court, the distance of the bat from the x-end of the court (Players_in), the ball radius (shorthand BR) the present
(x0,y0) and past (x-1,y-1) coordinates of the ball? It is very similar to the ball-bat collision event.
The ball must be out of the vertical
boundaries of the bat (with an added
10% bonus)
TRUEBRPlayers_inlength
xandBRPlayers_inlength
xandsizeBat
yysizeBat
ynot batbat
228.1
_
8.1
_101_01_
TRUEBRPlayers_inlength
xandBRPlayers_inlength
xandsizeBat
yysizeBat
ynot batbat
228.1
_
8.1
_102_02_
How do we write the missed-bat detection formula for Bat #2 (which is located to the right of the court)?
The current x-coordinate of the ball
exceeds the coordinate of Bat#1 (in the
negative direction) plus the ball radius
The past x-coordinate of the
ball is within the coordinate
of Bat#1 plus the ball radius
An outline of all the collision event formulas are presented below:
T16: “=AND(S5-P12/1.8<=S27,S27<=S5+P12/1.8,R27<-V9/2+V8+V11,R28>=-V9/2+V8+V11)”
T17: “=AND(NOT(AND(S5-P12/1.8<=S27,S27<=S5+P12/1.8)),R27<-V9/2+V8+V11,R28>=-V9/2+V8+V11)”
T18: “=AND(S6-P12/1.8<=S27,S27<=S6+P12/1.8,R27>V9/2-V8-V11,R28<=V9/2-V8-V11)”
T19: “=AND(NOT(AND(S6-P12/1.8<=S27,S27<=S6+P12/1.8)),R27>V9/2-V8-V11,R28<=V9/2-V8-V11)”