Skip to content

Commit

Permalink
all source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Nesterenko committed Apr 4, 2015
0 parents commit ebeb4dc
Show file tree
Hide file tree
Showing 34 changed files with 3,170 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CollisionDefines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#define GRAVITATION 0.3
#define DRAGGING 0.999999
#define BOUNCE 0.5
#define FRICTION 0.1

#define WORLD_LEFT 0
#define WORLD_TOP 0
#define WORLD_RIGHT 800
#define WORLD_BOTTOM 600


#define COLLISION_NONE 0
#define COLLISION_AXIS 1
#define COLLISION_OTHER 2
8 changes: 8 additions & 0 deletions Edges.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once



#define EDGE_ID_OFF 0
#define EDGE_ID_INTERESTING 1
#define EDGE_ID_SOLID 2

20 changes: 20 additions & 0 deletions EditMap.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EditMap", "EditMap.vcproj", "{E9423B38-C119-40D9-93B1-858DB43B8CA5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E9423B38-C119-40D9-93B1-858DB43B8CA5}.Debug|Win32.ActiveCfg = Debug|Win32
{E9423B38-C119-40D9-93B1-858DB43B8CA5}.Debug|Win32.Build.0 = Debug|Win32
{E9423B38-C119-40D9-93B1-858DB43B8CA5}.Release|Win32.ActiveCfg = Release|Win32
{E9423B38-C119-40D9-93B1-858DB43B8CA5}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added EditMap.suo
Binary file not shown.
122 changes: 122 additions & 0 deletions GraphicDynamicObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "GraphicDynamicObject.h"

GraphicDynamicObject::GraphicDynamicObject()
: GraphicObject()
{
TYPE_ID = GRAPHIC_DYNAMIC_OBJECT_ID;

this->old_pos = pos;
speed = max_speed = 0;

tile = NULL;
}

GraphicDynamicObject::GraphicDynamicObject(const char *texture_name, hgeVector pos, short xw, short yw, short layer)
: GraphicObject(texture_name, pos, xw, yw, layer)
{
TYPE_ID = GRAPHIC_DYNAMIC_OBJECT_ID;

this->old_pos = pos;
speed = max_speed = 0;

tile = NULL;

}

GraphicDynamicObject::~GraphicDynamicObject()
{
}

void GraphicDynamicObject::Verlet()
{
float d = DRAGGING;
float g = GRAVITATION;

float px, py;

float ox = old_pos.x; //we can't swap buffers since mcs/sticks point directly to vector2s..
float oy = old_pos.y;

old_pos.x = px = pos.x; //get vector values
old_pos.y = py = pos.y; //pos = position
//old_pos = old position

//integrate
pos.x += (d*px) - (d*ox);
pos.y += (d*py) - (d*oy) + g;
}

//(px,py) is projection vector, (dx,dy) is surface normal
void GraphicDynamicObject::ReportCollision(float px, float py, float dx, float dy, TileMap *tile)
{
//calc velocity
float vx = pos.x - old_pos.x;
float vy = pos.y - old_pos.y;

//find component of velocity parallel to collision normal
float dp = (vx*dx + vy*dy);
float nx = dp*dx;//project velocity onto collision normal

float ny = dp*dy;//nx,ny is normal velocity

float tx = vx-nx;//px,py is tangent velocity
float ty = vy-ny;

//we only want to apply collision response forces if the object is travelling into, and not out of, the collision
float b,bx,by,f,fx,fy;
if(dp < 0)
{
f = FRICTION;
fx = tx * f;
fy = ty * f;

b = 1 + BOUNCE;//this bounce constant should be else where, i.e inside the object/tile/etc..

bx = (nx * b);
by = (ny * b);
}
else
{
//moving out of collision, do not apply forces
bx = by = fx = fy = 0;
}

pos.x += px;//project object out of collision
pos.y += py;

old_pos.x += px + bx + fx;//apply bounce+friction impulses which alter velocity
old_pos.y += py + by + fy;
}

void GraphicDynamicObject::Move(int move)
{
float fx = 0;
float fy = 0;

switch (move)
{
case MOVE_LEFT:
fx -= speed;
break;

case MOVE_RIGHT:
fx += speed;
break;

case MOVE_UP:
fy -= speed + GRAVITATION;
break;

case MOVE_DOWN:
fy += speed;
break;
}

float vx = pos.x - old_pos.x;
float newx = min(max_speed, max(-max_speed, vx+fx));
pos.x = old_pos.x + newx;

float vy = pos.y - old_pos.y;
float newy = min(max_speed, max(-max_speed, vy+fy));
pos.y = old_pos.y + newy;
}
40 changes: 40 additions & 0 deletions GraphicDynamicObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "GraphicObject.h"
#include "CollisionDefines.h"

#define MOVE_LEFT 1
#define MOVE_RIGHT 2
#define MOVE_UP 3
#define MOVE_DOWN 4

class TileMap;
class World;

class GraphicDynamicObject: public GraphicObject
{
friend World;
public:
GraphicDynamicObject();
GraphicDynamicObject(const char *texture_name, hgeVector pos, short xw, short yw, short layer);
virtual ~GraphicDynamicObject();

virtual void Verlet();
virtual void ReportCollision(float px, float py, float dx, float dy, TileMap *tile);

virtual void Move(int move);

virtual void Render() = 0;

void SetSpeed(float speed) { this->speed = this->max_speed = speed; }
void SetSpeed(float speed, float max_speed) { this->speed = speed; this->max_speed = max_speed; }

virtual int Update() = 0;

protected:
hgeVector old_pos;
float speed, max_speed;

TileMap *tile;
//TileMap *prev_tile;
};
46 changes: 46 additions & 0 deletions GraphicObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "GraphicObject.h"



HGE *GraphicObject::hge = NULL;
hgeResourceManager *GraphicObject::resources_manager = NULL;

GraphicObject::GraphicObject()

: xw(0)
, yw(0)
{
TYPE_ID = GRAPHIC_OBJECT_ID;
hge = hgeCreate(HGE_VERSION);

layer = 0;

pos.x = 0;
pos.y = 0;
}

GraphicObject::GraphicObject(const char *texture_name, hgeVector pos, short xw, short yw, short layer)
{
if (texture_name)
{
resource_name = texture_name;
}

TYPE_ID = GRAPHIC_OBJECT_ID;
hge = hgeCreate(HGE_VERSION);

this->layer = layer;

this->pos = pos;
this->xw = abs(xw);
this->yw = abs(yw);
}

GraphicObject::~GraphicObject()
{
hge->Release();

// world->RemoveObject(this);

resource_name.clear();
}
52 changes: 52 additions & 0 deletions GraphicObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "Object.h"

#include <hge.h>
#include <hgevector.h>
#include <hgeresource.h>
#include <string>

class GraphicObject: public Object
{
public:
GraphicObject();
GraphicObject(const char *resource_name, hgeVector pos, short xw, short yw, short layer);

virtual ~GraphicObject();

void SetResName(char *name) { resource_name = name; }
const char *GetResName() { return resource_name.c_str(); }

void SetPos(hgeVector pos) { this->pos = pos; }
void SetPos(float x, float y) { this->pos.x = x; this->pos.y = y; }
hgeVector GetPos() { return pos; }

void SetWidth(short width) { this->xw = width; }
short GetWidth() const { return xw; }

void SetHeight(short height) { this->yw = height; }
short GetHeight() const { return yw; }

void SetLayer(short layer) { this->layer = layer; }
short GetLayer() { return layer; }


virtual void UpdateGraphic() { /*empty*/ }

virtual void Render() = 0;

static void SetResourceManager(hgeResourceManager *rm) { resources_manager = rm; }

protected:
hgeVector pos;
short xw, yw;
short layer; //óðîâåíü ðèñîâàíèÿ è ñòîëêíîâåíèÿ

std::string resource_name;

static HGE *hge;

static hgeResourceManager *resources_manager;

};
Loading

0 comments on commit ebeb4dc

Please sign in to comment.