mirror of
https://github.com/PoetryInCode/simple-rigid-sim.git
synced 2025-06-09 19:03:09 -04:00
window border collision, and other object collision
This commit is contained in:
parent
009bc6cc1f
commit
356f646f95
@ -193,13 +193,13 @@ int main() {
|
||||
int floor_dis;
|
||||
for(uint i=0; i<objs.size(); i++) {
|
||||
floor_dis = objs[i].obj.center.distanceTo(Vector(objs[i].obj.center.x,h));
|
||||
objs[i].calculate_vectors(&objs);
|
||||
objs[i].calculate_vectors(&objs,Vector(w,h));
|
||||
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);
|
||||
} else {
|
||||
}/* else {
|
||||
if(objs[i].velocity.y != 0) {
|
||||
printf("Zeroing object %i\n",i);
|
||||
objs[i].velocity.y = 0;
|
||||
@ -209,7 +209,7 @@ int main() {
|
||||
objs[i].obj.y = h-objs[i].obj.radius;
|
||||
printf("Moving %i off of border to (%i,%i)\n",i,objs[i].obj.x,objs[i].obj.y);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
objs[i].render(rend);
|
||||
}
|
||||
if(change_vector[0]) {
|
||||
|
@ -105,25 +105,57 @@ bool allEq(std::vector<float> nums, float comp) {
|
||||
}
|
||||
}
|
||||
|
||||
void phys_obj::calculate_vectors(std::vector<phys_obj> *objects) {
|
||||
void phys_obj::calculate_vectors(
|
||||
std::vector<phys_obj> *objects,
|
||||
Vector win_dim
|
||||
) {
|
||||
for(uint i=0; i<objects[0].size(); i++) {
|
||||
Circle io=objects[0][i].obj;
|
||||
Vector ic=io.center;
|
||||
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(
|
||||
Circle ao=objects[0][a].obj;
|
||||
Vector ac=ao.center;
|
||||
if(i != a) { //dont check collision against self
|
||||
if(objects[0][i].checkCollision(objects[0][a])) {//check collision
|
||||
objects[0][i].translate(Vector(//move object out of other object
|
||||
-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)
|
||||
));
|
||||
}
|
||||
}
|
||||
//window border collision detection
|
||||
if(ac.x+ao.radius < 0 ) { //left wall collision
|
||||
objects[0][a].translate(Vector(
|
||||
ac.xDif(Vector(0,ac.y))+ao.radius,
|
||||
0
|
||||
));
|
||||
} else if (ac.x+ao.radius > win_dim.x) {//right wall collision
|
||||
objects[0][a].translate(Vector(
|
||||
-1*(ac.xDif(Vector(win_dim.x,0))+ao.radius),
|
||||
0
|
||||
));
|
||||
}
|
||||
if(ac.y+ao.radius < 0 ) {//top collision
|
||||
//zero out the y component if it has collided
|
||||
objects[0][a].translate(Vector(
|
||||
0,
|
||||
ac.yDif(Vector(0,ac.y+ao.radius))
|
||||
));
|
||||
} else if (ac.y+ao.radius > win_dim.y) {//bottom collision
|
||||
objects[0][a].contact_floor = true;
|
||||
objects[0][a].translate(Vector(
|
||||
0,
|
||||
-1*(ac.yDif(Vector(0,win_dim.y-ao.radius)))
|
||||
));
|
||||
}/* else {
|
||||
if(objects[0][a].contact_floor) {
|
||||
objects[0][a].contact_floor = false;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
@ -5,17 +5,15 @@
|
||||
class phys_obj {
|
||||
public:
|
||||
Vector velocity;
|
||||
//Quad obj;
|
||||
Circle obj;
|
||||
BoundingBox bounds;
|
||||
bool contact_floor;
|
||||
phys_obj();
|
||||
//phys_obj(Quad object);
|
||||
//phys_obj(Quad object, Vector init_vel);
|
||||
phys_obj(Circle c);
|
||||
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, Vector win_dim);
|
||||
void translate(Vector vec);
|
||||
bool checkCollision(phys_obj o);
|
||||
float angleTo(phys_obj o);
|
||||
|
Loading…
x
Reference in New Issue
Block a user