Fix collision
It works now! How great
This commit is contained in:
		
							
								
								
									
										14
									
								
								App.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								App.c
									
									
									
									
									
								
							@@ -9,7 +9,7 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static void _app_onHit(Entity *me, Entity *other, Vec2 triedDelta, void *data) {
 | 
			
		||||
static void _app_onHitBy(Entity *me, Entity *other, Vec2 triedDelta, void *data) {
 | 
			
		||||
	fprintf(
 | 
			
		||||
		stderr,
 | 
			
		||||
		"[_app_onHit] Entity \"%s\" hit by \"%s\", delta: [%.2lf, %.2lf]\n",
 | 
			
		||||
@@ -46,7 +46,7 @@ App *app_NewApp() {
 | 
			
		||||
	hit1->hitbox->box.lefttop = vec2(200, 200);
 | 
			
		||||
	hit1->hitbox->box.size    = vec2(100, 400);
 | 
			
		||||
	hit1->hitbox->fixed       = true;
 | 
			
		||||
	hit1->hitbox->onHit       = &_app_onHit;
 | 
			
		||||
	hit1->hitbox->onHitBy     = &_app_onHitBy;
 | 
			
		||||
	entity_Commit(app->entity, hit1);
 | 
			
		||||
 | 
			
		||||
	Entity *hit2 = entity_Create(app->entity, "hit2");
 | 
			
		||||
@@ -54,9 +54,17 @@ App *app_NewApp() {
 | 
			
		||||
	hit2->hitbox->box.lefttop = vec2(700, 200);
 | 
			
		||||
	hit2->hitbox->box.size    = vec2(100, 400);
 | 
			
		||||
	hit2->hitbox->fixed       = true;
 | 
			
		||||
	hit2->hitbox->onHit       = &_app_onHit;
 | 
			
		||||
	hit2->hitbox->onHitBy     = &_app_onHitBy;
 | 
			
		||||
	entity_Commit(app->entity, hit2);
 | 
			
		||||
 | 
			
		||||
	Entity *hit3 = entity_Create(app->entity, "hit3");
 | 
			
		||||
	ADD_COMPONENT(hit3, hitbox, zero_malloc(sizeof(Component_Hitbox)));
 | 
			
		||||
	hit3->hitbox->box.lefttop = vec2(100, 550);
 | 
			
		||||
	hit3->hitbox->box.size    = vec2(800, 30);
 | 
			
		||||
	hit3->hitbox->fixed       = true;
 | 
			
		||||
	hit3->hitbox->onHitBy     = &_app_onHitBy;
 | 
			
		||||
	entity_Commit(app->entity, hit3);
 | 
			
		||||
 | 
			
		||||
	return app;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ typedef struct {
 | 
			
		||||
	Box2    box;
 | 
			
		||||
	bool    fixed;
 | 
			
		||||
 | 
			
		||||
	physics_HitHandler onHit;
 | 
			
		||||
	physics_HitHandler onHit, onHitBy;
 | 
			
		||||
	void              *onHitData;
 | 
			
		||||
} Component_Hitbox;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,10 @@ static inline void call_hithandler(Entity *me, Entity *other, Vec2 triedDelta, v
 | 
			
		||||
	if (me->hitbox->onHit)
 | 
			
		||||
		me->hitbox->onHit(me, other, triedDelta, data);
 | 
			
		||||
}
 | 
			
		||||
static inline void call_hitby(Entity *me, Entity *other, Vec2 triedDelta, void *data) {
 | 
			
		||||
	if (me->hitbox->onHitBy)
 | 
			
		||||
		me->hitbox->onHitBy(me, other, triedDelta, data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void _physics_MoveX(System_Physics *sys, Entity *e, Duration deltaTime) {
 | 
			
		||||
@@ -34,14 +38,13 @@ void _physics_MoveX(System_Physics *sys, Entity *e, Duration deltaTime) {
 | 
			
		||||
 | 
			
		||||
		if (box2_Intersects(tohit, box2_OffsetX(mybox, delta), NULL)) {
 | 
			
		||||
			call_hithandler(e, tohit_comp->super, vec2(delta, 0), e->hitbox->onHitData);
 | 
			
		||||
			call_hitby(tohit_comp->super, e, vec2(delta, 0), tohit_comp->onHitData);
 | 
			
		||||
			if (delta > 0) {
 | 
			
		||||
				// Moves right, hits left edge
 | 
			
		||||
				fprintf(stderr, "hit left edge");
 | 
			
		||||
				double maxdelta = tohit.lefttop.x - mybox.lefttop.x - mybox.size.x;
 | 
			
		||||
				delta           = maxdelta - EPS;
 | 
			
		||||
			} else {
 | 
			
		||||
				// Moves left, hits right edge
 | 
			
		||||
				fprintf(stderr, "hit right edge");
 | 
			
		||||
				double maxdelta = tohit.lefttop.x - mybox.lefttop.x + tohit.size.x;
 | 
			
		||||
				delta           = maxdelta + EPS;
 | 
			
		||||
			}
 | 
			
		||||
@@ -72,6 +75,7 @@ void _physics_MoveY(System_Physics *sys, Entity *e, Duration deltaTime) {
 | 
			
		||||
 | 
			
		||||
		if (box2_Intersects(tohit, box2_OffsetY(mybox, delta), NULL)) {
 | 
			
		||||
			call_hithandler(e, tohit_comp->super, vec2(0, delta), e->hitbox->onHitData);
 | 
			
		||||
			call_hitby(tohit_comp->super, e, vec2(0, delta), tohit_comp->onHitData);
 | 
			
		||||
			if (delta > 0) {
 | 
			
		||||
				// Moves down, hits top edge
 | 
			
		||||
				double maxdelta = tohit.lefttop.y - mybox.lefttop.y - mybox.size.y;
 | 
			
		||||
 
 | 
			
		||||
@@ -62,7 +62,14 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
 | 
			
		||||
	if (input_IsPressed(input->keys[input_Key_Right]))
 | 
			
		||||
		targetVecX += walkSpeed;
 | 
			
		||||
 | 
			
		||||
	double targetVecY = 0.0;
 | 
			
		||||
	if (input_IsPressed(input->keys[input_Key_Up]))
 | 
			
		||||
		targetVecY += -walkSpeed;
 | 
			
		||||
	if (input_IsPressed(input->keys[input_Key_Down]))
 | 
			
		||||
		targetVecY += walkSpeed;
 | 
			
		||||
 | 
			
		||||
	sys->player->super->position->velocity.x = targetVecX;
 | 
			
		||||
	sys->player->super->position->velocity.y = targetVecY;
 | 
			
		||||
 | 
			
		||||
	if (sys->player->onGround)
 | 
			
		||||
		sys->player->jumpCount = 0;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								Types.c
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Types.c
									
									
									
									
									
								
							@@ -32,8 +32,8 @@ bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection) {
 | 
			
		||||
	// Compute the min and max of the first rectangle on both axes
 | 
			
		||||
	const double r1MinX = dmin(x.lefttop.x, x.lefttop.x + x.size.x);
 | 
			
		||||
	const double r1MaxX = dmax(x.lefttop.x, x.lefttop.x + x.size.x);
 | 
			
		||||
	const double r1MinY = dmin(x.lefttop.y, x.lefttop.x + x.size.x);
 | 
			
		||||
	const double r1MaxY = dmax(x.lefttop.y, x.lefttop.x + x.size.x);
 | 
			
		||||
	const double r1MinY = dmin(x.lefttop.y, x.lefttop.y + x.size.y);
 | 
			
		||||
	const double r1MaxY = dmax(x.lefttop.y, x.lefttop.y + x.size.y);
 | 
			
		||||
 | 
			
		||||
	// Compute the min and max of the second rectangle on both axes
 | 
			
		||||
	const double r2MinX = dmin(y.lefttop.x, y.lefttop.x + y.size.x);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user