forked from OpenDDS/OpenDDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BouncingShapeDynamics.cpp
73 lines (61 loc) · 2.1 KB
/
BouncingShapeDynamics.cpp
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
#include "BouncingShapeDynamics.hpp"
#include <math.h>
#include <stdlib.h>
#include <iostream>
static const float PI = 3.1415926535F;
#ifdef WIN32
#define roundf(a) ((a)>0?floor((a)+0.5):ceil((a)-0.5))
#endif
using org::omg::dds::demo::ShapeType;
using org::omg::dds::demo::ShapeTypeDataWriter_var;
BouncingShapeDynamics::BouncingShapeDynamics(int x0, int y0,
const QRect& shapeBounds,
const QRect& constraint,
float angle,
float speed,
const ShapeType& shape,
ShapeTypeDataWriter_var& shapeWriter)
: ShapeDynamics(x0, y0, constraint),
shapeBounds_(shapeBounds),
alpha_(angle),
angle_(angle),
speed_(speed),
shape_(shape),
shapeWriter_(shapeWriter)
{ }
BouncingShapeDynamics::~BouncingShapeDynamics()
{ }
bool
BouncingShapeDynamics::flip() {
bool doflip = false;
if (rand() <= RAND_MAX/2)
doflip = true;
return doflip;
}
void
BouncingShapeDynamics::simulate()
{
pos_.rx() = roundf(pos_.rx() + speed_*cosf(angle_));
pos_.ry() = roundf(pos_.ry() + speed_*sinf(angle_));
if (pos_.x() <= shapeBounds_.width()/2) {
angle_ = this->flip() ? -alpha_ : alpha_;
pos_.rx() = shapeBounds_.width()/2;
}
else if (pos_.x() >= (constraint_.width() - (shapeBounds_.width()/2))) {
angle_ = this->flip() ? (PI + alpha_) : (PI - alpha_);
pos_.rx() = constraint_.width() - shapeBounds_.width()/2;
}
else if (pos_.y() <= shapeBounds_.height()/2) {
angle_ = this->flip() ? alpha_ : PI - alpha_;
pos_.ry() = shapeBounds_.height()/2;
}
else if (pos_.y() >= (constraint_.height() - shapeBounds_.height()/2)) {
angle_ = this->flip() ? (PI+alpha_) : -alpha_;
pos_.ry() = constraint_.height() - shapeBounds_.height()/2;
}
shape_.x = pos_.x();
shape_.y = pos_.y();
shapeWriter_->write(shape_, ::DDS::HANDLE_NIL);
plist_.erase(plist_.begin(), plist_.end());
plist_.push_back(pos_);
}