Exemplo n.º 1
0
//-----------------------------------------------------------------------------------------------------------------------------------
bool GameObject::CheckCollisionWithObjects(const std::list<GameObject*>& collisionObjects)
{
  if (!GetCollider() || m_noCollisions)
  {
    return false;
  }

  for (GameObject* gameObject : collisionObjects)
  {
    if (gameObject == this)
    {
      continue;
    }

    // Check distance away from object before doing proper collision detection
    if (Vector2::DistanceSquared(GetWorldPosition(), gameObject->GetWorldPosition()) >= (GetSize() * 0.5f).LengthSquared() + (gameObject->GetSize() * 0.5f).LengthSquared())
    {
      continue;
    }

    if (GetCollider()->CheckCollisionWith(gameObject->GetCollider()))
    {
      OnCollision();

      return true;
    }
  }

  return false;
}
void cEntity::Logic(int *map, boxCollider *plane)
{
	int alfa = 0;
	
	// JUMP & GRAVITY
	if (jumping)
	{
		jump_alfa += JUMP_STEP;

		if (jump_alfa == 180)
		{
			jumping = false;
		Position.y = jump_y;
		}
		else
		{
			alfa = ((float)jump_alfa) * 0.017453f;
			//Position.y = jump_y + ((float)JUMP_HEIGHT) * sin(alfa);

			if (jump_alfa > 90)
			{
			//Over floor?
			jumping = !cEntity::Collides(GetCollider(), plane);
			}
		}
	}
	else
	{
		//Over floor?
		if (!cEntity::Collides(GetCollider(), plane))
			Position.y -= (2 * speed +0.5);
		}
	
}
Exemplo n.º 3
0
void cCamera::Update(bool keys[],int mouseX,int mouseY, object *plane)
{
	float angle;
	float alfa;
	Map = plane;
	if(mouseX!=middleX)
	{
		angle = (middleX - mouseX) / 50.0f;
		RotateY(angle);
	}
	if(mouseY!=middleY)
	{
		angle = (middleY - mouseY) / 50.0f;
		RotateX(angle);
	}

	if (keys['w'])		MoveForwards(-speed);
	if (keys['s'])		MoveForwards(speed);
	if (keys['a'])		StrafeRight(-speed);
	if (keys['d'])		StrafeRight(speed);
	if (keys[' '])		Jump();


	// JUMP & GRAVITY
	if (jumping)
	{
		jump_alfa += JUMP_STEP;

		if (jump_alfa == 180)
		{
			jumping = false;
			Position.y = jump_y;
		}
		else
		{
			alfa = ((float)jump_alfa) * 0.017453f;
			Position.y = jump_y + (int)(((float)JUMP_HEIGHT) * sin(alfa));

			if (jump_alfa > 90)
			{
				//Over floor?
				jumping = !CollidesMap(GetCollider(), Map);
			}
		}
	}
	else
	{
		//Over floor?
		if (!CollidesMap(GetCollider(), Map))
			Position.y -= (2 * speed);
	}


}
Exemplo n.º 4
0
void cCamera::Jump()
{
	if (!jumping)
	{
		if (CollidesMap(GetCollider(), Map))
		{
			jumping = true;
			jump_alfa = 0;
			jump_y = Position.y;
		}
	}
		
}
Exemplo n.º 5
0
void cCamera::StrafeRight ( GLfloat distance )
{
	if (ViewDirChanged) GetViewDir();
	cVector3D MoveVector;
	MoveVector.z = -ViewDir.x * -distance;
	MoveVector.y = 0.0;
	MoveVector.x = ViewDir.z * -distance;

	if (CollidesMap(GetCollider(), Map))
		MoveVector.y = 0;
	else
		MoveVector.y = -1;
	Position.Add(MoveVector);
}
Exemplo n.º 6
0
void cCamera::MoveForwards( GLfloat distance )
{
	bool col;
	if (ViewDirChanged) GetViewDir();
	cVector3D MoveVector;
	MoveVector.x = ViewDir.x * -distance;
	MoveVector.y = ViewDir.y * -distance;
	MoveVector.z = ViewDir.z * -distance;
	
	if (CollidesMap(GetCollider(), Map))
		MoveVector.y = 0;
	else
		MoveVector.y = -1;
	Position.Add(MoveVector);
}
Exemplo n.º 7
0
void cCamera::Folk(GLfloat distance)
{
	cVector3D MoveVector;
	MoveVector.x = 0.0;
	MoveVector.y = 1.0 * distance;
	MoveVector.z = 0.0;
	
	if (CollidesMap(GetCollider(), Map))
	{
		onFloor = true;
	}
	else
	{
		Position.Add(MoveVector);
	}
}
Exemplo n.º 8
0
void Blast::Update(void)
{
	const float LIFE_SPAN = 0.375f;
	static float GREEN = 1.0f;
	
	if( GREEN > 0.5f )
		GREEN = 0.5f;
	else
		GREEN = 1.0f;

	EDGameCore::ICollider* iCollider = GetCollider();
	if( iCollider == 0 )
		return;

	if( iCollider->GetColliderType() != EDGameCore::SPHERE )
		return;

	EDGameCore::SphereCollider* sphereCollider = (EDGameCore::SphereCollider*)iCollider;

	EDMath::Sphere sphere = sphereCollider->GetSphere();

	float deltaTime = EDGameCore::Game::GetDeltaTime();
	age += deltaTime;

	if( age >= LIFE_SPAN )
	{
		EDGameCore::Game::destroy( GetGameObject() );
		return;
	}

	float ratio = age / LIFE_SPAN;

	float ALPHA = 1.0f - ratio*ratio;
	
	sphere.radius *= 1.0f + 10.0f * deltaTime;

	sphereCollider->SetSphere( sphere );

	XMFLOAT4 color( 1.0f, GREEN, 0.0f, ALPHA );

	EDRendererD3D::DebugRenderer::GetInstance()->DrawSphere( sphereCollider->GetWorldSphere(), color );
}
void cEntity::Jump(boxCollider *Map)
{
	if (!jumping)
	{
		if (Collides(GetCollider(), Map))
		{
			jumping = true;
			jump_alfa = 0;
			jump_y = Position.y;
			
			if (state != ANIM_JUMP)
			{
				state = ANIM_JUMP;
				seq = 0;
				delay = 0;
				Model->SetAnimation(state);
			}
			
		}
	}


}
Exemplo n.º 10
0
//-----------------------------------------------------------------------------------------------------------------------------------
void PlayerCar::HandleInput(float elapsedGameTime, const Vector2& mousePosition)
{
  Car::HandleInput(elapsedGameTime, mousePosition);

  if (AcceptsInput())
  {
    const KeyboardInput& keyboard = ScreenManager::GetKeyboardInput();
    float velocityDiff = 0;
    float angleDiff = 0;

    bool notCollided = GetCollider()->GetCollisionStatus() == CollisionStatus::kStillNotCollided;

    if (keyboard.IsKeyDown(Keyboard::Keys::W) && notCollided)
    {
      velocityDiff += GetCarData()->GetAcceleration();
    }

    if (keyboard.IsKeyDown(Keyboard::Keys::S) && notCollided)
    {
      velocityDiff += -GetCarData()->GetBraking();
    }

    if (keyboard.IsKeyDown(Keyboard::Keys::A))
    {
      angleDiff = -GetCarData()->GetHandling();
    }

    if (keyboard.IsKeyDown(Keyboard::Keys::D))
    {
      angleDiff = GetCarData()->GetHandling();
    }

    GetRigidBody()->SetLinearAcceleration(Vector2(0, velocityDiff));
    GetRigidBody()->SetAngularVelocity(angleDiff);
  }
}
Exemplo n.º 11
0
void psSolid::Setup()
{
    colliderWrap = 0;
    noCollider = false;
    GetCollider();
}
Exemplo n.º 12
0
int main()
{
    // Initialization
    //--------------------------------------------------------------------------------------
    int screenWidth = 800;
    int screenHeight = 450;
    
    InitWindow(screenWidth, screenHeight, "raylib [physics] example - basic rigidbody");

    InitPhysics(3);      // Initialize physics system with maximum physic objects
    
    // Object initialization
    Transform player = (Transform){(Vector2){(screenWidth - OBJECT_SIZE) / 2, (screenHeight - OBJECT_SIZE) / 2}, 0.0f, (Vector2){OBJECT_SIZE, OBJECT_SIZE}};
    AddCollider(PLAYER_INDEX, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, 0});
    AddRigidbody(PLAYER_INDEX, (Rigidbody){true, 1.0f, (Vector2){0, 0}, (Vector2){0, 0}, false, false, true, 0.5f, 1.0f});
    
    // Floor initialization 
    // NOTE: floor doesn't need a rigidbody because it's a static physic object, just a collider to collide with other dynamic colliders (with rigidbody)
    Transform floor = (Transform){(Vector2){0, screenHeight * 0.8f}, 0.0f, (Vector2){screenWidth, screenHeight * 0.2f}};
    AddCollider(PLAYER_INDEX + 1, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0});
    
    // Object properties initialization
    float moveSpeed = 6.0f;
    float jumpForce = 5.0f;
    
    bool physicsDebug = false;
    
    SetTargetFPS(60);
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        
        // Update object physics 
        // NOTE: all physics detections and reactions are calculated in ApplyPhysics() function (You will live happier :D)
        ApplyPhysics(PLAYER_INDEX, &player.position);
        
        // Check jump button input
        if (IsKeyDown(KEY_SPACE) && GetRigidbody(PLAYER_INDEX).isGrounded)
        {
            // Reset object Y velocity to avoid double jumping cases but keep the same X velocity that it already has
            SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){GetRigidbody(PLAYER_INDEX).velocity.x, 0});
            
            // Add jumping force in Y axis
            AddRigidbodyForce(PLAYER_INDEX, (Vector2){0, jumpForce});
        }
        
        // Check movement buttons input
        if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
        {
            // Set rigidbody velocity in X based on moveSpeed value and apply the same Y velocity that it already has
            SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
        }
        else if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
        {
            // Set rigidbody velocity in X based on moveSpeed negative value and apply the same Y velocity that it already has
            SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){-moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
        }
        
        // Check debug mode toggle button input
        if (IsKeyPressed(KEY_P)) physicsDebug = !physicsDebug;
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);
            
            // Draw information
            DrawText("Use LEFT / RIGHT to MOVE and SPACE to JUMP", (screenWidth - MeasureText("Use LEFT / RIGHT to MOVE and SPACE to JUMP", 20)) / 2, screenHeight * 0.20f, 20, LIGHTGRAY);
            DrawText("Use P to switch DEBUG MODE", (screenWidth - MeasureText("Use P to switch DEBUG MODE", 20)) / 2, screenHeight * 0.3f, 20, LIGHTGRAY);
            
            // Check if debug mode is enabled
            if (physicsDebug)
            {
                // Draw every internal physics stored collider if it is active
                for (int i = 0; i < 2; i++)
                {
                    if (GetCollider(i).enabled)
                    {
                        DrawRectangleLines(GetCollider(i).bounds.x, GetCollider(i).bounds.y, GetCollider(i).bounds.width, GetCollider(i).bounds.height, GREEN);
                    }
                }
            }
            else
            {
                // Draw player and floor
                DrawRectangleRec((Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, GRAY);
                DrawRectangleRec((Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, BLACK);
            }

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadPhysics();      // Unload physic objects
    
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}