818 |
return normal; |
return normal; |
819 |
} |
} |
820 |
|
|
|
static V2 |
|
|
reflect(V2 v, V2 normal) |
|
|
{ |
|
|
double n = length(normal); |
|
|
if (n != 0) |
|
|
normal /= n; |
|
|
return V2(v-2*(v*normal)*normal); |
|
|
} |
|
|
|
|
821 |
static void |
static void |
822 |
handle_contacts(Actor *a) |
handle_contacts(Actor *a) |
823 |
{ |
{ |
824 |
|
ActorInfo &a1 = *a->get_actorinfo(); |
825 |
|
|
826 |
/* |
/* |
827 |
** Handle contacts with stones |
** Handle contacts with stones |
828 |
*/ |
*/ |
829 |
StoneContactList cl; |
StoneContactList cl; |
830 |
find_stone_contacts(a, cl); |
find_stone_contacts(a, cl); |
831 |
V2 normal = accumulate(cl.begin(), cl.end(), V2(), handle_stone_contact); |
if (!cl.empty()) { |
832 |
|
V2 normal = accumulate(cl.begin(), cl.end(), V2(), handle_stone_contact); |
|
ActorInfo &a1 = *a->get_actorinfo(); |
|
833 |
|
|
834 |
a1.vel = reflect(a1.vel, normal); |
double n = length(normal); |
835 |
|
if (n != 0) { |
836 |
|
double restitution = 0.8; |
837 |
|
normal /= n; |
838 |
|
a1.vel -= (1 + restitution)*(a1.vel*normal)*normal; |
839 |
|
} |
840 |
|
} |
841 |
|
|
842 |
/* |
/* |
843 |
** Handle contacts with other actors |
** Handle contacts with other actors |