After less than one week – weekend not counted in (I have had over 24hrs of sleep during weekend, I really hate to be sick) I have cought up with university material. Material covered:
- Tile system for map loaded from a file
- offset for moving map
- entity manager for all objects on the map – plus improvement
- messaging system –Â communication between instances of objects
- collision detection – and my own improvement
- moving around the map – mouse based
- font manager
Plans for next week
- Selecting objects by mouse click – hoping to select more than one element
- collision detection applied for object on a map
- placing building on the map
- shooting to zombies and other stuff.
Meanwhile I will post some code – I haven’t done that for a while.
Collision detection class with improvements
// Collision.h
#pragma once
#ifndef COLLISION_H
#define COLLISION_H
const float TOUCH_DISTANCE = 0.000000000001;
static enum CollisionResults {
NO_COLLISION, TOUCHING, OVERLAPPING
};
struct Circle{
D3DXVECTOR3& position;
float radius;
};
struct BoundingBox{
D3DXVECTOR3& position;
D3DXVECTOR3& size;
};
bool TestCollision(const BoundingBox& a, const BoundingBox& b);
CollisionResults TestCollisionCircle(const Circle& a, const Circle& b);
#endif
Pretty easy stuff here – two structs for BoundingBox and Circle enum for collision between circles – I have added it because I want to base selecting objects and – well all collision on two circles.
// Collision.cpp
#include "Collision.h"
bool TestCollision(const BoundingBox& a, const BoundingBox& b){
float t;
if((t = a.position.x - b.position.x) > b.size.x || -t > a.size.x)
return false;
if((t = a.position.y - b.position.y) > b.size.y || -t > a.size.y)
return false;
if((t = a.position.z - b.position.z) > b.size.z || -t > a.size.z)
return false;
return true;
}
CollisionResults TestCollisionCircle(const Circle& a, const Circle& b){
//for math
CollisionResults colliding;
float distance_squared;
float radii_squared;
//a*a + b*b = c*c
distance_squared = ((a.position.x - b.position.x)* (a.position.x - b.position.x))+
((a.position.y - b.position.y)* (a.position.y - b.position.y));
//Multiplication is faster than taking a square root
radii_squared = (a.radius + b.radius) * (a.radius + b.radius);
if( -TOUCH_DISTANCE < radii_squared - distance_squared &&radii_squared - distance_squared < TOUCH_DISTANCE)
colliding = TOUCHING;
else if(radii_squared > distance_squared)
colliding = OVERLAPPING;
else
colliding = NO_COLLISION;
return colliding;
}
pretty simple stuff here as well – Pythagorean theorem based. if distance between two points is bigger than sum of radius of circles then there is no collision if it is equal there is a collision but if it is smaller they overlap – so we have covered all 3 states first two for collision detection on the map and third one for selecting. simple
🙂