Created
February 5, 2021 07:12
-
-
Save PrimaryFeather/e6a508a600e350a05f262678aa07f29d to your computer and use it in GitHub Desktop.
Sample (for Starling Framework) showing how to detect collisions between a complex and a simple object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package starling | |
{ | |
import flash.geom.Matrix; | |
import flash.geom.Point; | |
import starling.display.Canvas; | |
import starling.display.Sprite; | |
import starling.events.Touch; | |
import starling.events.TouchEvent; | |
import starling.events.TouchPhase; | |
import starling.geom.Polygon; | |
import starling.utils.MatrixUtil; | |
import starling.utils.Pool; | |
public class CollisionDetectionSample extends Sprite | |
{ | |
private var _moon:Sprite; | |
private var _ball:Sprite; | |
private var _moonPolygon:Polygon; | |
private const _moonVertices:Array = [ | |
50, 0, | |
36, 16, | |
29.6, 32, | |
27.2, 50, | |
29.6, 68, | |
36, 84, | |
50, 100, | |
24, 92.7, | |
7.3, 76, | |
0, 50, | |
7.3, 24, | |
24, 7.3 | |
]; | |
private const _ballVertices:Array = [10, 0, 20, 10, 10, 20, 0, 10]; | |
public function CollisionDetectionSample() | |
{ | |
_moonPolygon = new Polygon(_moonVertices); | |
var moonCanvas:Canvas = new Canvas(); | |
moonCanvas.beginFill(0xff0000); | |
moonCanvas.drawPolygon(_moonPolygon); | |
moonCanvas.endFill(); | |
_moon = new Sprite(); | |
_moon.addChild(moonCanvas); | |
_moon.x = 150; | |
_moon.y = 100; | |
_moon.scale = 2.0; | |
_moon.rotation = 0.5; | |
addChild(_moon); | |
var ballPolygon:Polygon = new Polygon(_ballVertices); | |
var ballCanvas:Canvas = new Canvas(); | |
ballCanvas.beginFill(0xff); | |
ballCanvas.drawPolygon(ballPolygon); | |
ballCanvas.endFill(); | |
_ball = new Sprite(); | |
_ball.addChild(ballCanvas); | |
_ball.x = 200; | |
_ball.y = 250; | |
_ball.addEventListener(TouchEvent.TOUCH, onBallTouched); | |
addChild(_ball); | |
} | |
public function onBallTouched(event:TouchEvent):void | |
{ | |
var touch:Touch = event.getTouch(this, TouchPhase.MOVED); | |
if (touch != null) | |
{ | |
var movement:Point = event.getTouch(_ball).getMovement(this); | |
_ball.x += movement.x; | |
_ball.y += movement.y; | |
testForCollision(); | |
} | |
} | |
private function testForCollision():void | |
{ | |
var matrix:Matrix = _ball.getTransformationMatrix(_moon); | |
var numCollidingPoints:int = 0; | |
for (var i:int = 0; i < _ballVertices.length; i += 2) | |
{ | |
var ballVertex:Point = Pool.getPoint(_ballVertices[i], _ballVertices[i+1]); | |
var ballVertexInMoon:Point = MatrixUtil.transformPoint( | |
matrix, ballVertex, Pool.getPoint()); | |
if (_moonPolygon.containsPoint(ballVertexInMoon)) | |
numCollidingPoints += 1; | |
Pool.putPoint(ballVertex); | |
Pool.putPoint(ballVertexInMoon); | |
} | |
trace("number of points colliding: " + numCollidingPoints); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment