@@ -57,41 +57,56 @@ class SLPhysicsCollision {
5757// circle.forces.append(collisionNormalVector
5858// .multiplyWithScalar(scalar: speedAfterRestitution))
5959// }
60- let restitution = 2.5
61-
62- if circleIntersectEdge ( circle: circle, vertexOne: triangle. vertexOne, vertexTwo: triangle. vertexTwo) {
60+ var restitution = 0.85
61+ if circleIntersectTriangleVertices ( triangle: triangle, circle: circle) {
62+ restitution = 0.95
63+ circle. setVelocity ( newVelocity: circle. velocity. negate ( ) . multiplyWithScalar ( scalar: restitution) )
64+ } else if circleIntersectEdge ( circle: circle, vertexOne: triangle. vertexOne, vertexTwo: triangle. vertexTwo) {
6365 // rebound counter clockwise
64- let line = Vector ( xDirection: triangle. vertexTwo. xCoordinate - triangle. vertexOne. xCoordinate,
65- yDirection: triangle. vertexTwo. yCoordinate - triangle. vertexOne. yCoordinate)
66- let normal = Vector ( xDirection: - line. yDirection, yDirection: line. xDirection)
67- let normalUnitVector = normal. multiplyWithScalar ( scalar: 1 / normal. magnitude)
68- let resultant = circle. velocity
69- . subtract ( vector: normalUnitVector
70- . multiplyWithScalar ( scalar: 2 * circle. velocity. dotProductWith ( vector: normalUnitVector) ) )
66+ // let line = Vector(xDirection: triangle.vertexTwo.xCoordinate - triangle.vertexOne.xCoordinate,
67+ // yDirection: triangle.vertexTwo.yCoordinate - triangle.vertexOne.yCoordinate)
68+ // let normal = Vector(xDirection: -line.yDirection, yDirection: line.xDirection)
69+ // let normalUnitVector = normal.multiplyWithScalar(scalar: 1/normal.magnitude)
70+ let resultant = Vector ( xDirection: - circle. velocity. yDirection,
71+ yDirection: circle. velocity. xDirection)
72+ // let resultant = circle.velocity
73+ // .subtract(vector: normalUnitVector
74+ // .multiplyWithScalar(scalar: 2 * circle.velocity.dotProductWith(vector: normalUnitVector)))
7175 circle. setVelocity ( newVelocity: resultant. multiplyWithScalar ( scalar: restitution) )
7276 } else if circleIntersectEdge ( circle: circle, vertexOne: triangle. vertexTwo, vertexTwo: triangle. vertexThree) {
7377 // rebound clockwise
74- let line = Vector ( xDirection: triangle. vertexThree. xCoordinate - triangle. vertexTwo. xCoordinate,
75- yDirection: triangle. vertexThree. yCoordinate - triangle. vertexTwo. yCoordinate)
76- let normal = Vector ( xDirection: line. yDirection, yDirection: - line. xDirection)
77- let normalUnitVector = normal. multiplyWithScalar ( scalar: 1 / normal. magnitude)
78- let resultant = circle. velocity
79- . subtract ( vector: normalUnitVector
80- . multiplyWithScalar ( scalar: 2 * circle. velocity. dotProductWith ( vector: normalUnitVector) ) )
78+ // let line = Vector(xDirection: triangle.vertexThree.xCoordinate - triangle.vertexTwo.xCoordinate,
79+ // yDirection: triangle.vertexThree.yCoordinate - triangle.vertexTwo.yCoordinate)
80+ // let normal = Vector(xDirection: line.yDirection, yDirection: -line.xDirection)
81+ // let normalUnitVector = normal.multiplyWithScalar(scalar: 1/normal.magnitude)
82+ let resultant = Vector ( xDirection: circle. velocity. yDirection,
83+ yDirection: - circle. velocity. xDirection)
84+ // let resultant = circle.velocity
85+ // .subtract(vector: normalUnitVector
86+ // .multiplyWithScalar(scalar: 2 * circle.velocity.dotProductWith(vector: normalUnitVector)))
8187 circle. setVelocity ( newVelocity: resultant. multiplyWithScalar ( scalar: restitution) )
8288 } else if circleIntersectEdge ( circle: circle, vertexOne: triangle. vertexThree, vertexTwo: triangle. vertexOne) {
8389 // rebound clockwise
84- let line = Vector ( xDirection: triangle. vertexOne. xCoordinate - triangle. vertexThree. xCoordinate,
85- yDirection: triangle. vertexOne. yCoordinate - triangle. vertexThree. yCoordinate)
86- let normal = Vector ( xDirection: line. yDirection, yDirection: - line. xDirection)
87- let normalUnitVector = normal. multiplyWithScalar ( scalar: 1 / normal. magnitude)
88- let resultant = circle. velocity
89- . subtract ( vector: normalUnitVector
90- . multiplyWithScalar ( scalar: 2 * circle. velocity. dotProductWith ( vector: normalUnitVector) ) )
90+ // let line = Vector(xDirection: triangle.vertexOne.xCoordinate - triangle.vertexThree.xCoordinate,
91+ // yDirection: triangle.vertexOne.yCoordinate - triangle.vertexThree.yCoordinate)
92+ // let normal = Vector(xDirection: line.yDirection, yDirection: -line.xDirection)
93+ // let normalUnitVector = normal.multiplyWithScalar(scalar: 1/normal.magnitude)
94+ let resultant = Vector ( xDirection: circle. velocity. yDirection,
95+ yDirection: - circle. velocity. xDirection)
96+ // let resultant = circle.velocity
97+ // .subtract(vector: normalUnitVector
98+ // .multiplyWithScalar(scalar: 2 * circle.velocity.dotProductWith(vector: normalUnitVector)))
9199 circle. setVelocity ( newVelocity: resultant. multiplyWithScalar ( scalar: restitution) )
92100 }
93101 }
94102
103+ private func circleIntersectTriangleVertices(
104+ triangle: SLPhysicsTriangle , circle: SLPhysicsCircle ) -> Bool {
105+ circle. containsPoint ( triangle. vertexOne)
106+ || circle. containsPoint ( triangle. vertexTwo)
107+ || circle. containsPoint ( triangle. vertexThree)
108+ }
109+
95110 private func circleIntersectEdge( circle: SLPhysicsCircle , vertexOne: Point , vertexTwo: Point ) -> Bool {
96111 let radiusSqr = circle. radius * circle. radius
97112
0 commit comments