-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduder.cpp
More file actions
85 lines (74 loc) · 2.53 KB
/
duder.cpp
File metadata and controls
85 lines (74 loc) · 2.53 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
#include <iostream>
#include "sdl_compat.h"
#include "duder.h"
#include "geom.h"
#include "defines.h"
#include "physics_world.h"
using namespace std;
Duder::Duder(float x, float y, float width, float height)
:Object(x, y, width, height){
pos.x = x;
pos.y = y;
vel.x = ((rand() % 300) - 150)/50.0;
vel.y = ((rand() % 300) - 150)/50.0;
color = map_rgb(rand()%255, rand()%255, rand()%255);
random_val = rand();
thick = rand() % 10 +1;
is_killed = false;
}
void Duder::initPhysics(PhysicsWorld& world) {
physicsWorldPtr = &world;
// Use DYNAMIC instead of KINEMATIC so duders collide with static bodies
physicsBody = world.createBody(this, PhysicsBodyType::DYNAMIC, 0.5f, 0.0f, 1.0f);
if (b2Body_IsValid(physicsBody)) {
// Disable gravity for duders so they float
b2Body_SetGravityScale(physicsBody, 0.0f);
b2Body_SetFixedRotation(physicsBody, true);
b2Body_SetLinearDamping(physicsBody, 0.0f);
// vel is pixels/frame, convert to pixels/second for physics
world.setLinearVelocity(physicsBody, vel.x * FRAME_RATE, vel.y * FRAME_RATE);
}
}
void Duder::syncFromPhysics() {
if (!physicsWorldPtr || !b2Body_IsValid(physicsBody)) return;
b2Vec2 physPos = physicsWorldPtr->getPosition(physicsBody);
b2Vec2 physVel = physicsWorldPtr->getLinearVelocity(physicsBody);
pos.x = physPos.x;
pos.y = physPos.y;
// Sync velocity back (convert from pixels/second to pixels/frame)
vel.x = physVel.x / FRAME_RATE;
vel.y = physVel.y / FRAME_RATE;
}
void Duder::update(int w, int h){
if (!is_killed){
if (physicsWorldPtr && b2Body_IsValid(physicsBody)) {
syncFromPhysics();
if (pos.x >= w || pos.x <= 0) {
vel.x = -vel.x;
physicsWorldPtr->setLinearVelocity(physicsBody, vel.x * FRAME_RATE, vel.y * FRAME_RATE);
}
if (pos.y >= h || pos.y <= 0) {
vel.y = -vel.y;
physicsWorldPtr->setLinearVelocity(physicsBody, vel.x * FRAME_RATE, vel.y * FRAME_RATE);
}
} else {
pos.x += vel.x;
pos.y += vel.y;
if (pos.x >= w || pos.x <= 0) vel.x = -vel.x;
if (pos.y >= h || pos.y <= 0) vel.y = -vel.y;
}
}
}
void Duder::draw(void){
computeRect();
if (!is_killed){
draw_ellipse(
pos.x, pos.y-height2, width2, height2,
color, thick
);
draw_filled_ellipse(
pos.x, pos.y, width, height2,
color
);
}
}