mirror of
https://github.com/PoetryInCode/simple-rigid-sim.git
synced 2025-04-19 10:17:13 -04:00
fixed wall collision and removed test objects
This commit is contained in:
parent
de95a6317e
commit
7f1713c2d7
26
src/main.cpp
26
src/main.cpp
@ -68,10 +68,10 @@ int main() {
|
|||||||
|
|
||||||
SDL_GetWindowSize(win,&w,&h);
|
SDL_GetWindowSize(win,&w,&h);
|
||||||
|
|
||||||
Vector vec = Vector(0,0);
|
//Vector vec = Vector(0,0);
|
||||||
|
|
||||||
Quad quad = Quad(vec,20,20);
|
//Quad quad = Quad(vec,20,20);
|
||||||
quad.setColor(Color(RED));
|
//quad.setColor(Color(RED));
|
||||||
|
|
||||||
Vector buffer = Vector(0.0,0.0);
|
Vector buffer = Vector(0.0,0.0);
|
||||||
bool change_vector[2] = {false,false};
|
bool change_vector[2] = {false,false};
|
||||||
@ -83,16 +83,16 @@ int main() {
|
|||||||
|
|
||||||
int x1,y1,x2,y2;
|
int x1,y1,x2,y2;
|
||||||
|
|
||||||
Quad rect;
|
//Quad rect;
|
||||||
Circle draw;
|
Circle draw;
|
||||||
|
|
||||||
std::vector<phys_obj> objs;
|
std::vector<phys_obj> objs;
|
||||||
//std::vector<int> yvalues;
|
//std::vector<int> yvalues;
|
||||||
|
|
||||||
Circle c = Circle(Vector(250,250),50);
|
//Circle c = Circle(Vector(250,250),50);
|
||||||
c.setColor(Color(RED));
|
//c.setColor(Color(RED));
|
||||||
|
|
||||||
objs.push_back(phys_obj(c));
|
//objs.push_back(phys_obj(c));
|
||||||
|
|
||||||
while(run) {
|
while(run) {
|
||||||
if(modified) {
|
if(modified) {
|
||||||
@ -194,7 +194,7 @@ int main() {
|
|||||||
for(uint 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));
|
floor_dis = objs[i].obj.center.distanceTo(Vector(objs[i].obj.center.x,h));
|
||||||
objs[i].calculate_vectors(&objs,Vector(w,h));
|
objs[i].calculate_vectors(&objs,Vector(w,h));
|
||||||
if(floor_dis >= objs[i].obj.radius && i != 0) {
|
if(floor_dis >= objs[i].obj.radius) {
|
||||||
objs[i].force(Vector(0,1));
|
objs[i].force(Vector(0,1));
|
||||||
objs[i].translate(objs[i].velocity);
|
objs[i].translate(objs[i].velocity);
|
||||||
printf("velocity of object %i (%f,%f)\n",i,objs[i].velocity.x,objs[i].velocity.y);
|
printf("velocity of object %i (%f,%f)\n",i,objs[i].velocity.x,objs[i].velocity.y);
|
||||||
@ -228,10 +228,10 @@ int main() {
|
|||||||
buffer.y = 0;
|
buffer.y = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.translate(buffer);
|
//c.translate(buffer);
|
||||||
c.render(rend);
|
//c.render(rend);
|
||||||
quad.translate(buffer);
|
//quad.translate(buffer);
|
||||||
quad.render(rend);
|
//quad.render(rend);
|
||||||
SDL_RenderPresent(rend);
|
SDL_RenderPresent(rend);
|
||||||
SDL_Delay(1000/60);
|
SDL_Delay(1000/60);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "phys_obj.h"
|
#include "phys_obj.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
phys_obj::phys_obj(Quad object, Vector init_vel) {
|
phys_obj::phys_obj(Quad object, Vector init_vel) {
|
||||||
@ -128,9 +129,10 @@ void phys_obj::calculate_vectors(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//window border collision detection
|
//window border collision detection
|
||||||
if(ac.x+ao.radius < 0 ) { //left wall collision
|
printf("left collision point %f\n", ac.x-ao.radius);
|
||||||
|
if((ac.x-ao.radius) < 0.0) { //left wall collision
|
||||||
objects[0][a].translate(Vector(
|
objects[0][a].translate(Vector(
|
||||||
ac.xDif(Vector(0,ac.y))+ao.radius,
|
ao.radius-(ac.x-0),
|
||||||
0
|
0
|
||||||
));
|
));
|
||||||
} else if (ac.x+ao.radius > win_dim.x) {//right wall collision
|
} else if (ac.x+ao.radius > win_dim.x) {//right wall collision
|
||||||
@ -140,22 +142,19 @@ void phys_obj::calculate_vectors(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
if(ac.y+ao.radius < 0 ) {//top collision
|
if(ac.y+ao.radius < 0 ) {//top collision
|
||||||
//zero out the y component if it has collided
|
|
||||||
objects[0][a].translate(Vector(
|
objects[0][a].translate(Vector(
|
||||||
0,
|
0,
|
||||||
ac.yDif(Vector(0,ac.y+ao.radius))
|
ac.yDif(Vector(0,ac.y+ao.radius))
|
||||||
));
|
));
|
||||||
} else if (ac.y+ao.radius > win_dim.y) {//bottom collision
|
} else if (ac.y+ao.radius > win_dim.y) {//bottom collision
|
||||||
|
//zero out the y component if it has collided
|
||||||
|
objects[0][a].velocity = Vector(objects[0][a].velocity.x,0);
|
||||||
objects[0][a].contact_floor = true;
|
objects[0][a].contact_floor = true;
|
||||||
objects[0][a].translate(Vector(
|
objects[0][a].translate(Vector(
|
||||||
0,
|
0,
|
||||||
-1*(ac.yDif(Vector(0,win_dim.y-ao.radius)))
|
-1*(ac.yDif(Vector(0,win_dim.y-ao.radius)))
|
||||||
));
|
));
|
||||||
}/* else {
|
}
|
||||||
if(objects[0][a].contact_floor) {
|
|
||||||
objects[0][a].contact_floor = false;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user