A 2D tile-based sandbox game.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Body.cc 887B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "Body.h"
  2. #include <math.h>
  3. namespace Swan {
  4. void Body::friction(Vec2 coef) {
  5. force_ += -vel_ * coef;
  6. }
  7. void Body::gravity(Vec2 g) {
  8. force_ += g * mass_;
  9. }
  10. void Body::collide(WorldPlane &plane) {
  11. int startx = (int)floor(pos_.x_);
  12. int endx = (int)floor(pos_.x_ + size_.x_);
  13. int y = (int)(pos_.y_ + size_.y_);
  14. on_ground_ = false;
  15. for (int x = startx; x <= endx; ++x) {
  16. Tile &tile = plane.getTile(TilePos(x, y));
  17. if (tile.is_solid_) {
  18. pos_.y_ = y - size_.y_;
  19. vel_.y_ = 0;
  20. on_ground_ = true;
  21. break;
  22. }
  23. }
  24. }
  25. void Body::outline(Win &win) {
  26. win.setPos(pos_);
  27. sf::RectangleShape rect(size_ * TILE_SIZE);
  28. rect.setFillColor(sf::Color::Transparent);
  29. rect.setOutlineColor(sf::Color(128, 128, 128));
  30. rect.setOutlineThickness(1);
  31. win.draw(rect);
  32. }
  33. void Body::update(float dt) {
  34. vel_ += (force_ / mass_) * dt;
  35. pos_ += vel_ * dt;
  36. force_ = { 0, 0 };
  37. }
  38. }