Permalink
Browse files

FUCKING COLLISIONS

  • Loading branch information...
1 parent da21885 commit efb031140b586b26299ab3363de2056a5b17cff3 @gnuvince committed Feb 2, 2012
@@ -179,15 +179,15 @@ namespace Application
// Rotations du bateau
Vector3<real> global_position = GetLocalToGlobal() * points[0];
Vector3<real> water_orientation = waterSurface.GetGlobalNormal(global_position);
- t.SetRotationX(atan(water_orientation.Z() / water_orientation.X()));
- t.SetRotationZ(atan(water_orientation.Z() / water_orientation.Y()));
+ t.SetRotationX(water_orientation.X());
+ t.SetRotationZ(water_orientation.Z());
mHull.SetLocalTransformation(t);
if (MainWindow::Get()->IsKeyPressed('w')) {
Transformation<real> t = GetLocalTransformation();
- t.Translate(Vector3<real>(cos(angle - PI/2)*-20*dt, 0, -sin(angle - PI/2)*-20*dt));
+ t.Translate(Vector3<real>(cos(angle - PI/2)*-8*dt, 0, -sin(angle - PI/2)*-8*dt));
SetLocalTransformation(t);
// Update wheels
@@ -202,7 +202,7 @@ namespace Application
if (MainWindow::Get()->IsKeyPressed('s')) {
Transformation<real> t = GetLocalTransformation();
- t.Translate(Vector3<real>(cos(angle - PI/2)*10*dt, 0, -sin(angle - PI/2)*10*dt));
+ t.Translate(Vector3<real>(cos(angle - PI/2)*4*dt, 0, -sin(angle - PI/2)*4*dt));
SetLocalTransformation(t);
// Update wheels
@@ -267,4 +267,8 @@ namespace Application
return BOAT_WIDTH;
}
+ real Boat::GetAngle() const {
+ return angle;
+ }
+
}
@@ -23,6 +23,7 @@ void Animate( const WaterSurface& waterSurface, real time, real dt );
real GetSizeZ() const;
real GetSizeX() const;
+real GetAngle() const;
private:
CoreLib::Cube<real> mHull;
@@ -102,8 +102,8 @@ void Buoy::Animate( const WaterSurface& waterSurface, real time, real dt )
// Orientation
Vector3<real> water_orientation = waterSurface.GetGlobalNormal(global_position);
- t.SetRotationX(atan(water_orientation.Z() / water_orientation.X()));
- t.SetRotationZ(atan(water_orientation.Z() / water_orientation.Y()));
+ t.SetRotationX(water_orientation.X());
+ t.SetRotationZ(water_orientation.Z());
SetLocalTransformation(t);
}
@@ -1,4 +1,14 @@
#include "CollisionHandler.h"
+#include <cmath>
+
+#define INSIDE 0
+#define LEFT 1
+#define RIGHT 2
+#define BOTTOM 4
+#define TOP 8
+
+#define SetBit(tbrl, bit) ((tbrl) |= (bit))
+#define BitIsSet(tbrl, bit) ((tbrl) & (bit))
using namespace CoreLib;
@@ -34,6 +44,83 @@ void CollisionHandler::ResolveCollision( Boat& boat, Buoy& buoy )
// La méthode OnCollide de la classe
// Buoy doit être appelée lorsqu'il y a collision.
///////////////////////////////////////
+
+ int tbrl = INSIDE;
+ real boat_top = -boat.GetSizeZ() / 2;
+ real boat_bot = -boat_top;
+ real boat_right = boat.GetSizeX() / 2;
+ real boat_left = -boat_right;
+
+
+ Vector3<real> buoy_center = boat.GetGlobalToLocal() * (buoy.GetLocalToGlobal() *
+ Vector3<real>(0, 0, 0));
+
+ // Calcul du code TBRL
+ if (buoy_center.Z() < boat_top) SetBit(tbrl, TOP);
+ else if (buoy_center.Z() > boat_bot) SetBit(tbrl, BOTTOM);
+ if (buoy_center.X() > boat_right) SetBit(tbrl, RIGHT);
+ else if (buoy_center.X() < boat_left) SetBit(tbrl, LEFT);
+
+
+
+ real r = buoy.GetRadius();
+
+ int collision = 0;
+
+ if (BitIsSet(tbrl, TOP)) {
+ if (fabs(buoy_center.Z() - boat_top) > r) {
+ collision = 0;
+ }
+ else {
+ if (BitIsSet(tbrl, LEFT))
+ collision = fabs(buoy_center.X() - boat_left) < r;
+ else if (BitIsSet(tbrl, RIGHT))
+ collision = fabs(buoy_center.X() - boat_right) < r;
+ else
+ collision = 1;
+ }
+ }
+
+ else if (BitIsSet(tbrl, BOTTOM)) {
+ if (fabs(buoy_center.Z() - boat_bot) > r) {
+ collision = 0;
+ }
+ else {
+ if (BitIsSet(tbrl, LEFT))
+ collision = fabs(buoy_center.X() - boat_left) < r;
+ else if (BitIsSet(tbrl, RIGHT))
+ collision = fabs(buoy_center.X() - boat_right) < r;
+ else
+ collision = 1;
+ }
+ }
+
+ else {
+ if (BitIsSet(tbrl, LEFT))
+ collision = fabs(buoy_center.X() - boat_left) < r;
+ else if (BitIsSet(tbrl, RIGHT))
+ collision = fabs(buoy_center.X() - boat_right) < r;
+ else
+ collision = 1;
+ }
+
+ if (collision) {
+ real delta = boat.GetSizeZ() / 2 + buoy.GetRadius();
+ buoy.OnCollide();
+ Vector3<real> v;
+ if (BitIsSet(tbrl, TOP))
+ v = Vector3<real>(buoy_center.X(), buoy_center.Y(), -delta);
+ else if (BitIsSet(tbrl, BOTTOM)) {
+ v = Vector3<real>(buoy_center.X(), buoy_center.Y(), delta);
+ }
+ else {
+ v = Vector3<real>(0, 0, -delta);
+ }
+ Vector3<real> v2 = boat.GetLocalToGlobal() * v;
+ Vector3<real> v3 = buoy.GetGlobalToLocal() * v2;
+
+ buoy.GetLocalTransformation().Translate(v3);
+ }
}
void CollisionHandler::ResolveCollision( Buoy& buoy1, Buoy& buoy2 )

0 comments on commit efb0311

Please sign in to comment.