Flash AS3 realizes simple sphere physical simulation

Source: Internet
Author: User
The code is as follows Copy Code
Package
{
Import Flash.display.Sprite;
Import flash.events.Event;
Import Flash.geom.Point;
Import Net.hires.debug.Stats;

/**
* ...
* @author Lizhi
*/
[SWF (width = 465, height = 465, BackgroundColor = 0xffffff, framerate = 100)]
public class Main extends Sprite
{
private var g:point = new Point (0,0.01);
private var b:number = 0.5;
private var cf:number = 0.02;
private Var W:number;
private Var H:number;

private Var Sb:ball;
private var max:number = 40;
private Var Numcols:int;
private Var Grid:array;
Public Function Main (): void
{
W = stage.stagewidth;
h = stage.stageheight;
Numcols = Math.ceil (H/max);
var c:int = 150;
var B:ball;
while (c-->0) {
var ball:ball = new ball ();
if (SB) {
B.next = ball;
b = ball;
}else {
SB = b = Ball;
}
ball.x = w * math.random ();
BALL.Y = h * math.random ();
BALL.R = 1 + * Math.random ();
}
SB.R = 20;
AddEventListener (event.enter_frame, update);
AddChild (New Stats ());
}

Private Function Update (e:event): void
{
Grid = [];
var b1:ball = sb;
sb.x = MouseX;
SB.Y = Mousey;
while (B1) {
var x:int = (b1.x + 0.5)/MAX;
var y:int = (b1.y + 0.5)/MAX;
var v:int = y * numcols + x;
if (grid[v]==null) {
GRID[V] = [];
}
Grid[v].push (B1);
b1.c = 0;
B1 = B1.next;
}

for (var k:int = grid.length-1 k >= 0;k--) {
var balls:array = grid[k];
if (balls) {
var arr2:array = grid[k-1];
if (ARR2) {
Balls = Balls.concat (ARR2);
}
ARR2 = Grid[k-numcols];
if (ARR2) {
Balls = Balls.concat (ARR2);
}
ARR2 = grid[k-numcols-1];
if (ARR2) {
Balls = Balls.concat (ARR2);
}
ARR2 = Grid[k-numcols + 1];
if (ARR2) {
Balls = Balls.concat (ARR2);
}
for (var i:int = 0; i < balls.length; i++) {
B1 = Balls[i];
B1.fy + = G.y;
if (B1.X-B1.R < 0) b1.fx + b;
if (b1.x + B1.R > W) b1.fx-= b;
if (B1.Y-B1.R < 0) B1.fy + b;
if (B1.y + b1.r > H) b1.fy-= b;
for (var j:int = i + 1; j < Balls.length; + +) {
var b2:ball = balls[j];
var dx:number = b1.x-b2.x;
var dy:number = b1.y-b2.y;
var d:number = b1.r + b2.r;
if (DX * dx + dy * Dy < d * d) {
B1.FX + + dx *CF;
B1.fy + dy *cf;
B2.FX-= DX *CF;
B2.fy-= dy * CF;
b1.c = 0xff0000;
b2.c = 0xff0000;
}
}
}
}
}

Graphics.clear ();
B1 = sb;
while (B1) {
B1.VX + = b1.fx;
B1.vy + = B1.fy;
B1.VX *= 0.95;
B1.vy *= 0.95;
b1.x + = B1.VX;
B1.y + = B1.vy;
b1.fx = 0;
B1.fy = 0;
Graphics.linestyle (0,b1.c);
Graphics.drawcircle (b1.x, B1.y, B1.R);
B1 = B1.next;
}
}

}

}
Class Ball {
public Var Next:ball;
public var c:uint = 0;
public var x:number = 0;
public var y:number = 0;
public var r:number = 10;
public var vx:number = 0;
public var vy:number = 0;
public var fx:number = 0;
public var fy:number = 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.