Ape physical engine + example

Source: Internet
Author: User
Tags addgroup

Go first
Http://www.cove.org/ape/index.htm


Download the author's latest ape version.
Then open the instruction document docs/API/index.html
There are a total of 12 classes, which seem very rare, so beginners can have more confidence.
**************************************** **************************************** *******************
First, let's briefly introduce:
Abstractcollection
Abstract classes of all groups
Abstractconstraint
All abstract classes of physical interaction
Abstractitem
Base classes of all interactions (collisions) and particles
Abstractparticle
About the particle base class
Apengine
Main engine and force
Circleparticle
Circular Particles
Rectangleparticle
Rectangular Particle
Composite
Composite objects that can contain particles and collisions
Group
A group of classes that can contain particles, collisions, and Complexes
Springconstraint
A class (spring) similar to an elastic collision between two particles)
Vector
Force
Wheelparticle
One particle, simulating wheel Behavior
**************************************** **************************************** ************
Inheritance relationships of these classes:
Abstractcollection
Abstract classes of all groups
Composite
Composite objects that can contain particles and collisions
Group
A group of classes that can contain particles, collisions, and Complexes
Abstractitem
Base classes of all interactions (collisions) and particles
Abstractconstraint
All abstract classes of physical interaction
Springconstraint
A class (spring) similar to an elastic collision between two particles)
Abstractparticle
About the particle base class
Rectangleparticle
Rectangular Particle
Circleparticle
Circular Particles
Wheelparticle
One particle, simulating wheel Behavior
Apengine
Main engine and force
Vector
Force

OK. We have a preliminary understanding of this physical engine. Here we will use it to implement a simple example and understand its usage:
Let's take a look at the results of this example.
:
Wsad can be used to increase the axial force of one ball. The ball quality is different from that of other balls.
The buttons below
You can add additional force to the entire container. Try the effect first ~ ^ & ^
Http://blog.5d.cn/user47/hzq1122/upload/2008-06/myP.swf



**************************************** **************************************** *******************************
Start production below:
My tools
It is Flex, so take flex production as an example. If you haven't installed it, search for it and try to install the flex plug-in version of Eclipse.
**************************************** **************************************** ********************************
Here I have created a class as the border box. Code
As follows:
Package
{
Import org. Cove. Ape. *; // import the package
Public class floor extends group
{
Public Function floor ()
{
// Defines the rectangular particles. The parameters in the brackets are: abscissa, ordinate, width, height, radians, and whether they are fixed.
VaR diban1: rectangleparticle = new rectangleparticle (, 10, 0, true)
VaR diban2: rectangleparticle = new rectangleparticle (250,400,400, true)
VaR diban3: rectangleparticle = new rectangleparticle (50,200,400, 10, math. PI/2, true)
VaR diban4: rectangleparticle = new rectangleparticle (450,200,400, 10, math. PI/2, true)
// Setstyle method of the parent class of the rectangular particle.
Diban1.setstyle (1, 0x006699,1, 0x663366,1)
Diban2.setstyle (1, 0x006699,1, 0x663366,1)
Diban3.setstyle (1, 0x006699,1, 0x663366,1)
Diban4.setstyle (1, 0x006699,1, 0x663366,1)
// Add the particle (when it is addchild)
Addparticle (diban1 );
Addparticle (diban2 );
Addparticle (diban3 );
Addparticle (diban4 );
}
}
}
OK. The class of the border box is finished. Write the mxml file below
.
**************************************** **************************************** ********************************
Source code
:
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" applicati>
<Mx: SCRIPT>
<! [CDATA [
Import MX. Core. uicomponent;
Import org. Cove. Ape .*;
// Flex requires uicomponent as a container, so it defines a container as a physical engine.
Private var apecontent: uicomponent;
Private var heros: Group
Private var floors: Floor
Private var others: Array

Private var hero: circleparticle

Private function gametest (): void {
// Setting the frame rate to a high point is smoother.
Stage. framerate = 100
Heros = new group ();
Floors = new floor ();
Others = new array ();
Apecontent = new uicomponent ();
Addchild (apecontent)
Apengine. INIT (0.25 );
Apengine. Container = apecontent;

// Define 10 balls
For (var I: Int = 0; I <10; I ++ ){
VaR other: Group = new group ()
Apengine. addgroup (other)
Other. addparticle (New circleparticle (math. Random () * 100 + 50, math. Random () * 100 + 20, 10 ))
Others
= Other
// Make the ball work with other members in the group
Other. addcolw.ablelist (Others)
}

Apengine. addgroup (heros );
Hero = new circleparticle (150,20, 10, false, 2 );
Heros. addparticle (HERO)
// Enable this hero to interact with members in others
Heros. addcol1_ablelist (Others)
Apengine. addgroup (floors );
Floors. addcollidable (heros)
// Same as above
Floors. addcolw.ablelist (Others)

Stage. addeventlistener (keyboardevent. key_down, OND );
Stage. addeventlistener (event. enter_frame, ONF );
}
Public Function OND (E: keyboardevent): void {
Switch (E. keycode ){
Case "W". charcodeat ():
Hero. addforce (new vector (0,-3 ));
// The difference between addforce and addmasslessforce lies in whether quality is ignored.
// Hero. addmasslessforce (new vector (0,-3 ));
Break;
Case "S". charcodeat ():
Hero. addforce (new vector (0, 3 ));
Break;
Case "A". charcodeat ():
Hero. addforce (new vector (-3, 0 ));
Break;
Case "D". charcodeat ():
Hero. addforce (new vector (3, 0 ));
Break;
}
}
Public Function onf (E: Event): void {
// Redraw
Apengine. Step ();
Apengine. Paint ();
}
// Create several buttons and click to execute the following method:
Private function left (): void {
Apengine. addmasslessforce (new vector (-3, 0 ));
}
Private function right (): void {
Apengine. addforce (new vector (3, 0 ));
}
Private function up (): void {
Apengine. addforce (new vector (0,-3 ));
}
Private function down (): void {
Apengine. addforce (new vector (0, 3 ));
}
Private function nothing (): void {
Apengine. addforce (new vector (0, 0 ));
}
]>
</MX: SCRIPT>
<Mx: button x = "10" Y = "410" label = "Left" Click = "Left ()"/>
<Mx: button x = "64" Y = "410" label = "right" Click = "right ()"/>
<Mx: button x = "125" Y = "410" label = "up" Click = "Up ()"/>
<Mx: button x = "173" Y = "410" label = "down" Click = "down ()"/>
<Mx: button x = "237" Y = "410" label = "nothing" Click = "Nothing ()"/>
</MX: Application>

**************************************** **************************************** ********************
This process is complete. Let's take a look at abstractparticle.
The most common attribute of this class
And Methods
Center: Vector

[Read-Only] returns a vector of the current location of the particle
Returns the force on the current object.
Colretriable: Boolean

Determines if the particle can collide with other participant or constraints.
Determines whether the current particle works with other particles or not.
Elasticity: Number

The elasticity of the particle.
Elasticity
Fixed: Boolean

The fixed state of the particle.
Fixed or not
Friction: Number

The surface friction of the particle.
Particle Surface Friction
Mass: Number

The mass of the particle.
Quality
Px: Number

The X position of this particle
PY: Number

The Y position of this particle
**************************************** **************************************** *****************************
Now you can try to make a game similar to billiards.
Now! Easy!

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.