-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphysics.cpp
39 lines (34 loc) · 1.18 KB
/
physics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "physics.h"
#include "math3.h"
#include "collidables.h"
#include "irrlicht.h"
void SphereCollisionResponse (Sphere& sph1, Sphere& sph2, float e){
irr::core::vector3df normal=sph2.cm-sph1.cm;
float dist=sph1.cm.distance(sph2.cm);
float penetration=sph1.R+sph2.R-dist;
normal.normalize();
float distcenter1 = sph1.cm.distance(Point(0,0,0));
float distcenter2 = sph2.cm.distance(Point(0,0,0));
if (distcenter2>distcenter1) sph1.cm=sph1.cm+penetration*normal;
else sph2.cm=sph2.cm+penetration*normal;
irr::core::vector3df relative=sph1.speed-sph2.speed;
float vdotn = normal.dotProduct(relative);
float j=-((1+e)*vdotn)/2;
sph1.speed+=j*normal;
sph2.speed-=j*normal;
}
void checkSphereCollision (Sphere& sph1, Sphere& sph2, float e){
if (sph1.cm.distance(sph2.cm)<=sph1.R+sph2.R) SphereCollisionResponse(sph1,sph2,e);
}
void BallToWallCheck(Sphere& sph,float size){
if (sph.cm.x+sph.R>size) sph.speed.X*=-1;
if (sph.cm.y+sph.R>size) sph.speed.Y*=-1;
if (sph.cm.z+sph.R>size) sph.speed.Z*=-1;
}
void UpdateSpherePos(Sphere& sph, float dt){
sph.cm=sph.cm+sph.speed*dt;
}
void checkMolCollision(Molecule3& m1, Molecule3& m2, float e){
if (m1.cm.distance(m2.cm)<m1.R+m2.R){
}
}