aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-11-17 18:45:46 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-11-17 18:45:46 +0100
commitb1c82f914f18457f9a30791b01896ea40832c2d3 (patch)
tree6c70f8336fb408f05a7e1816369008a2954065d0 /src
parent9885c58817912125506de3b3555f3dc6206e0e6a (diff)
bounce
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Rigidbody.h2
-rw-r--r--src/crepe/facade/SDLContext.cpp8
-rw-r--r--src/crepe/system/CollisionSystem.cpp8
-rw-r--r--src/example/collision.cpp19
4 files changed, 23 insertions, 14 deletions
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index a45623f..b55297c 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -76,6 +76,8 @@ public:
bool use_gravity = true;
//! if object bounces
bool bounce = false;
+ //! bounce factor of material
+ double bouncie_factor = 0.0;
//! offset of all colliders
Vector2 offset;
};
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 274cd7a..5d22cdf 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -117,8 +117,8 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform,
= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
| (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
- double adjusted_x = (transform.position.x - cam.x) * cam.zoom;
- double adjusted_y = (transform.position.y - cam.y) * cam.zoom;
+ double adjusted_x = (transform.position.x - cam.x -(sprite.sprite_rect.w/2)) * cam.zoom;
+ double adjusted_y = (transform.position.y - cam.y -(sprite.sprite_rect.h/2)) * cam.zoom;
double adjusted_w = sprite.sprite_rect.w * transform.scale * cam.zoom;
double adjusted_h = sprite.sprite_rect.h * transform.scale * cam.zoom;
@@ -130,8 +130,8 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform,
};
SDL_Rect dstrect = {
- .x = static_cast<int>(adjusted_x/2),
- .y = static_cast<int>(adjusted_y/2),
+ .x = static_cast<int>(adjusted_x),
+ .y = static_cast<int>(adjusted_y),
.w = static_cast<int>(adjusted_w),
.h = static_cast<int>(adjusted_h),
};
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
index 4b74e67..8377823 100644
--- a/src/crepe/system/CollisionSystem.cpp
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -152,7 +152,13 @@ void CollisionSystem::call_collision_handler(CollidedInfoStor& data1,CollidedInf
void CollisionSystem::static_collision_handler(CollisionInfo& info){
std::cout << "INFO: x:" << info.first.transform.position.x << "y:" << info.first.transform.position.y << std::endl;
info.first.transform.position += info.move_back_value;
- info.first.rigidbody.data.linear_velocity = {0,0};
+ if(info.first.rigidbody.data.bounce) {
+ info.first.rigidbody.data.linear_velocity = -info.first.rigidbody.data.linear_velocity * info.first.rigidbody.data.bouncie_factor;
+ }
+ else {
+ info.first.rigidbody.data.linear_velocity = {0,0};
+ }
+
}
std::vector<std::pair<CollisionSystem::CollidedInfoStor,CollisionSystem::CollidedInfoStor>> CollisionSystem::check_collisions(const std::vector<std::reference_wrapper<BoxCollider>>& boxcolliders, const std::vector<std::reference_wrapper<CircleCollider>>& circlecolliders) {
diff --git a/src/example/collision.cpp b/src/example/collision.cpp
index a5cec83..b141d3d 100644
--- a/src/example/collision.cpp
+++ b/src/example/collision.cpp
@@ -49,8 +49,8 @@ int main(int argc, char * argv[]) {
LoopManager gameloop;
Color color(0, 0, 0, 0);
- double screen_size_width = 1280;
- double screen_size_height = 960;
+ double screen_size_width = 640;
+ double screen_size_height = 480;
double world_collider = 1000;
//define playable world
GameObject World(0, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
@@ -72,15 +72,16 @@ int main(int argc, char * argv[]) {
GameObject game_object1(1, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
game_object1.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
- .gravity_scale = 1,
+ .gravity_scale = 0.01,
.body_type = Rigidbody::BodyType::DYNAMIC,
- .linear_velocity = {0,1},
+ .linear_velocity = {0,0},
.constraints = {0, 0, 0},
- .use_gravity = false,
- .bounce = false,
+ .use_gravity = true,
+ .bounce = true,
+ .bouncie_factor = 1,
.offset = {0,0},
});
- game_object1.add_component<BoxCollider>(Vector2{0, 0}, 40, 40);
+ game_object1.add_component<BoxCollider>(Vector2{0, 0}, 20, 20);
game_object1.add_component<BehaviorScript>().set_script<MyScript>();
game_object1.add_component<Sprite>(
make_shared<Texture>("/home/jaro/crepe/asset/texture/green_square.png"), color,
@@ -88,7 +89,7 @@ int main(int argc, char * argv[]) {
game_object1.add_component<Camera>(Color::get_white());
- // GameObject game_object2(2, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2+100}, 0, 1);
+ // GameObject game_object2(2, "Name", "Tag", Vector2{20, 470}, 0, 1);
// game_object2.add_component<Rigidbody>(Rigidbody::Data{
// .mass = 1,
// .gravity_scale = 1,
@@ -99,7 +100,7 @@ int main(int argc, char * argv[]) {
// .bounce = false,
// .offset = {0,0},
// });
- // game_object2.add_component<BoxCollider>(Vector2{0, 0}, 40, 40);
+ // game_object2.add_component<BoxCollider>(Vector2{0, 0}, 0, 0);
// game_object2.add_component<BehaviorScript>().set_script<MyScript>();
// game_object2.add_component<Sprite>(
// make_shared<Texture>("/home/jaro/crepe/asset/texture/red_square.png"), color,