aboutsummaryrefslogtreecommitdiff
path: root/Simulation/Tank.cpp
blob: 7d0c6d1c46ecd1678792305463274cbf17cd7a24 (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
#include "Tank.hpp"
#include "SimBase.hpp"

void Tank::create(SimBase *sim)
{
    hSim = sim;

    // Prepare tank graphics
    setCenterRad(worldEdges, hSim->prms.worldRad);
    worldEdges.setPointCount(360);
    worldEdges.setFillColor(hSim->prms.worldColor);
    worldEdges.setOutlineColor(hSim->prms.zapperColor);
    worldEdges.setOutlineThickness(hSim->prms.worldRad / 100.f);

    // Prepare tank physics
    world.SetAllowSleeping(true);
    contactListener.hSim = hSim;
    world.SetContactListener(&contactListener);

    // Create edge of the world
    b2BodyDef worldDef;
    worldDef.type = b2_staticBody;

    tankEdge = world.CreateBody(&worldDef);

    b2ChainShape shape;
    b2Vec2 vrtx[360];
    for (int dgr = 0; dgr < 360; ++dgr)
    {
        vrtx[dgr].Set(cosf((float)dgr / Params::RAD_DGRS), sinf((float)dgr / Params::RAD_DGRS)),
        vrtx[dgr] *= hSim->prms.worldRad;
    }
    shape.CreateLoop(vrtx, 360);

    b2FixtureDef worldFix;
    worldFix.shape = &shape;
    worldFix.friction = 1.f;

    tankEdge->CreateFixture(&worldFix);
}


void Tank::updateAllPhysics()
{
    world.Step(1.f / 60.f, 8, 3);
}


void Tank::draw()
{
    hSim->window.draw(worldEdges);
}