-
Notifications
You must be signed in to change notification settings - Fork 0
/
EfectCollisionResponse.cpp
68 lines (48 loc) · 2.07 KB
/
EfectCollisionResponse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "EfectCollisionResponse.h"
EfectCollisionResponse::EfectCollisionResponse(float _toOtherRatio, float _springRatio, float _maxSpringForce, const Vector2D& _constRatio)
: toOtherRatio(_toOtherRatio), springRatio(_springRatio), maxSpringForce(_maxSpringForce), constRatio(_constRatio)
{
}
bool logger = false;
void EfectCollisionResponse::onUpdate(float dt)
{
Efect::onUpdate(dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::N))
logger = true;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M))
logger = false;
}
void EfectCollisionResponse::onCollision( const ColliderInfo& otherCollider)
{
Efect::onCollision( otherCollider);
if (colliderInfo == nullptr)
return;
Vector2D toOther = otherCollider.getActualCollider().center - colliderInfo->getActualCollider().center;
if (toOther.isZero())
return;
Vector2D toOtherNormalised = toOther.normalised();
toOther *= springRatio / toOther.getLenghtSq() * (colliderInfo->collider.range + otherCollider.collider.range);
if (toOther.getLenghtSq() > maxSpringForce*maxSpringForce)
{
toOther = toOtherNormalised * maxSpringForce;
}
Vector2D force = toOtherNormalised * toOtherRatio + toOther;
//if (sf::Keyboard::isKeyPressed(sf::Keyboard::N) && !toOther.isZero())
if (logger && !toOther.isZero())
{
//cout << "force = " << force << "\n";
cout << "ToOther = " << toOther << "\n";
//cout << "ToOtherNormalised = " << toOtherNormalised << "\n";
//cout << "\n";
}
otherCollider.efect->onPush(force);
/*Vector2D toOther = otherCollider.getActualCollider().center - colliderInfo->getActualCollider().center;
//Vector2D toOther = other->getPosC().position - getOwner()->getPosC().position;
Vector2D velocity = otherCollider.efect->getPosC().velocity - getOwner()->getPosC().velocity;
if ( !toOther.isZero() )
toOther /= toOther.getLenghtSq();
if (!velocity.isZero())
velocity /= velocity.getLenghtSq();
Vector2D force = toOther.normalised() * toOtherRatio + velocity * velocityRatio + constRatio;
otherCollider.efect->onPush( force );/**/
}