From: Tobias Gläßer Date: Thu, 19 Feb 2004 19:07:34 +0000 (+0000) Subject: improved physic X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4c421033bb1223a88b918a1de5e253571d77c29d;p=supertux.git improved physic SVN-Revision: 130 --- diff --git a/src/badguy.c b/src/badguy.c index fe5b7e34d..fa20790d6 100644 --- a/src/badguy.c +++ b/src/badguy.c @@ -84,10 +84,10 @@ void badguy_action(bad_guy_type* pbad) { pbad->dir = RIGHT; } - else if (issolid( pbad->base.x + pbad->base.width-1, (int) pbad->base.y)) - { + else if (issolid( pbad->base.x + pbad->base.width-1, (int) pbad->base.y)) + { pbad->dir = LEFT; - } + } } /* Fall if we get off the ground: */ @@ -172,26 +172,32 @@ void badguy_action(bad_guy_type* pbad) /* Bump into things horizontally: */ + /* Bump into things horizontally: */ + if (!pbad->dying) { - if (issolid(pbad->base.x, pbad->base.y)) + int changed = pbad->dir; + if (issolid( pbad->base.x - 1, (int) pbad->base.y)) { - pbad->dir = !pbad->dir; - - if (pbad->mode == KICK) - { - /* handle stereo sound */ - /* FIXME: In theory a badguy object doesn't know anything about player objects */ - if (tux.base.x > pbad->base.x) - play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER); - else if (tux.base.x < pbad->base.x) - play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER); - else - play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER); - } + pbad->dir = RIGHT; + } + else if (issolid( pbad->base.x + pbad->base.width-1, (int) pbad->base.y)) + { + pbad->dir = LEFT; + } + if(pbad->mode == KICK && changed != pbad->dir) + { + /* handle stereo sound */ + /* FIXME: In theory a badguy object doesn't know anything about player objects */ + if (tux.base.x > pbad->base.x) + play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER); + else if (tux.base.x < pbad->base.x) + play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER); + else + play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER); } - } + } /* Fall if we get off the ground: */ @@ -229,22 +235,28 @@ void badguy_action(bad_guy_type* pbad) pbad->base.y = pbad->base.y + pbad->base.ym * frame_ratio; - if(physic_get_state(&pbad->physic) == -1) - physic_set_state(&pbad->physic,PH_VTU); - - if(issolid(pbad->base.x, pbad->base.y + 32)) - { - physic_set_state(&pbad->physic,PH_VTU); - pbad->base.ym = -0.6; - } - else if(issolid(pbad->base.x, pbad->base.y - 1)) - { /* This works, but isn't the best solution imagineable */ - pbad->base.ym = physic_get_velocity(&pbad->physic,-6.); - } - else - { - pbad->base.ym = physic_get_velocity(&pbad->physic,6.); - } + if(physic_get_state(&pbad->physic) == -1) + { + physic_set_state(&pbad->physic,PH_VT); + physic_set_start_vy(&pbad->physic,0.); + } + + if(issolid(pbad->base.x, pbad->base.y + 32)) + { + physic_set_state(&pbad->physic,PH_VT); + physic_set_start_vy(&pbad->physic,6.); + pbad->base.ym = physic_get_velocity(&pbad->physic); + } + else if(issolid(pbad->base.x, pbad->base.y - 1)) + { /* This works, but isn't the best solution imagineable */ + physic_set_state(&pbad->physic,PH_VT); + physic_set_start_vy(&pbad->physic,0.); + pbad->base.ym = physic_get_velocity(&pbad->physic); + } + else + { + pbad->base.ym = physic_get_velocity(&pbad->physic); + } if (pbad->base.y > screen->h) pbad->base.alive = NO; diff --git a/src/physic.c b/src/physic.c index 7f3c25676..f6440a473 100644 --- a/src/physic.c +++ b/src/physic.c @@ -30,19 +30,25 @@ pphysic->state = nstate; pphysic->start_time = st_get_ticks(); } -float physic_get_velocity(physic_type* pphysic, float start_velocity) +void physic_set_start_vy(physic_type* pphysic, float start_vy) { -return - (start_velocity - 10.* ((float)(st_get_ticks() - pphysic->start_time))/1000.); +pphysic->start_vy = start_vy; } -float physic_get_max_distance(physic_type* pphysic, float start_velocity) +float physic_get_velocity(physic_type* pphysic) { -return (start_velocity * start_velocity / 2.*10.); +if(pphysic->state == PH_VT) +return - (pphysic->start_vy - 10.* ((float)(st_get_ticks() - pphysic->start_time))/1000.); } -unsigned int physic_get_max_time(physic_type* pphysic, float start_velocity) +float physic_get_max_distance(physic_type* pphysic) { -return (unsigned int)((start_velocity / 10.) * 1000); +return (pphysic->start_vy * pphysic->start_vy / 2.*10.); +} + +unsigned int physic_get_max_time(physic_type* pphysic) +{ +return (unsigned int)((pphysic->start_vy / 10.) * 1000); } unsigned int physic_get_time_gone(physic_type* pphysic) diff --git a/src/physic.h b/src/physic.h index 3730285b7..915034524 100644 --- a/src/physic.h +++ b/src/physic.h @@ -16,7 +16,7 @@ #include "timer.h" enum { - PH_VTU /* Vertical throw up. */ + PH_VT, /* Vertical throw.*/ }; /* Physic type: */ @@ -24,6 +24,7 @@ enum { typedef struct physic_type { int state; + float start_vy; unsigned int start_time; } physic_type; @@ -31,9 +32,10 @@ physic_type; void physic_init(physic_type* pphysic); int physic_get_state(physic_type* pphysic); void physic_set_state(physic_type* pphysic, int nstate); -float physic_get_velocity(physic_type* pphysic, float start_velocity); -float physic_get_max_distance(physic_type* pphysic, float start_velocity); -unsigned int physic_get_max_time(physic_type* pphysic, float start_velocity); +void physic_set_start_vy(physic_type* pphysic, float start_vy); +float physic_get_velocity(physic_type* pphysic); +float physic_get_max_distance(physic_type* pphysic); +unsigned int physic_get_max_time(physic_type* pphysic); unsigned int physic_get_time_gone(physic_type* pphysic); #endif /*SUPERTUX_PHYSIC_H*/