cocos2d-x3.0 Physics封裝物理引擎

3.0以後最box2d和chipmunk這兩個物理引擎進行了封裝,使用起來非常的便利。

官方鏈接地址:英文版

泰然網:中文教程

offset:重心點
velocity:速度

dadamping:阻尼

rerestitution:彈力

mamaterial:材質

mass:質量
moment:力矩,當他碰到另一個剛體時候 ,會產生一股扭轉力,做旋轉運動
body:剛體,表示物理世界中的抽象實體,附帶有物理屬性
shape:剛體的形狀,同一個body可以附加多個shape 該shape們不會發生碰撞
joint:關節,可以連接>=2個剛體 

1.physicsBody

001./** 創建一個body  mass和moment爲默認值  */
002.static PhysicsBody* create();
003./** 創建一個質量爲mass的body moment爲默認值. */
004.static PhysicsBody* create(float mass);
005./** 創建一個body 併爲mass 和moment賦值 */
006.static PhysicsBody* create(float mass, float moment);
007./**創建一個shape爲圓形的body */
008.static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
009./** 創建一個shape爲四邊形的body. */
010.static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
011./**創建一個動態多邊形剛體,多邊形的頂點存放在Point array[ ]中  示例:Point array[ ]={ point(1,1),point(2,2)}  注意:頂點必須按順時針存放,並且圖形爲凸狀,不能是凹的*/
012.static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
013. 
014./** 創建一個靜態的線狀剛體. */
015.static PhysicsBody* createEdgeSegment(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
016./** 創建一個靜態四邊形剛體. */
017.static PhysicsBody* createEdgeBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1const Point& offset = Point::ZERO);
018./** 創建一個靜態多邊形剛體. */
019.static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
020./** 創建一個鏈條狀剛體 */
021.static PhysicsBody* createEdgeChain(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
022. 
023./*
024.添加一個shape  mass和moment賦值true
025.*/
026.virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
027./*通過shape移除shape*/
028.void removeShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
029./*通過tag移除shape*/
030.void removeShape(int tag, bool reduceMassAndMoment = true);
031./* 移除body的所有shape */
032.void removeAllShapes(bool reduceMassAndMoment = true);
033./* 獲取body的shapes */
034.inline const Vector<PhysicsShape*>& getShapes() const return _shapes; }
035./* 獲取第一個shape. */
036.inline PhysicsShape* getFirstShape() const return _shapes.size() >= 1 ? _shapes.at(0) : nullptr; }
037./* 通過tag從body中獲取shape */
038.PhysicsShape* getShape(int tag) const;
039. 
040./**給body施加一個循序漸進的力,物體會受加速度影響,越來越快,像火車一樣*/
041.virtual void applyForce(const Vect& force);
042./** offset爲偏移度 指碰到物體時 body旋轉 偏移 一般設爲默認值 值越大 旋轉越快 偏移角度越大*/
043.virtual void applyForce(const Vect& force, const Point& offset);
044./** 重置施加在body上的力  清0了. */
045.virtual void resetForces();
046./** 不會產生力,直接與body的速度疊加 產生新的速度. */
047.virtual void applyImpulse(const Vect& impulse);
048./** Applies a continuous force to body. */
049.virtual void applyImpulse(const Vect& impulse, const Point& offset);
050./**施加一個扭轉力到剛體上  就像向前翻轉一塊大石頭一樣. */
051.virtual void applyTorque(float torque);
052. 
053./** 設置剛體的速度*/
054.virtual void setVelocity(const Vect& velocity);
055./** 獲取剛體的速度 */
056.virtual Point getVelocity();
057./** 設置剛體角速度 就是單位時間內轉動的弧度*/
058.virtual void setAngularVelocity(float velocity);
059./** 通過一個局部點獲取剛體的角速度*/
060.virtual Point getVelocityAtLocalPoint(const Point& point);
061./** 通過世界點獲取剛體的角速度*/
062.virtual Point getVelocityAtWorldPoint(const Point& point);
063./** 獲取剛體的角速度 */
064.virtual float getAngularVelocity();
065./** 設置速度的極限值*/
066.virtual void setVelocityLimit(float limit);
067./**獲取速度的極限值 */
068.virtual float getVelocityLimit();
069./** 設置角速度極限值 */
070.virtual float getAngularVelocityLimit();
071. 
072./** 從world中移除body */
073.void removeFromWorld();
074. 
075./** 獲取world */
076.inline PhysicsWorld* getWorld() const return _world; }
077./**獲取body的所有關節 */
078.inline const std::vector<PhysicsJoint*>& getJoints() const return _joints; }
079. 
080./** 取得body設置的sprite. */
081.inline Node* getNode() const return _node; }
082. 
083./**
084.* A mask that defines which categories this physics body belongs to.
085.* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
086.* The default value is 0xFFFFFFFF (all bits set).
087.*/沒搞懂
088.void setCategoryBitmask(int bitmask);
089./**
090.* A mask that defines which categories of bodies cause intersection notifications with this physics body.
091.* When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
092.* The default value is 0x00000000 (all bits cleared).
093.*/
094.void setContactTestBitmask(int bitmask);
095./**
096.* A mask that defines which categories of physics bodies can collide with this physics body.
097.* When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
098.* The default value is 0xFFFFFFFF (all bits set).
099.*/
100.void setCollisionBitmask(int bitmask);
101./** get the category bit mask */
102.inline int getCategoryBitmask() const return _categoryBitmask; }
103./** get the contact test bit mask */
104.inline int getContactTestBitmask() const return _contactTestBitmask; }
105./** get the collision bit mask */
106.inline int getCollisionBitmask() const return _collisionBitmask; }
107. 
108./**
109.* set the group of body
110.* Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index)
111.* it have high priority than bit masks
112.*/
113.void setGroup(int group);
114./** get the group of body */
115.inline int getGroup() const return _group; }
116. 
117./** 獲取body座標 */
118.Point getPosition() const;
119./** 獲取body角度. */
120.float getRotation() const;
121. 
122./**判斷body是否靜止*/
123.inline bool isDynamic() const return _dynamic; }
124./**設置body狀態  false爲靜態 true爲動態*/
125.void setDynamic(bool dynamic);
126. 
127./**設置mass值 如果需要增加mass  有addmass方法  不要在這裏做加減 */
128.void setMass(float mass);
129./** 取得mass. */
130.inline float getMass() const return _mass; }
131./**
132.* @brief add mass to body.
133.* if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
134.* if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
135.* if mass == -PHYSICS_INFINITY, _mass will not change.
136.* if mass + _mass <= 0, _mass will equal to MASS_DEFAULT(1.0)
137.* other wise, mass = mass + _mass;
138.*/增加質量
139.void addMass(float mass);
140. 
141./**
142.* @brief set the body moment of inertia.
143.* @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
144.*/設置力矩
145.void setMoment(float moment);
146./** 獲取慣性的力矩. */
147.inline float getMoment(float moment) const return _moment; }
148./**
149.* @brief add moment of inertia to body.
150.* if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
151.* if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
152.* if moment == -PHYSICS_INFINITY, _moment will not change.
153.* if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
154.* other wise, moment = moment + _moment;
155.*/增加力矩
156.void addMoment(float moment);
157./** 取得線性阻尼 */
158.inline float getLinearDamping() const return _linearDamping; }
159./**
160.* 設置阻尼值
161.*它用來模擬body在氣體或者液體中的摩擦力
162.*取值範圍是 0.0f to 1.0f.
163.*/
164.inline void setLinearDamping(float damping) { _linearDamping = damping; }
165./** 獲取角阻尼 */
166.inline float getAngularDamping() const return _angularDamping; }
167./**
168.* 設置角阻尼
169.* 它用來模擬body在氣體或者液體中的角阻尼
170.* the value is 0.0f to 1.0f.
171.*/
172.inline void setAngularDamping(float damping) { _angularDamping = damping; }
173. 
174./** 判斷body是否是 休息狀態 */
175.bool isResting() const;
176./**
177.*判斷body能否在物理世界中模擬
178.*/
179.inline bool isEnabled() const return _enable; }
180./**
181.設置body能否在物理世界中模擬
182.*/
183.void setEnable(bool enable);
184. 
185./** whether the body can rotation */
186.inline bool isRotationEnabled() const return _rotationEnable; }
187./**設置能否旋轉*/
188.void setRotationEnable(bool enable);
189. 
190./** 判斷body是否受引力影響 */
191.inline bool isGravityEnabled() const return _gravityEnable; }
192./** 設置body是否受引力影響 */
193.void setGravityEnable(bool enable);
194. 
195./** 取得body 的tag值 */
196.inline int getTag() const return _tag; }
197./** 設置body tag值*/
198.inline void setTag(int tag) { _tag = tag; }
199. 
200./** 轉換 世界點 到 局部點  類似 世界座標和 局部座標的轉換*/
201.Point world2Local(const Point& point);
202./** 轉換局部座標到 世界座標 */
203.Point local2World(const Point& point);

2.PhysicsShape

01./** 通過shape 取得body */
02.inline PhysicsBody* getBody() const return _body; }
03./** 返回shape的類型 */
04.inline Type getType() const return _type; }
05./** 返回shape的面積 */
06.inline float getArea() const return _area; }
07./** 取得moment 力矩 */
08.inline float getMoment() const return _moment; }
09./** Set moment, it will change the body's moment this shape attaches */
10.void setMoment(float moment);//設置力矩
11.inline void setTag(int tag) { _tag = tag; }//設置標籤tag
12.inline int getTag() const return _tag; }//取得tag標籤
13. 
14./**獲取質量 */
15.inline float getMass() const return _mass; }
16./** Set mass, it will change the body's mass this shape attaches */                                        
17.void setMass(float mass);//設置質量
18.inline float getDensity() const return _material.density; }//density爲密度
19.void setDensity(float density);//獲取密度
20.inline float getRestitution() const return _material.restitution; }//獲取彈性
21.void setRestitution(float restitution);//設置彈性
22.inline float getFriction() const return _material.friction; }//friction爲摩擦力
23.void setFriction(float friction);//設置摩擦力
24.const PhysicsMaterial& getMaterial() const return _material; }//Material爲材質
25.void setMaterial(const PhysicsMaterial& material);設置材質
26. 
27./** 返回默認力矩  其值爲0  */
28.virtual float calculateDefaultMoment() { return 0.0f; }
29./** 取得重心 初始值爲zero */
30.virtual Point getOffset() { return Point::ZERO; }
31./** 獲取shape的重心點  */
32.virtual Point getCenter() { return getOffset(); }
33./**shape是否包含該點 */
34.bool containsPoint(const Point& point) const;
35. 
36./** 改變重心點 */
37.static void recenterPoints(Point* points, int count, const Point& center = Point::ZERO);
38./** 取得多邊形的重心點 */
39.static Point getPolyonCenter(const Point* points, int count);
40. 
41./**
42.* A mask that defines which categories this physics body belongs to.
43.* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
44.* The default value is 0xFFFFFFFF (all bits set).
45.*/
46.inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
47.inline int getCategoryBitmask() const return _categoryBitmask; }
48./**
49.* A mask that defines which categories of bodies cause intersection notifications with this physics body.
50.* When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
51.* The default value is 0x00000000 (all bits cleared).
52.*/
53.inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }
54.inline int getContactTestBitmask() const return _contactTestBitmask; }
55./**
56.* A mask that defines which categories of physics bodies can collide with this physics body.
57.* When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
58.* The default value is 0xFFFFFFFF (all bits set).
59.*/
60.inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
61.inline int getCollisionBitmask() const return _collisionBitmask; }
62. 
63.void setGroup(int group);
64.inline int getGroup() { return _group; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章