Complex collision detection tools

2010-03-08  来源:本站原创  分类:Flash  人气:202 

package com.easyasrpg.implement.algorithm
(
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
public class HitTest
(
/ **
* Collision detection of complex
* @ Param target1
* @ Param target2
* @ Param accurracy
* @ Return
*
* /
public static function complexHitTestObject (target1: DisplayObject, target2: DisplayObject, accurracy: Number = 1): Boolean
(
return complexIntersectionRectangle (target1, target2, accurracy). width! = 0;
)

public static function intersectionRectangle (target1: DisplayObject, target2: DisplayObject): Rectangle
(
/ / If either of the items don't have a reference to stage, then they are not in a display list
/ / Or if a simple hitTestObject is false, they cannot be intersecting.
if (! target1.root | |! target2.root | |! target1.hitTestObject (target2)) return new Rectangle ();
/ / Get the bounds of each DisplayObject.
var bounds1: Rectangle = target1.getBounds (target1.root);
var bounds2: Rectangle = target2.getBounds (target2.root);
/ / Determine test area boundaries.
var intersection: Rectangle = new Rectangle ();
intersection.x = Math.max (bounds1.x, bounds2.x);
intersection.y = Math.max (bounds1.y, bounds2.y);
intersection.width = Math.min ((bounds1.x + bounds1.width) - intersection.x, (bounds2.x + bounds2.width) - intersection.x);
intersection.height = Math.min ((bounds1.y + bounds1.height) - intersection.y, (bounds2.y + bounds2.height) - intersection.y);
return intersection;
)
public static function complexIntersectionRectangle (target1: DisplayObject, target2: DisplayObject, accurracy: Number = 1): Rectangle
(
if (accurracy <= 0) throw new Error ("ArgumentError: Error # 5001: Invalid value for accurracy", 5001);
/ / If a simple hitTestObject is false, they cannot be intersecting.
if (! target1.hitTestObject (target2)) return new Rectangle ();
var hitRectangle: Rectangle = intersectionRectangle (target1, target2);
/ / If their boundaries are no interesecting, they cannot be intersecting.

if (hitRectangle.width * accurracy <1 | | hitRectangle.height * accurracy <1) return new Rectangle ();
var bitmapData: BitmapData = new BitmapData (hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000);
/ / Draw the first target.
bitmapData.draw (target1, HitTest.getDrawMatrix (target1, hitRectangle, accurracy), new ColorTransform (1, 1, 1, 1, 255, -255, -255, 255));

/ / Overlay the second target.

bitmapData.draw (target2, HitTest.getDrawMatrix (target2, hitRectangle, accurracy), new ColorTransform (1, 1, 1, 1, 255, 255, 255, 255), BlendMode.DIFFERENCE);
/ / Find the intersection.

var intersection: Rectangle = bitmapData.getColorBoundsRect (0xFFFFFFFF, 0xFF00FFFF);
bitmapData.dispose ();
/ / Alter width and positions to compensate for accurracy

if (accurracy! = 1)
(
intersection.x / = accurracy;

intersection.y / = accurracy;

intersection.width / = accurracy;

intersection.height / = accurracy;
)
intersection.x + = hitRectangle.x;

intersection.y + = hitRectangle.y;
return intersection;

)
protected static function getDrawMatrix (target: DisplayObject, hitRectangle: Rectangle, accurracy: Number): Matrix
(
var localToGlobal: Point;;
var matrix: Matrix;
var rootConcatenatedMatrix: Matrix = target.root.transform.concatenatedMatrix;
localToGlobal = target.localToGlobal (new Point ());
matrix = target.transform.concatenatedMatrix;
matrix.tx = localToGlobal.x - hitRectangle.x;
matrix.ty = localToGlobal.y - hitRectangle.y;
matrix.a = matrix.a / rootConcatenatedMatrix.a;
matrix.d = matrix.d / rootConcatenatedMatrix.d;
if (accurracy! = 1) matrix.scale (accurracy, accurracy);
return matrix;
)
)
)

相关文章
  • Complex collision detection tools 2010-03-08

    package com.easyasrpg.implement.algorithm ( import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.DisplayObject; import flash.display.Sprite; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geo

  • Irregular picture or object collision detection 2010-07-02

    The following class encapsulates three public static methods, a simple collision detection can be used directly complexHitTestObject, need to be more complex, it would need to see to understand the following code, and use the three public static meth

  • 2D polygon-based collision detection and response (V) 2010-11-16

    Fifth, dealing with rotation In many games can be found in a number of rotating objects. Collision with them a bit complicated. Wizard's standard practice is to rotate through a simple point of view, usually the interval [0,2 * PI]. Triangular matrix

  • Multi-object collision detection 2010-08-20

    Document class package { import flash.display.Sprite; import flash.events.Event; /** * ... Multiple object collision detection * @author zkl * From :Action Script3.0 Animation */ public class Bubbles2 extends Sprite { private var balls:Array; private

  • flash in the collision detection (as3 Palace Way) 2010-09-04

    1. Main AS3 API hitTestObject (obj: DisplayObject): Boolean hitTestPoint (x: Number, y: Number, shapeFlag: Boolean = false); shapeFlag check the display object defines the actual pixel (true); or display the object border (false) package as3 { import

  • 2D polygon-based collision detection and response (a) 2010-11-16

    Introduction This article describes how a 2D action game accurate and efficient collision detection. This collision is based on the polygon and not on the wizard. Between these two will be different in design. Wizard-based collision detection is thro

  • 2D polygon-based collision detection and response (c) 2010-11-16

    Third, fast-moving objects on the further expansion of Slow moving object above approach will achieve very good results. But when the object moving very fast, the collision system will lose accuracy, missing the collision, or to allow cross between o

  • <Making Things Move> hitTestObject collision detection 2010-11-20

    This example comes from the "Making Things Move" book method for object collision detection hitTestObject simple to use, but for the irregular shape to be displayed "error in the collision," so the rectangle is, hitTestObject result of

  • [Turn] up point is not the origin of the collision detection 2011-01-10

    [Url] http://bbs.9ria.com/viewthread.php?tid=71944&extra=page% 3D1% 26amp; orderby% 3Ddateline% 26amp; filter% 3D86400 [/ url] General bitmapdata collision detection, if the registration point is at the origin can be a normal collision detection if t

  • Ultra-efficient collision detection of irregular objects 2011-01-12

    Before seen in the altar where a friend had posted a class of irregular objects, collision detection, here, I paste a foreigner, a cow write a class by their own tests, with a 1500 * 1500 vector and a 10 * not less than 10 moves like a small ball col

  • The second gate network security intrusion detection tools recommended by the top seven 2011-04-29

    IDS (Intrusion Detection), is an intrusion detection. It is through the collection and analysis of network behavior, security logs, audit data, and other information available on the network and computer systems in a number of key points of informati

  • IE browser memory leak detection tools 2010-03-16

    With the development of Web2.0 technology, rich client applications can be said more and more to each browser great pressure. The previous test may only focus on the server side, but now need to study the resources of browser usage, such as CPU utili

  • 2D polygon-based collision detection and response (b) 2010-11-16

    Second, the expansion for the collision response method of separating axes Polygon intersection detection method is very useful, but can do more. When the polygon intersection, I want them to move to avoid them intersect. Separation axis method can b

  • 2D polygon-based collision detection and response (f) 2010-11-16

    VI, calculated contact In order to move the rigid body dynamics, we need accurate calculation of the contact between two colliding polygons. This is not complicated for 2D, but in the 3D scene can become very complex. In the 2D case, consider two cas

  • 2D polygon-based collision detection and response (d) 2010-11-16

    Fourth, the basic arc collision response The following is to be made given the amount of separation of the two objects, and add a little friction and a number of static friction, so that the objects resting on the inclined plane. The speed of some si

  • 2D polygon-based collision detection and response (g) 2010-11-16

    In order not to waste time talking about the dynamic rigid body, can directly reference see the following links: http://www.d6.com/users/checker/dynamics.htm # articles Here shows how to implement the 2D real physical game. I will introduce some of m

  • Pixel collision detection 2010-11-18

    var redRect:Rectangle = redClip.getBounds(this); var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0); redClipBmpData.draw(redClip); var blueRect:Rectangle = blueClip.getBounds(this); var blueClipBmpData = new BitmapData(blueRe

  • WebSphere Application Server memory leak detection and diagnostic tools 2010-11-25

    Level: Intermediate School North Lee ([email protected]), Senior Software Engineer, IBM China Software Development Center November 21, 2006 This article describes how to implement WebSphere Application Server application memory leak detection, and IB

  • More complex formulas, including the (slow moving - Friction - Rotary - Collision - Rebound - Gravity - Gravity) 2011-01-10

    -------------------------------------------------- --------- (Basic physical) velocity and acceleration Angular velocity is converted to x, y velocity vector: vx = speed * Math.cos (angle); vy = speed * Math.sin (angle); Angular acceleration (acting

  • Spring inventory outstanding tools, Part 1: File and Web-related resources, tools operation 2010-08-06

    Spring inventory outstanding tools, Part 1: File and Web-related resources, tools operation Transfer from: http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ Spring not only provides a full-featured application development framework, itsel