/* ************************************************************************** */ /* _____ _ __ ___ __ */ /* RigidBody.cpp | ___(_)/ _| ___ ( _ ) / /_ */ /* Project: C-Engine | |_ | | |_ / _ \/ _ \| '_ \ */ /* | _| | | _| __/ (_) | (_) | */ /* Author: Fife |_| |_|_| \___|\___/ \___/ */ /* */ /* ************************************************************************** */ #include "..\C-Engine\include\Object\RigidBody.hpp" RigidBody::RigidBody(Object * Pointeur) { usefixebody = false; widht = 0; height = 0; Conteneur = Pointeur; R_body.transform = Conteneur->GetTransform(); R_body.velocity.x = 0; R_body.velocity.y = 0; R_body.acceleration.x = 0; R_body.acceleration.y = 0; R_body.force.x = 0; R_body.force.y = 0; R_body.mass_data.mass = 0; R_body.mass_data.inv_mass = 0; } void RigidBody::SetMass(float v) { R_body.mass_data.mass = v; if(v) R_body.mass_data.inv_mass = 1 / v; else R_body.mass_data.inv_mass = 0; } void RigidBody::UseFixeBody(int w , int h) { usefixebody = true; widht = w; height = h; } void RigidBody::UseFlexBody() { usefixebody = false; widht = 0; height = 0; } int RigidBody::GetHeight() { return height; } int RigidBody::GetWidht() { return widht; } bool RigidBody::GetStat() { return usefixebody; } Body * RigidBody::GetBody() { return &R_body; } void RigidBody::AppliedForce() { if(R_body.mass_data.mass > 0) { #ifdef CE_PLATEFORME R_body.force.y = -10 * R_body.mass_data.mass * R_body.mass_data.mass ; #endif // CE_PLATEFORME R_body.acceleration.x = R_body.force.x * R_body.mass_data.inv_mass + R_body.acceleration.x; R_body.acceleration.y = R_body.force.y * R_body.mass_data.inv_mass + R_body.acceleration.y; R_body.velocity.x = R_body.velocity.x + (R_body.acceleration.x * 0.1); R_body.velocity.y = R_body.velocity.y + (R_body.acceleration.y * 0.1); Move(R_body.velocity.x * 0.1, R_body.velocity.y * 0.1); R_body.force.x = 0; R_body.force.y = 0; R_body.acceleration.x = 0; R_body.acceleration.y = 0; } } int RigidBody::Move(int x,int y) { if(TryMove( x, y))return 1; Affine(x,y); return 0; } int RigidBody::TryMove( int x ,int y) { Level * m = Conteneur->GetEngine()->GetCurrentLevel(); if( !m ) { int tx = R_body.transform->GetX() + x; int ty = R_body.transform->GetY() + y; R_body.transform->SetXY( tx , ty); return 1; } if( x >= m->tileWidth || y>= m->tileHeight || x <= - m->tileWidth || y <= - m->tileHeight) { TryMove( x / 2 , y / 2 ); TryMove( x / 2 , y / 2 ); return 1; } int tx = R_body.transform->GetX() + x; int ty = R_body.transform->GetY() + y; if(!CollisionDecor( tx , ty)) { R_body.transform->SetXY( tx , ty); return 1; } else { if( x )R_body.velocity.x = 0 ; if( y )R_body.velocity.y = 0 ; } return 0; } int RigidBody::CollisionDecor( int x , int y) { if(Conteneur->GetEngine()->GetCurrentLevel()) { Level * m = Conteneur->GetEngine()->GetCurrentLevel(); int sizex , sizey; if(!usefixebody) { sizex = Conteneur->GetRender()->GetSizeX(); sizey = Conteneur->GetRender()->GetSizeY(); } else { sizex = widht; sizey = height; } if( x < 0 || ( x + sizex ) > m->levelWidth * m->tileWidth || y + sizey > m->levelHeight * m->tileHeight || y < 2) return 1; int xmin,xmax,ymin,ymax,i,j,indicetile, hm; hm = m->levelHeight * m->tileHeight; xmin = x / m->tileWidth; ymin = (hm - y - sizey ) / m->tileHeight; xmax = (x + sizex - 1) / m->tileWidth; ymax = (hm - y - 1) / m->tileHeight; for( i = xmin ; i <= xmax ; i++ ) { for( j = ymin ; j <= ymax ; j++ ) { indicetile = m->GetIdMap(i,j); if(m->solid[indicetile])return 1; } } } return 0; } void RigidBody::Affine(int vx,int vy) { int i; for( i = 0 ; i < abs(vx) ; i++) { if( !TryMove ( sgn(vx) , 0 ) ) break; } for( i = 0 ; i < abs(vy) ; i++) { if( !TryMove ( 0 , sgn(vy) ) ) break; } }