aboutsummaryrefslogtreecommitdiffstats
path: root/yage/physics/particlebody.cpp
blob: 49520558c290930f03c0a00e78fb77e42eedda82 (plain)
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
/** ---------------------------------------------------------------------------
 * -*- c++ -*-
 * @file: particlebody.cpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

#include <yage/physics/particlebody.h>

#include <cmath>
#include <iostream>

namespace yage
{

ParticleBody::ParticleBody(const Vector2d &position, double mass,
                           const Vector2d &velocity, bool gravity)
    : Body(position, mass, velocity, gravity)
{
}

void ParticleBody::applyForce(const Vector2d &force)
{
    force_ += force;
}

void ParticleBody::update()
{
    // set the time_step for 60fps
    double time_step = 1.0 / 60.0;

    // set the last acceleration
    Vector2d last_acceleration = acceleration_;

    // update the position of the body
    position_ += velocity_ * time_step +
                 (0.5 * last_acceleration * std::pow(time_step, 2));

    // update the acceleration
    if (gravity_) {
        acceleration_ =
            Vector2d(force_.x() / mass_, (GRAVITY + force_.y()) / mass_);
    } else {
        acceleration_ = Vector2d(force_.x() / mass_, force_.y() / mass_);
    }

    Vector2d avg_acceleration = (acceleration_ + last_acceleration) / 2.0;

    // update the velocity of the body
    velocity_ += avg_acceleration * time_step;
}

} // namespace yage