Repository navigation
Expand file tree
/
Copy pathmotionmanager.cpp
More file actions
96 lines (75 loc) · 2.63 KB
/
Copy pathmotionmanager.cpp
File metadata and controls
96 lines (75 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<motionmanager.h>
PsyEntity psyEntitys[100];//实体状态对象的数组
float32 timeStep=1.0f/60.0f;//仿真世界每次计算间隔时间
int32 velocityIterations = 6;//box2d速度计算迭代次数
int32 positionIterations = 2;//box2d位置计算迭代次数
MotionManager::MotionManager(){
build();//初始构造
}
MotionManager::~MotionManager(){
delete world;
}
//
void MotionManager::build(){
b2Vec2 gravity(0.0f,0.0f);//重力,俯视视角,重力设为零
world=(new b2World(gravity));
count=0;
addBoundary(-0.1f,-0.1f,0.1f,20.2f);//添加边界实体
addBoundary(-0.1f,-0.1f,12.2f,0.1f);
addBoundary(0.0f,20.0f,12.1f,0.1f);
addBoundary(12.0f,-0.1f,0.1f,20.2f);
addBall(2.0f,12.0f,2.0f,-3.0f,0.6f);//添加冰球
//addBall(0.8f,1.0f,-0.1f,0.0f,0.06f);
}
//获取实体数量的方法
int MotionManager::getBodyAmount(){
return count;
}
//添加边界的方法
void MotionManager::addBoundary(float x,float y,float width,float height){
b2BodyDef boundaryBodyDef;
boundaryBodyDef.position.Set(x+width/2,y+height/2);
b2Body* boundaryBody=(world->CreateBody(&boundaryBodyDef));
b2PolygonShape boundaryShape;
boundaryShape.SetAsBox(width,height);
b2FixtureDef boundaryFixtureDef;
boundaryFixtureDef.shape=&boundaryShape;
boundaryFixtureDef.density=1;
boundaryFixtureDef.restitution=0.8;
boundaryBody->CreateFixture(&boundaryFixtureDef);
PsyEntity psyEntity(2,boundaryBody);
psyEntity.w=width;
psyEntity.h=height;
psyEntitys[count]=psyEntity;
count++;
}
//添加球的方法
void MotionManager::addBall(float x,float y,float vx,float vy,float r){
b2BodyDef ballBodyDef;
ballBodyDef.position.Set(x,y);
ballBodyDef.type=b2_dynamicBody;
ball=world->CreateBody(&ballBodyDef);
b2Vec2 velocity(vx,vy);
ball->SetLinearVelocity(velocity);
b2CircleShape ballShape;
ballShape.m_p.Set(r,-r);
ballShape.m_radius=r;
b2FixtureDef ballFixtureDef;
ballFixtureDef.shape=&ballShape;
ballFixtureDef.density=1.0f;//密度
ballFixtureDef.friction=0.0f;
ballFixtureDef.restitution=0.7;//弹性碰撞恢复系数
ball->CreateFixture(&ballFixtureDef);
PsyEntity ballEntity(1,ball);
ballEntity.r=r;
psyEntitys[count]=ballEntity;
count++;
}
//获取实体对象的方法
PsyEntity* MotionManager::getEneity(int i){
return &psyEntitys[i];
};
//仿真世界向前演算的方法,调用Box2的step方法
void MotionManager::step(){
world->Step(timeStep,velocityIterations,positionIterations);
}