' Display
_Title "Collisions - Version 9"
' Meta
start:
' Data structures
' Object type
' Object storage
' Dynamics
' Environment
' Initialize
ShapeCount = 0
CollisionCount = 0
' Prompt
Call cprintstring
(16 * 17, "WELCOME! ") Call cprintstring
(16 * 16, "Press 1 for Pool prototype ") Call cprintstring
(16 * 15, "Press 2 for Wacky game ") Call cprintstring
(16 * 14, "Press 3 for Concentric rings") Call cprintstring
(16 * 13, "Press 4 for Walls only ") Call cprintstring
(16 * 12, "Press 5 for Angle pong game ")
Call cprintstring
(-16 * 4, "During Play:") Call cprintstring
(-16 * 6, "Move mouse to select closest object (by centroid).") Call cprintstring
(-16 * 7, "Boost velocity with arrow keys or W/S/A/D. ") Call cprintstring
(-16 * 8, "Boost angluar velocity with Q/E. ") Call cprintstring
(-16 * 9, "Drag and fling object with Mouse 1. ") Call cprintstring
(-16 * 10, "Rotate selected object with Mousewheel. ") Call cprintstring
(-16 * 11, "Halt all motion with ESC. ") Call cprintstring
(-16 * 12, "Create new ball with Mouse 2. ") Call cprintstring
(-16 * 13, "Initiate creative mode with SPACE. ") Call cprintstring
(-16 * 14, "Restart by pressing R during motion. ") Call cprintstring
(-16 * 16, "PRESS ANY KEY TO BEGIN.")
' Main loop
Call PairDynamics
(CPC
, FPC
, RST
) Call FleetDynamics
(VD
, SV
)
TheReturn = 0
' Keyboard input
Call cprintstring
(16 * 17, "Drag Mouse 1 counter-clockwise to draw a new shape.") Call cprintstring
(16 * 16, "Make sure centroid is inside body. ") Call NewMouseShape
(7.5, 150, 15) Shape(SelectedShape).Velocity.y = Shape(SelectedShape).Velocity.y * 1.05 + 1.5
Shape(SelectedShape).Velocity.y = Shape(SelectedShape).Velocity.y * 0.95 - 1.5
Shape(SelectedShape).Velocity.x = Shape(SelectedShape).Velocity.x * 0.95 - 1.5
Shape(SelectedShape).Velocity.x = Shape(SelectedShape).Velocity.x * 1.05 + 1.5
Shape(SelectedShape).Omega = Omega * 0.5 - .02
Shape(SelectedShape).Omega = Omega * 1.5 + .02
TheReturn = -1
Shape
(k
).Velocity.x
= .000001 * (Rnd - .5) Shape
(k
).Velocity.y
= .000001 * (Rnd - .5) Shape
(k
).Omega
= .000001 * (Rnd - .5)
' Mouse input
mb = 0
mxold = 999999999
myold = 999999999
rmin = 999999999
dx = x - Shape(k).Centroid.x
dy = y - Shape(k).Centroid.y
r2 = dx * dx + dy * dy
rmin = r2
SelectedShape = k
mb = 1
vtemp.x = x - Shape(SelectedShape).Centroid.x
vtemp.y = y - Shape(SelectedShape).Centroid.y
Call TranslateShape
(SelectedShape
, vtemp
) Shape(SelectedShape).Velocity.x = 0
Shape(SelectedShape).Velocity.y = 0
Shape(SelectedShape).Omega = 0
mxold = x
myold = y
mb = 1
Call NewAutoBall
(x
, y
, 15, 0, 1, 1, 0) Call RotShape
(SelectedShape
, Shape
(SelectedShape
).Centroid
, -.02 * 8 * Atn(1)) Call RotShape
(SelectedShape
, Shape
(SelectedShape
).Centroid
, .02 * 8 * Atn(1)) If ((mxold
<> 999999999) And (myold
<> 999999999)) Then Shape(SelectedShape).Velocity.x = x - mxold
Shape(SelectedShape).Velocity.y = y - myold
UserInput = TheReturn
' Proximity detection
ProximalPairsCount = 0
Shape1 = 0
Shape2 = 0
Shape(j).CollisionSwitch = 0
Shape(j).DeltaO = 0
Shape(j).DeltaV.x = 0
Shape(j).DeltaV.y = 0
Shape(j).PartialNormal.x = 0
Shape(j).PartialNormal.y = 0
For k
= j
+ 1 To ShapeCount
dx = Shape(j).Centroid.x - Shape(k).Centroid.x
dy = Shape(j).Centroid.y - Shape(k).Centroid.y
dr
= Sqr(dx
* dx
+ dy
* dy
) If (dr
< (CoarseProximityConstant
) * (Shape
(j
).Diameter
+ Shape
(k
).Diameter
)) Then ProximalPairsCount = ProximalPairsCount + 1
ProximalPairs(ProximalPairsCount, 1) = j
ProximalPairs(ProximalPairsCount, 2) = k
'Shape1 = j
'Shape2 = k
ContactPoints = 0
If (ProximalPairsCount
> 0) Then For n
= 1 To ProximalPairsCount
Shape1 = ProximalPairs(n, 1)
Shape2 = ProximalPairs(n, 2)
' Collision detection
rmin = 999999999
ClosestIndex1 = 0
ClosestIndex2 = 0
NumJK = 0
For j
= 1 To Shape
(Shape1
).Elements
For k
= 1 To Shape
(Shape2
).Elements
dx = PointChain(Shape1, j).x - PointChain(Shape2, k).x
dy = PointChain(Shape1, j).y - PointChain(Shape2, k).y
r2 = dx * dx + dy * dy
If (r2
<= FineProximityConstant
) Then
ContactPoints = ContactPoints + 1
' Partial normal vector 1
nx1 = CalculateNormalY(Shape1, j)
ny1 = -CalculateNormalX(Shape1, j)
nn
= Sqr(nx1
* nx1
+ ny1
* ny1
) nx1 = nx1 / nn
ny1 = ny1 / nn
Shape(Shape1).PartialNormal.x = Shape(Shape1).PartialNormal.x + nx1
Shape(Shape1).PartialNormal.y = Shape(Shape1).PartialNormal.y + ny1
' Partial normal vector 2
nx2 = CalculateNormalY(Shape2, k)
ny2 = -CalculateNormalX(Shape2, k)
nn
= Sqr(nx2
* nx2
+ ny2
* ny2
) nx2 = nx2 / nn
ny2 = ny2 / nn
Shape(Shape2).PartialNormal.x = Shape(Shape2).PartialNormal.x + nx2
Shape(Shape2).PartialNormal.y = Shape(Shape2).PartialNormal.y + ny2
NumJK = NumJK + 1
GrossJ(NumJK) = j
GrossK(NumJK) = k
rmin = r2
ClosestIndex1 = j
ClosestIndex2 = k
If ((GrossJ
(1) - GrossJ
(NumJK
)) * (GrossJ
(1) - GrossJ
(NumJK
)) > 50) Then 'ClosestIndex1 = 1
ClosestIndex1
= Int(IntegrateArray
(GrossJ
(), NumJK
) / NumJK
) If ((GrossK
(1) - GrossK
(NumJK
)) * (GrossK
(1) - GrossK
(NumJK
)) > 50) Then 'ClosestIndex2 = 1
ClosestIndex2
= Int(IntegrateArray
(GrossK
(), NumJK
) / NumJK
)
If (rmin
<= FineProximityConstant
) Then
CollisionCount = CollisionCount + 1
Shape(Shape1).CollisionSwitch = 1
Shape(Shape2).CollisionSwitch = 1
' Undo previous motion
If (Shape
(Shape1
).Collisions
= 0) Then Call RotShape
(Shape1
, Shape
(Shape1
).Centroid
, -1 * Shape
(Shape1
).Omega
) vtemp.x = -1 * (Shape(Shape1).Velocity.x)
vtemp.y = -1 * (Shape(Shape1).Velocity.y)
Call TranslateShape
(Shape1
, vtemp
) If (Shape
(Shape2
).Collisions
= 0) Then Call RotShape
(Shape2
, Shape
(Shape2
).Centroid
, -1 * Shape
(Shape2
).Omega
) vtemp.x = -1 * (Shape(Shape2).Velocity.x)
vtemp.y = -1 * (Shape(Shape2).Velocity.y)
Call TranslateShape
(Shape2
, vtemp
)
' Momentum absorption
If (Shape
(Shape1
).Collisions
= 0) Then Shape(Shape1).Velocity.x = Shape(Shape1).Velocity.x * Restitution
Shape(Shape1).Velocity.y = Shape(Shape1).Velocity.y * Restitution
If (Shape
(Shape2
).Collisions
= 0) Then Shape(Shape2).Velocity.x = Shape(Shape2).Velocity.x * Restitution
Shape(Shape2).Velocity.y = Shape(Shape2).Velocity.y * Restitution
' Centroid of object 1 (cx1, cy1)
cx1 = Shape(Shape1).Centroid.x
cy1 = Shape(Shape1).Centroid.y
' Centroid of object 2 (cx2, cy2)
cx2 = Shape(Shape2).Centroid.x
cy2 = Shape(Shape2).Centroid.y
' Contact point on object 1 (px1, py1)
px1 = PointChain(Shape1, ClosestIndex1).x
py1 = PointChain(Shape1, ClosestIndex1).y
' Contact point on object 2 (px2, py2)
px2 = PointChain(Shape2, ClosestIndex2).x
py2 = PointChain(Shape2, ClosestIndex2).y
' Contact-centroid differentials 1 (dx1, dy1)
dx1 = px1 - cx1
dy1 = py1 - cy1
' Contact-centroid differentials 2 (dx2, dy2)
dx2 = px2 - cx2
dy2 = py2 - cy2
' Normal vector 1 (nx1, ny1)
nn
= Sqr(Shape
(Shape1
).PartialNormal.x
* Shape
(Shape1
).PartialNormal.x
+ Shape
(Shape1
).PartialNormal.y
* Shape
(Shape1
).PartialNormal.y
) nx1 = Shape(Shape1).PartialNormal.x / nn
ny1 = Shape(Shape1).PartialNormal.y / nn
' Normal vector 2 (nx2, ny2)
nn
= Sqr(Shape
(Shape2
).PartialNormal.x
* Shape
(Shape2
).PartialNormal.x
+ Shape
(Shape2
).PartialNormal.y
* Shape
(Shape2
).PartialNormal.y
) nx2 = Shape(Shape2).PartialNormal.x / nn
ny2 = Shape(Shape2).PartialNormal.y / nn
'''
'nx1 = CalculateNormalY(Shape1, ClosestIndex1)
'ny1 = -CalculateNormalX(Shape1, ClosestIndex1)
'nn = SQR(nx1 * nx1 + ny1 * ny1)
'nx1 = nx1 / nn
'ny1 = ny1 / nn
'nx2 = CalculateNormalY(Shape2, ClosestIndex2)
'ny2 = -CalculateNormalX(Shape2, ClosestIndex2)
'nn = SQR(nx2 * nx2 + ny2 * ny2)
'nx2 = nx2 / nn
'ny2 = ny2 / nn
'''
' Perpendicular vector 1 (prx1, pry1)
prx1 = -1 * dy1
pry1 = dx1
pp
= Sqr(prx1
* prx1
+ pry1
* pry1
) prx1 = prx1 / pp
pry1 = pry1 / pp
' Perpendicular vector 2 (prx2, pry2)
prx2 = -1 * dy2
pry2 = dx2
pp
= Sqr(prx2
* prx2
+ pry2
* pry2
) prx2 = prx2 / pp
pry2 = pry2 / pp
' Angular velocity vector 1 (w1, r1, vx1, vy1)
w1 = Shape(Shape1).Omega
r1
= Sqr(dx1
* dx1
+ dy1
* dy1
) vx1 = w1 * r1 * prx1
vy1 = w1 * r1 * pry1
' Angular velocity vector 2 (w2, r2, vx2, vy2)
w2 = Shape(Shape2).Omega
r2
= Sqr(dx2
* dx2
+ dy2
* dy2
) vx2 = w2 * r2 * prx2
vy2 = w2 * r2 * pry2
' Mass terms (m1, m2, mu)
m1 = Shape(Shape1).Mass
m2 = Shape(Shape2).Mass
mu = 1 / (1 / m1 + 1 / m2)
' Re-Calculate moment of inertia (i1, i2)
vtemp.x = px1
vtemp.y = py1
Call CalculateMOI
(Shape1
, vtemp
) vtemp.x = px2
vtemp.y = py2
Call CalculateMOI
(Shape2
, vtemp
) i1 = Shape(Shape1).MOI
i2 = Shape(Shape2).MOI
' Velocity differentials (v1, v2, dvtx, dvty)
vcx1 = Shape(Shape1).Velocity.x
vcy1 = Shape(Shape1).Velocity.y
vcx2 = Shape(Shape2).Velocity.x
vcy2 = Shape(Shape2).Velocity.y
vtx1 = vcx1 + vx1
vty1 = vcy1 + vy1
vtx2 = vcx2 + vx2
vty2 = vcy2 + vy2
v1
= Sqr(vtx1
* vtx1
+ vty1
* vty1
) v2
= Sqr(vtx2
* vtx2
+ vty2
* vty2
) dvtx = vtx2 - vtx1
dvty = vty2 - vty1
' Geometry (n1dotdvt, n2dotdvt)
n1dotdvt = nx1 * dvtx + ny1 * dvty
n2dotdvt = nx2 * dvtx + ny2 * dvty
' Momentum exchange (qx1, qy1, qx2, qy2)
qx1 = nx1 * 2 * mu * n1dotdvt
qy1 = ny1 * 2 * mu * n1dotdvt
qx2 = nx2 * 2 * mu * n2dotdvt
qy2 = ny2 * 2 * mu * n2dotdvt
' Momentum exchange unit vector (qhat)
qq
= Sqr(qx1
* qx1
+ qy1
* qy1
) qhatx1 = qx1 / qq
qx1 = 0
qhatx1 = 0
qhaty1 = qy1 / qq
qy1 = 0
qhaty1 = 0
qq
= Sqr(qx2
* qx2
+ qy2
* qy2
) qhatx2 = qx2 / qq
qx2 = 0
qhatx2 = 0
qhaty2 = qy2 / qq
qy2 = 0
qhaty2 = 0
' Angular impulse (qdotp)
q1dotp1 = qx1 * prx1 + qy1 * pry1
q2dotp2 = qx2 * prx2 + qy2 * pry2
' Translational impulse (qdotn, ndotrhat, f)
q1dotn1 = qhatx1 * nx1 + qhaty1 * ny1
q2dotn2 = qhatx2 * nx2 + qhaty2 * ny2
n1dotr1hat = (nx1 * dx1 + ny1 * dy1) / r1
n2dotr2hat = (nx2 * dx2 + ny2 * dy2) / r2
f1 = -q1dotn1 * n1dotr1hat
f2 = -q2dotn2 * n2dotr2hat
' Special case for shape within shape.
np = nx1 * nx2 + ny1 * ny2
dcx = cx1 - cx2
dcy = cy1 - cy2
dc
= Sqr(dcx
* dcx
+ dcy
* dcy
) If (m1
> m2
) Then ' This criteria may be bullshit in general but works now. q1dotp1 = -q1dotp1
f1 = -f1
q2dotp2 = -q2dotp2
f2 = -f2
' Angular impulse update (edits omega)
Shape(Shape1).DeltaO = Shape(Shape1).DeltaO + r1 * q1dotp1 / i1
Shape(Shape2).DeltaO = Shape(Shape2).DeltaO - r2 * q2dotp2 / i2
' Linear impulse update (edits velocity)
dvx1 = f1 * qx1 / m1
dvy1 = f1 * qy1 / m1
dvx2 = f2 * qx2 / m2
dvy2 = f2 * qy2 / m2
dvx1s = dvx1 * dvx1
dvy1s = dvy1 * dvy1
dvx2s = dvx2 * dvx2
dvy2s = dvy2 * dvy2
Shape(Shape1).DeltaV.x = Shape(Shape1).DeltaV.x + dvx1
Shape(Shape1).DeltaV.y = Shape(Shape1).DeltaV.y + dvy1
Shape(Shape2).DeltaV.x = Shape(Shape2).DeltaV.x + dvx2
Shape(Shape2).DeltaV.y = Shape(Shape2).DeltaV.y + dvy2
' External torque (edits omega)
torque1 = m1 * (dx1 * ForceField.y - dy1 * ForceField.x)
torque2 = m2 * (dx2 * ForceField.y - dy2 * ForceField.x)
Shape(Shape1).DeltaO = Shape(Shape1).DeltaO - torque1 / i1
Shape(Shape2).DeltaO = Shape(Shape2).DeltaO - torque2 / i2
' Separate along normal (edits position)
If (Shape
(Shape1
).Collisions
< 2) Then ' changed from = 0 vtemp.x = -nx1 * (.5 / m1) * (1 * v1 ^ 2 + 1 * w1 ^ 2)
vtemp.y = -ny1 * (.5 / m1) * (1 * v1 ^ 2 + 1 * w1 ^ 2)
Call TranslateShape
(Shape1
, vtemp
) If (Shape
(Shape2
).Collisions
< 2) Then vtemp.x = -nx2 * (.5 / m2) * (1 * v2 ^ 2 + 1 * w2 ^ 2)
vtemp.y = -ny2 * (.5 / m2) * (1 * v2 ^ 2 + 1 * w2 ^ 2)
Call TranslateShape
(Shape2
, vtemp
)
' Dent along normal
'PointChain(Shape1, ClosestIndex1).x = PointChain(Shape1, ClosestIndex1).x - v1 * nx1 / 2
'PointChain(Shape1, ClosestIndex1).y = PointChain(Shape1, ClosestIndex1).y - v1 * ny1 / 2
'PointChain(Shape2, ClosestIndex2).x = PointChain(Shape2, ClosestIndex2).x - v2 * nx2 / 2
'PointChain(Shape2, ClosestIndex2).y = PointChain(Shape2, ClosestIndex2).y - v2 * ny2 / 2
' Feedback
If ((Shape
(Shape1
).Collisions
= 0) And (Shape
(Shape2
).Collisions
= 0)) Then Call snd
(100 * (v1
+ v2
) / 2, .5)
For ShapeIndex
= 1 To ShapeCount
' Contact update
If (Shape
(ShapeIndex
).CollisionSwitch
= 1) Then Shape(ShapeIndex).Collisions = Shape(ShapeIndex).Collisions + 1
Shape(ShapeIndex).Collisions = 0
If (Shape
(ShapeIndex
).Fixed
= 0) Then
' Angular velocity update
Shape(ShapeIndex).Omega = Shape(ShapeIndex).Omega + Shape(ShapeIndex).DeltaO
' Linear velocity update
Shape(ShapeIndex).Velocity.x = Shape(ShapeIndex).Velocity.x + Shape(ShapeIndex).DeltaV.x
Shape(ShapeIndex).Velocity.y = Shape(ShapeIndex).Velocity.y + Shape(ShapeIndex).DeltaV.y
If (Shape
(ShapeIndex
).Collisions
= 0) Then ' Freefall (if airborne)
Shape(ShapeIndex).Velocity.x = Shape(ShapeIndex).Velocity.x + ForceField.x
Shape(ShapeIndex).Velocity.y = Shape(ShapeIndex).Velocity.y + ForceField.y
If (Shape
(ShapeIndex
).Collisions
> 2) Then ' Static friction
If ((Shape
(ShapeIndex
).Velocity.x
* Shape
(ShapeIndex
).Velocity.x
) < LowLimitVelocity
) Then Shape(ShapeIndex).Velocity.x = Shape(ShapeIndex).Velocity.x * .05
If ((Shape
(ShapeIndex
).Velocity.y
* Shape
(ShapeIndex
).Velocity.y
) < LowLimitVelocity
) Then Shape(ShapeIndex).Velocity.y = Shape(ShapeIndex).Velocity.y * .05
If ((Shape
(ShapeIndex
).Omega
* Shape
(ShapeIndex
).Omega
) < .000015 * LowLimitVelocity
) Then Shape(ShapeIndex).Omega = 0
' Rotation update
Call RotShape
(ShapeIndex
, Shape
(ShapeIndex
).Centroid
, Shape
(ShapeIndex
).Omega
)
' Position update
Call TranslateShape
(ShapeIndex
, Shape
(ShapeIndex
).Velocity
)
' Motion Damping
Shape(ShapeIndex).Velocity.x = Shape(ShapeIndex).Velocity.x * MotionDamping
Shape(ShapeIndex).Velocity.y = Shape(ShapeIndex).Velocity.y * MotionDamping
Shape(ShapeIndex).Omega = Shape(ShapeIndex).Omega * MotionDamping
' Lock all motion
Shape(ShapeIndex).Velocity.x = 0
Shape(ShapeIndex).Velocity.y = 0
Shape(ShapeIndex).Omega = 0
Locate 1, 1:
Print ProximalPairsCount
, CollisionCount
, ContactPoints
For ShapeIndex
= 1 To ShapeCount
For i
= 1 To Shape
(ShapeIndex
).Elements
- 1 Call cpset
(PointChain
(ShapeIndex
, i
).x
, PointChain
(ShapeIndex
, i
).y
, Shape
(ShapeIndex
).Shade
) Call cline
(PointChain
(ShapeIndex
, i
).x
, PointChain
(ShapeIndex
, i
).y
, PointChain
(ShapeIndex
, i
+ 1).x
, PointChain
(ShapeIndex
, i
+ 1).y
, Shape
(ShapeIndex
).Shade
) If (ShapeIndex
= SelectedShape
) Then Call ccircle
(PointChain
(ShapeIndex
, i
).x
, PointChain
(ShapeIndex
, i
).y
, 1, Shape
(ShapeIndex
).Shade
) Call cpset
(PointChain
(ShapeIndex
, Shape
(ShapeIndex
).Elements
).x
, PointChain
(ShapeIndex
, Shape
(ShapeIndex
).Elements
).y
, Shape
(ShapeIndex
).Shade
) Call cline
(PointChain
(ShapeIndex
, 1).x
, PointChain
(ShapeIndex
, 1).y
, PointChain
(ShapeIndex
, Shape
(ShapeIndex
).Elements
).x
, PointChain
(ShapeIndex
, Shape
(ShapeIndex
).Elements
).y
, Shape
(ShapeIndex
).Shade
) Call cline
(Shape
(ShapeIndex
).Centroid.x
, Shape
(ShapeIndex
).Centroid.y
, PointChain
(ShapeIndex
, 1).x
, PointChain
(ShapeIndex
, 1).y
, Shape
(ShapeIndex
).Shade
) If (ShapeIndex
= SelectedShape
) Then Call ccircle
(Shape
(ShapeIndex
).Centroid.x
, Shape
(ShapeIndex
).Centroid.y
, 3, Shape
(ShapeIndex
).Shade
) Call cpaint
(Shape
(ShapeIndex
).Centroid.x
, Shape
(ShapeIndex
).Centroid.y
, Shape
(ShapeIndex
).Shade
, Shape
(ShapeIndex
).Shade
)
t = 0
t = t + arr(j)
IntegrateArray = t
li = i - 1
ri = i + 1
If (i
= 1) Then li
= Shape
(k
).Elements
If (i
= Shape
(k
).Elements
) Then ri
= 1 l.x = PointChain(k, li).x
r.x = PointChain(k, ri).x
dx = r.x - l.x
CalculateNormalX = dx
li = i - 1
ri = i + 1
If (i
= 1) Then li
= Shape
(k
).Elements
If (i
= Shape
(k
).Elements
) Then ri
= 1 l.y = PointChain(k, li).y
r.y = PointChain(k, ri).y
dy = r.y - l.y
CalculateNormalY = dy
xx = 0
yy = 0
For i
= 1 To Shape
(k
).Elements
xx = xx + PointChain(k, i).x
yy = yy + PointChain(k, i).y
Shape(k).Centroid.x = xx / Shape(k).Elements
Shape(k).Centroid.y = yy / Shape(k).Elements
r2max = -1
For i
= 1 To Shape
(k
).Elements
xx = Shape(k).Centroid.x - PointChain(k, i).x
yy = Shape(k).Centroid.y - PointChain(k, i).y
r2 = xx * xx + yy * yy
r2max = r2
Shape
(k
).Diameter
= Sqr(r2max
)
aa = 0
For i
= 2 To Shape
(k
).Elements
x = PointChain(k, i).x - Shape(k).Centroid.x
y = PointChain(k, i).y - Shape(k).Centroid.y
dx = (PointChain(k, i).x - PointChain(k, i - 1).x)
dy = (PointChain(k, i).y - PointChain(k, i - 1).y)
da = .5 * (x * dy - y * dx)
aa = aa + da
Shape
(k
).Mass
= factor
* Sqr(aa
* aa
)
xx = 0
yy = 0
For i
= 1 To Shape
(k
).Elements
a = ctrvec.x - PointChain(k, i).x
b = ctrvec.y - PointChain(k, i).y
xx = xx + a * a
yy = yy + b * b
Shape
(k
).MOI
= Sqr((xx
+ yy
) * (xx
+ yy
)) * (Shape
(k
).Mass
/ Shape
(k
).Elements
)
For i
= 1 To Shape
(k
).Elements
PointChain(k, i).x = PointChain(k, i).x + c.x
PointChain(k, i).y = PointChain(k, i).y + c.y
Shape(k).Centroid.x = Shape(k).Centroid.x + c.x
Shape(k).Centroid.y = Shape(k).Centroid.y + c.y
For i
= 1 To Shape
(k
).Elements
xx = PointChain(k, i).x - c.x
yy = PointChain(k, i).y - c.y
PointChain
(k
, i
).x
= c.x
+ xx
* Cos(da
) - yy
* Sin(da
) PointChain
(k
, i
).y
= c.y
+ yy
* Cos(da
) + xx
* Sin(da
)
ShapeCount = ShapeCount + 1
Shape(ShapeCount).Fixed = fx
Shape(ShapeCount).Collisions = 0
i = 0
i = i + 1
r
= r1
+ r2
* Cos(pa
* j
) ^ pb
PointChain
(ShapeCount
, i
).x
= x1
+ r
* Cos(j
) PointChain
(ShapeCount
, i
).y
= y1
+ r
* Sin(j
) Shape(ShapeCount).Elements = i
Call CalculateCentroid
(ShapeCount
) Call CalculateMass
(ShapeCount
, 1) Call CalculateMass
(ShapeCount
, 999999) Call CalculateMOI
(ShapeCount
, Shape
(ShapeCount
).Centroid
) Call CalculateDiameter
(ShapeCount
) Shape(ShapeCount).Velocity.x = 0
Shape(ShapeCount).Velocity.y = 0
Shape(ShapeCount).Omega = 0
Shape
(ShapeCount
).Shade
= _RGB(100, 100, 100) SelectedShape = ShapeCount
ShapeCount = ShapeCount + 1
Shape(ShapeCount).Fixed = 1
Shape(ShapeCount).Collisions = 0
i = 0
i = i + 1
PointChain(ShapeCount, i).x = x1 + wx / 2
PointChain(ShapeCount, i).y = y1 + j
i = i + 1
PointChain(ShapeCount, i).x = x1 + j
PointChain(ShapeCount, i).y = y1 + wy / 2
i = i + 1
PointChain(ShapeCount, i).x = x1 - wx / 2
PointChain(ShapeCount, i).y = y1 + j
i = i + 1
PointChain(ShapeCount, i).x = x1 + j
PointChain(ShapeCount, i).y = y1 - wy / 2
Shape(ShapeCount).Elements = i
Call CalculateCentroid
(ShapeCount
) Call CalculateMass
(ShapeCount
, 99999) Call CalculateMOI
(ShapeCount
, Shape
(ShapeCount
).Centroid
) Call CalculateDiameter
(ShapeCount
) Shape(ShapeCount).Velocity.x = 0
Shape(ShapeCount).Velocity.y = 0
Shape(ShapeCount).Omega = 0
Shape
(ShapeCount
).Shade
= _RGB(100, 100, 100) SelectedShape = ShapeCount
Call RotShape
(ShapeCount
, Shape
(ShapeCount
).Centroid
, ang
)
d1
= Sqr((xf
- xi
) ^ 2 + (yf
- yi
) ^ 2) d2
= Sqr(wx
^ 2 + wy
^ 2) ang
= Atn((yf
- yi
) / (xf
- xi
)) f = 1.2 * d2 / d1
Call NewAutoBrick
(xi
* (1 - t
) + xf
* t
, yi
* (1 - t
) + yf
* t
, wx
, wy
, ang
)
ShapeCount = ShapeCount + 1
Shape(ShapeCount).Fixed = 0
Shape(ShapeCount).Collisions = 0
numpoints = 0
xold = 999 ^ 999
yold = 999 ^ 999
delta
= Sqr((x
- xold
) ^ 2 + (y
- yold
) ^ 2) If (delta
> rawresolution
) And (numpoints
< targetpoints
- 1) Then numpoints = numpoints + 1
PointChain(ShapeCount, numpoints).x = x
PointChain(ShapeCount, numpoints).y = y
xold = x
yold = y
Do While (numpoints
< targetpoints
) rad2max = -1
kmax = -1
For k
= 1 To numpoints
- 1 xfac = PointChain(ShapeCount, k).x - PointChain(ShapeCount, k + 1).x
yfac = PointChain(ShapeCount, k).y - PointChain(ShapeCount, k + 1).y
rad2 = xfac ^ 2 + yfac ^ 2
kmax = k
rad2max = rad2
edgecase = 0
xfac = PointChain(ShapeCount, numpoints).x - PointChain(ShapeCount, 1).x
yfac = PointChain(ShapeCount, numpoints).y - PointChain(ShapeCount, 1).y
rad2 = xfac ^ 2 + yfac ^ 2
kmax = numpoints
rad2max = rad2
edgecase = 1
numpoints = numpoints + 1
PointChain(ShapeCount, j + 1).x = PointChain(ShapeCount, j).x
PointChain(ShapeCount, j + 1).y = PointChain(ShapeCount, j).y
PointChain(ShapeCount, kmax + 1).x = (1 / 2) * (PointChain(ShapeCount, kmax).x + PointChain(ShapeCount, kmax + 2).x)
PointChain(ShapeCount, kmax + 1).y = (1 / 2) * (PointChain(ShapeCount, kmax).y + PointChain(ShapeCount, kmax + 2).y)
PointChain(ShapeCount, numpoints).x = (1 / 2) * (PointChain(ShapeCount, 1).x + PointChain(ShapeCount, numpoints - 1).x)
PointChain(ShapeCount, numpoints).y = (1 / 2) * (PointChain(ShapeCount, 1).y + PointChain(ShapeCount, numpoints - 1).y)
For j
= 1 To smoothiterations
For k
= 2 To numpoints
- 1 TempChain(ShapeCount, k).x = (1 / 2) * (PointChain(ShapeCount, k - 1).x + PointChain(ShapeCount, k + 1).x)
TempChain(ShapeCount, k).y = (1 / 2) * (PointChain(ShapeCount, k - 1).y + PointChain(ShapeCount, k + 1).y)
For k
= 2 To numpoints
- 1 PointChain(ShapeCount, k).x = TempChain(ShapeCount, k).x
PointChain(ShapeCount, k).y = TempChain(ShapeCount, k).y
TempChain(ShapeCount, 1).x = (1 / 2) * (PointChain(ShapeCount, numpoints).x + PointChain(ShapeCount, 2).x)
TempChain(ShapeCount, 1).y = (1 / 2) * (PointChain(ShapeCount, numpoints).y + PointChain(ShapeCount, 2).y)
PointChain(ShapeCount, 1).x = TempChain(ShapeCount, 1).x
PointChain(ShapeCount, 1).y = TempChain(ShapeCount, 1).y
TempChain(ShapeCount, numpoints).x = (1 / 2) * (PointChain(ShapeCount, 1).x + PointChain(ShapeCount, numpoints - 1).x)
TempChain(ShapeCount, numpoints).y = (1 / 2) * (PointChain(ShapeCount, 1).y + PointChain(ShapeCount, numpoints - 1).y)
PointChain(ShapeCount, numpoints).x = TempChain(ShapeCount, numpoints).x
PointChain(ShapeCount, numpoints).y = TempChain(ShapeCount, numpoints).y
Shape(ShapeCount).Elements = numpoints
Call CalculateCentroid
(ShapeCount
) Call CalculateMass
(ShapeCount
, 1) Call CalculateMOI
(ShapeCount
, Shape
(ShapeCount
).Centroid
) Call CalculateDiameter
(ShapeCount
) Shape(ShapeCount).Velocity.x = 0
Shape(ShapeCount).Velocity.y = 0
Shape(ShapeCount).Omega = 0
SelectedShape = ShapeCount
' Set external field
ForceField.x = 0
ForceField.y = 0
' Rectangular border
wx = 42
wy = 10
wx = 40
wy = 10
' Balls (billiard setup)
x0 = 160
y0 = 0
r = 15
gg = 2 * r + 4
gx
= gg
* Cos(30 * 3.14159 / 180) gy
= gg
* Sin(30 * 3.14159 / 180) Call NewAutoBall
(x0
+ 0 * gx
, y0
+ 0 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 1 * gx
, y0
+ 1 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 1 * gx
, y0
- 1 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 2 * gx
, y0
+ 2 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 2 * gx
, y0
+ 0 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 2 * gx
, y0
- 2 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
+ 3 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
+ 1 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
- 1 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
- 3 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 4 * gx
, y0
+ 4 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 4 * gx
, y0
+ 2 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 4 * gx
, y0
- 0 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 4 * gx
, y0
- 2 * gy
, r
, 0, 1, 1, 0) Call NewAutoBall
(x0
+ 4 * gx
, y0
- 4 * gy
, r
, 0, 1, 1, 0)
' Cue ball
Call NewAutoBall
(-220, 0, r
, 0, 1, 1, 0) Shape
(ShapeCount
).Velocity.x
= 10 + 2 * Rnd Shape
(ShapeCount
).Velocity.y
= 1 * (Rnd - .5) Shape
(ShapeCount
).Shade
= _RGB(255, 255, 255)
' Parameters
CPC = 1.15
FPC = 8
RST = 0.75
VD = 0.995
SV = 0
' Set external field
ForceField.x = 0
ForceField.y = -.08
' Rectangular border
wx = 42
wy = 10
wx = 40
wy = 10
' Wacky balls
x0 = -70
y0 = 120
r1 = 15
r2 = 2.5
gg = 2.5 * (r1 + r2) + 3.5
gx
= gg
* Cos(30 * 3.14159 / 180) gy
= gg
* Sin(30 * 3.14159 / 180) Call NewAutoBall
(x0
+ 0 * gx
, y0
+ 0 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 1 * gx
, y0
+ 1 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 1 * gx
, y0
- 1 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 2 * gx
, y0
+ 2 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 2 * gx
, y0
+ 0 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 2 * gx
, y0
- 2 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
+ 3 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
+ 1 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
- 1 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0) Call NewAutoBall
(x0
+ 3 * gx
, y0
- 3 * gy
, r1
, r2
, Int(Rnd * 3) + 1, Int(Rnd * 1) + 2, 0)
' Slanted bricks
wx = 60
wy = 10
ww
= Sqr(wx
* wx
+ wy
* wy
) * .85 Call NewBrickLine
(ww
, 0, 100 + ww
, 100, wx
, wy
) Call NewBrickLine
(-ww
, 0, -100 - ww
, 100, wx
, wy
)
' Fidget spinner
Call NewAutoBall
(-220, 0, 20, 15, 1.5, 2, 0) Shape
(ShapeCount
).Shade
= _RGB(255, 255, 255)
' Parameters
CPC = 1.15
FPC = 8
RST = 0.70
VD = 0.995
SV = 0.025
' Set external field
ForceField.x = 0
ForceField.y = 0
' Rectangular border
wx = 42
wy = 10
wx = 40
wy = 10
Call NewAutoBall
(0, 0, r
, 0, 1, 1, 0)
' Parameters
CPC = 1.15
FPC = 8
RST = 0.75
VD = 0.995
SV = 0.025
' Set external field
ForceField.x = 0
ForceField.y = 0 - .08
' Fidget spinner
Call NewAutoBall
(-220, 0, 20, 15, 1.5, 2, 0) Shape
(ShapeCount
).Shade
= _RGB(255, 255, 255)
' Rectangular border
wx = 42
wy = 10
wx = 40
wy = 10
' Parameters
CPC = 1.15
FPC = 8
RST = 0.75
VD = 0.995
SV = 0.025
' Set external field
ForceField.x = 0
ForceField.y = 0
' Rectangular border
wx = 42
wy = 10
wx = 40
wy = 10
' Pong ball
Call NewAutoBall
(0, 200, 20, 0, 1, 1, 0) Shape(ShapeCount).Velocity.x = -1
Shape(ShapeCount).Velocity.y = -3
Shape
(ShapeCount
).Shade
= _RGB(255, 255, 255)
' Pong Paddle
Call NewAutoBrick
(-100, 10, 100, -10, -.02 * 8 * Atn(1)) vtemp.x = 0
vtemp.y = -200
Call TranslateShape
(ShapeCount
, vtemp
) Shape
(ShapeCount
).Shade
= _RGB(200, 200, 200)
' Parameters
CPC = 1.15
FPC = 8
RST = 1 '0.75
VD = 1 '0.995
SV = 0.025