[Translation] chipmunk 2D game gravity Engine

Source: Internet
Author: User
Tags mathematical functions

Translation:Jan 17,201 0By Alex Liu
Documentation

Chipmunk physics

Note:The documentation on this Wiki documents ents chipmunk v5.0.0.

First of all, chipmunk is a 2D rigid body physics library distributed under the MIT license. though not yet complete, it is intended to be fast, numerically stable, and easy to use. I 've put hundreds of hours of work into making chipmunk what it is today. if you find chipmunk has saved you a lot of time, please consider donating. you'll make an indie game developer very happy.

I wowould like to give a Erin catto a big thank you, as the most of the ideas for the constraint solver I use now come from his box2d example code. (now a full fledged physics engine all it's own: box2d ). his contact persistence idea allows for stable stacks of objects with very few iterations of the contact solution. previusly my solver produced mushy piles of objects or required a large number of iterations in order to get a stable behavior.

 

This document is intended for V5. 0.

First, chipmunk is a 2D rigid-body physical engine library that complies with the MIT protocol. Although it is not completed yet, it is designed faster, more stable, and easier to use. I have invested hundreds of hours in chipmunk. If you find that using chipmunk can save you time, you can consider donating money. It can make game developers happy.

I am very grateful to Erin catto, because it comes from box2d examples of many solutions ..... ..

OVERVIEW

There are 4 basic things you need to know about in order to work with chipmunk.

To be able to use chipmunk, you need to understand the following basic features in order.

 

 

  • Rigid Bodies:A rigid body holds the physical properties of an object. (mass, position, rotation, velocity, etc .) it does not have a shape by itself. if you 've done physics with participant before, rigid bodies differ in that they are able to rotate.
  • A rigid body is the physical attribute of an object. It does not have its own shape. If you have knowledge about particles in material resources, the rigid body can be rotated unlike particles.
  • Collision shapes:By attaching shapes to bodies, you can define the body's shape. you can attach as your shapes to a single body as you need to in order to define a complex shape. shapes contain the surface properties of an object such as how much friction or elasticity it has.
  • Conflicting shape: By appending an object shape, you can define the shape of an object. You can attach many shapes and complex shapes to a single object. Shape includes surface properties, as if there is much friction or elasticity (elasticity)
  • Constraints/joints:Constraints and joints describe how bodies are attached to each other.
  • Restriction and connection: describes how objects are exposed.
  • Spaces:Spaces are the basic simulation unit in chipmunk. You add bodies, shapes and joints to a space, and then update the space as a whole.
  • Space: simulation of the most basic unit of chipmunk. Add an object, shape, to a space, and update the space as a whole.
Support

The best way to get support is to visit the chipmunk forums. there are plenty of people around using chipmunk on the just about every platform I 've ever heard. if you are working on a commercial project, howling moon software is also available for contracting.

The best support for chipmunk is the chipmunk Forum. Many developers who use chipmunk apply it to many platforms I have heard. If you work on a commercial project, sign the agreement?

Rigid Bodies, collision shapes and sprites: rigid body, conflicting form and genie

There is often confusion between rigid bodies and their collision shapes in Chipmunk and how they relate to sprites. A sprite wocould be a visual representation of an object, the Sprite is drawn at the position and rotation of the rigid body. collision shapes representation of the object, and how it shoshould collide with other objects. A sprite and collision shape have little to do with one another other than you probably want the collision shape to match the sprite's shape.

People often confuse this rigid body, the relationship between the conflict and the genie. The sprite is a virtual object that is displayed. The sprite draws an object at a position and a rotating rigid body position. The form of conflict represents the conflict between an object and other objects. What needs to be done between the genie and the conflict form is that you may match the conflict form with the sprite shape.


Chipmunk BasicsBasic knowledge of chipmunk


Initialization

Before you do anything else, youMustInitialize chipmunk. Otherwise youWillCrash as soon as the first collision happens.

 
Cpinitchipmunk(); // That is all

Additionally, if you didn't compile chipmunk with the ndebug flag, it will print out the debug mode message and the current version number to stdout.


 

Before you start, you must initialize

Chipmunk, otherwise it will crash and use the above function for initialization. In addition, if you haven't compiled it through ndebug flag

For chipmunk, You need to print the debug mode information and the current stdout version number.

 

 



Memory Management The Chipmunk way

Chipmunk memory management

For details of the structures you will use, chipmunk uses a more or less standard set of memory management functions. For example: For many frameworks you have used,

Chipmunk uses standard memory management functions, such:

 

    • cpspacealloc () allocates but does not initialize a cpspace struct.
    • cpspaceinit (cpspace * space) initializes a cpspace struct.
    • cpspacedestroy (cpspace * space) frees all memory allocated by the cpspaceinit () , but does not free the cpspace struct itself.
    • cpspacenew () allocates and initializes a cpspace struct (CILS cpspacealloc () and cpspaceinit () ).
    • cpspacefree (cpspace * space) cballs cpspacedestroy () and then frees the cpspace struct.

While you will probably use the new/free versions exclusively if you are using C/C ++, but the alloc/init/destroy functions can be helpful when writing language extensions so that you can more easily work with a garbage collector.

In general, you are responsible for freeing any structs that you allocate. Chipmunk does not have any fancy reference counting or garbage collection.

 

When you use C/C ++, you may choose not to use new/free to allocate memory. However, the assignment/initialization/destruction function will greatly help your language extension, making the garbage collection mechanism easier.

Generally, it is your responsibility to use free to destroy any structure you allocate.

Does chipmunk have any reference records and garbage collection? (I didn't understand what it meant) 

 

Math the chipmunk way match

First of all, chipmunk uses double precision floating point numbers throughout it's calculations by default. this is likely to be faster on most modern desktop processors, and means you have to worry less about floating point round off errors. you can change the floating point type used by chipmunk when compiling the library. look in chipmunk. h.

First,

The double-precision floating point number used by default during chipmunk calculation. This is fast for the current desktop processor. It also means that you don't have to worry about the problem. You can change the floating point type when compiling the database. For more information, see

Chipmunk. h

 

 

 

Chipmunk defines a number of aliases for common math functions so that you can choose to use floats or doubles for Chipmunk's floating point type. in your own code, there probably isn't a strong reason to use these aliases unless you should CT you might want to change Chipmunk's floating point type later and a 2% Performance hit from using the wrong float/Double version of math functions will matter.

Chipmunk defines the mathematical functions of some columns, so that you can choose to use single-precision or double-precision floating point types.

In your ownCode, This may not be a big factor to use these functions unless you want to change

The type of the chipmunk floating point number, which is caused by float/double and has a 2% performance loss.

 

 

That said, there are a few functions you will probably find very useful:

 
Cpfloat cpfclamp(Cpfloat F,Cpfloat min,Cpfloat Max)

ClampFTo beMinAndMax.

 
Cpfloat cpflerp(Cpfloat F1,Cpfloat F2,Cpfloat t)

Linearly interpolateF1AndF2Extra line between. F1 and F2

Cpfloat cpflerpconst(Cpfloat F1,Cpfloat F2,Cpfloat d)

Linearly interpolate fromF1TowardsF2ByD.

To represent vectors, chipmunk defines the cpvect type and a set of inline functions for working with them (CPV, cpvadd, cpvmult, etc). See the API reference for more information ..

Chipmunk c api (C language API)


Main APIs

  • Cpvect-create and manipulate 2D vectors. create and operate a 2D Vector
  • Cpbb-create and manipulate 2D axis-aligned Bounding Boxes. create and operate a box bound to a 2D axis
  • Cpbody-create and work with rigid bodies. Create an object with a rigid body attribute
  • Cpshape-Attach collision shapes to rigid bodies. Add a conflicting shape to the rigid body.
  • Cpspace-create a "space" to put your objects into and simulate them. Create a space between objects and simulate them.
  • Cpconstraint-create joints and other constraints. Create a connection and other constraints
  • Learn about how collisiondetection in chipmunk works. Learn how to handle

    Chipmunk conflict

  • Learn about Chipmunk's callbacksystem and cparbiter struct for recieving collision events and adding custom behavior to your physics. The above two structures accept the conflict time and add a custom Physical representation.
  • Learn about queries. Point queries and segment queries (raycasting). Learn how to query and point out the queried parts.
Links (the following are related links)

    • Http://www.slembcke.net/forums/viewforum.php? F = 1-the official forum
    • Games-a list of games done with chipmunk used

      Chipmunk engine game list

    • Bindingsandports-a list of bindings and ports of chipmunk to other ages.

 

This is a good afternoon translation, or let's take a look at the cocos2d application. The result is depressing... K ..

You can only translate and then go to bed .. The MACOs input method is easy to use ..

Note: This document is not a full-text sentence translation. This is probably the meaning. In general. Not important .. If you need it, read it in detail. But this is just an overview... Further analysis and research are required ..

 

 

 

 

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.