collision detection

This commit is contained in:
Solomon W. 2020-06-04 13:48:22 -04:00
parent 4508e7569c
commit 6364a572e5
3 changed files with 125 additions and 42 deletions

View File

@ -4,6 +4,7 @@
#include <SDL2/SDL2_gfxPrimitives.h>
#include "phys_obj.h"
#include "Circle.h"
#include <time.h>
SDL_Window *win = NULL;
SDL_Renderer *rend = NULL;
@ -31,7 +32,7 @@ int init() {
return 0;
} else {
return 1;
}
}
}
uint8_t rt,gt,bt,at;
@ -52,6 +53,7 @@ bool clamp(float *value, float val) {
}
}
clock_t t=0;
void gravity(phys_obj *obj) {
obj[0].velocity = Vector(obj[0].velocity.x,obj[0].velocity.x++);
}
@ -59,7 +61,7 @@ void gravity(phys_obj *obj) {
int main() {
init();
cc = Color(BLACK);
bool run = true;
int w,h;
//int mouseX,mouseY;
@ -90,6 +92,8 @@ int main() {
Circle c = Circle(Vector(250,250),50);
c.setColor(Color(RED));
objs.push_back(phys_obj(c));
while(run) {
if(modified) {
clear(rend);
@ -187,10 +191,11 @@ int main() {
}
}
int floor_dis;
for(int i=0; i<objs.size(); i++) {
for(uint i=0; i<objs.size(); i++) {
floor_dis = objs[i].obj.center.distanceTo(Vector(objs[i].obj.center.x,h));
if(floor_dis >= objs[i].obj.radius) {
objs[i].velocity.y++;
objs[i].calculate_vectors(&objs);
if(floor_dis >= objs[i].obj.radius && i != 0) {
objs[i].force(Vector(0,1));
objs[i].translate(objs[i].velocity);
printf("velocity of object %i (%f,%f)\n",i,objs[i].velocity.x,objs[i].velocity.y);
//objs[i].calculate_vectors(objs);

View File

@ -7,8 +7,12 @@ phys_obj::phys_obj(Quad object, Vector init_vel) {
this->bounds=BoundingBox(object);
}*/
void phys_obj::force(Vector vec) {
this->velocity.x += vec.x;
this->velocity.y += vec.y;
if(vec.x != 0) {
this->velocity.x += vec.x;
}
if(vec.y != 0) {
this->velocity.y += vec.y;
}
}
void phys_obj::render(SDL_Renderer *rend) {
/*
@ -44,48 +48,121 @@ int phys_obj::distanceTo(Circle c) {
}
bool phys_obj::checkCollision(phys_obj o) {
if(distanceTo(o.obj) <= obj.radius) {
if((o.obj.radius + obj.radius) >= distanceTo(o.obj)) {
return true;
} else {
return false;
}
}
void phys_obj::calculate_vectors(std::vector<phys_obj> objects) {
for(int i=0; i<objects.size(); i++) {
Vector step;
int xvel = objects[i].velocity.x;
int yvel = objects[i].velocity.y;
if(xvel != 0) {
if(xvel < 0) {
objects[i].velocity.x++;
step.x = -1;
} else {
if(xvel > 0) {
objects[i].velocity.x--;
step.x = 1;
}
}
}
if(yvel != 0) {
if(yvel < 0) {
objects[i].velocity.y++;
step.y = -1;
} else {
if(yvel > 0) {
objects[i].velocity.y--;
step.y = 1;
}
}
}
objects[i].translate(step);
for(int y=0; y<objects.size(); y++) {
if(y != i) {
}
std::vector<float> getYforces(std::vector<phys_obj> objs) {
std::vector<float> values;
for(uint i=0; i<objs.size(); i++) {
values.push_back(objs[i].velocity.y);
}
return values;
}
std::vector<float> getXforces(std::vector<phys_obj> objs) {
std::vector<float> values;
for(uint i=0; i<objs.size(); i++) {
values.push_back(objs[i].velocity.x);
}
return values;
}
bool noneEq(std::vector<float> nums, float comp) {
bool state = true;
for(uint i=0; i<nums.size(); i++) {
if(nums[i] == comp) {
state = false;
}
}
return state;
}
bool anyEq(std::vector<float> nums, float comp) {
bool state = false;
for(uint i=0; i<nums.size(); i++) {
if(nums[i] == comp) {
state = true;
}
}
return state;
}
bool allEq(std::vector<float> nums, float comp) {
uint state=0;
for(uint i=0; i<nums.size(); i++) {
if(nums[i] == comp) {
state++;
}
}
if(state == nums.size()) {
return true;
} else {
return false;
}
}
void phys_obj::calculate_vectors(std::vector<phys_obj> *objects) {
for(uint i=0; i<objects[0].size(); i++) {
for(uint a=0; a<objects[0].size(); a++) {
if(i != a) {
if(objects[0][i].checkCollision(objects[0][a])) {
printf("%i colliding with %i\n",i,a);
Circle io=objects[0][i].obj,ao=objects[0][a].obj;
Vector ic=io.center,ac=ao.center;
objects[0][i].translate(Vector(
-1*( io.getOverlap(ao)*ic.xDif(ac)/ic.distanceTo(ac)),
-1*( io.getOverlap(ao)*ic.yDif(ac)/ic.distanceTo(ac))
));
printf("resolving overlap\n");
objects[0][a].translate(Vector(
io.getOverlap(ao)*ic.xDif(ac)/ic.distanceTo(ac),
io.getOverlap(ao)*ic.yDif(ac)/ic.distanceTo(ac)
));
}
}
}
}
/*
do {
for(uint i=0; i<objects.size(); i++) {
Vector step, v_buffer=objects[i].velocity;
int xvel = objects[i].velocity.x;
int yvel = objects[i].velocity.y;
if(xvel != 0) {
if(xvel < 0) {
v_buffer.x++;
step.x = -1;
} else { //if xvel > 0
v_buffer.x--;
step.x = 1;
}
}
if(yvel != 0) {
if(yvel < 0) {
v_buffer.y++;
step.y = -1;
} else { //if yvel > 0
v_buffer.y--;
step.y = 1;
}
}
objects[i].translate(step);//translate the object
for(uint a=0; a<objects.size(); a++) {
if(i != a) {//dont check collision with self
if(objects[i].checkCollision(objects[a])) {
//caulculate collision here
}
}
}
}
} while(!allEq(getXforces(objects),0) || !allEq(getYforces(objects),0));
*/
}
void phys_obj::setPosition(Vector vec) {
obj.center = vec;
}
}

View File

@ -15,9 +15,10 @@ class phys_obj {
phys_obj(Circle c, Vector init_vel);
void force(Vector vec);
void render(SDL_Renderer *rend);
static void calculate_vectors(std::vector<phys_obj> objects);
static void calculate_vectors(std::vector<phys_obj> *objects);
void translate(Vector vec);
bool checkCollision(phys_obj o);
float angleTo(phys_obj o);
int distanceTo(Circle c);
void setPosition(Vector vec);
};
};