Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ...
- bool canJump = false;
- bool canLeft = true;
- bool canRight = true;
- for (b2ContactEdge* ce = _playerBody->GetContactList(); ce; ce = ce->next) {
- b2Contact* c = ce->contact;
- if (c->IsTouching()) {
- b2WorldManifold worldManifold;
- c->GetWorldManifold(&worldManifold);
- // Math::dot...
- // Returns 0 when two vectors are perpendicular, > 0 when two vectors are in the same general direction,
- // 1 when two normalized vectors are parallel, < 0 when two vectors are in opposite general direction and
- // -1 when two normalized* vectors are antiparallel.
- if(Math::dot(Vector2(worldManifold.normal.x, worldManifold.normal.y), Vector2::yAxis(-1)) == 1) {
- canJump = true;
- }
- if(Math::dot(Vector2(worldManifold.normal.x, worldManifold.normal.y), Vector2::xAxis(-1)) == 1) {
- canLeft = false;
- }
- if(Math::dot(Vector2(worldManifold.normal.x, worldManifold.normal.y), Vector2::xAxis(1)) == 1) {
- canRight = false;
- }
- }
- }
- b2Vec2 vel = _playerBody->GetLinearVelocity();
- if (pressedKeys[KeyEvent::Key::Comma] && canJump) {
- vel.y = 8.f;
- }
- if (pressedKeys[KeyEvent::Key::A] && canLeft) {
- vel.x = -3.f;
- }
- if (pressedKeys[KeyEvent::Key::E] && canRight) {
- vel.x = 3.f;
- }
- _playerBody->SetLinearVelocity(vel);
Add Comment
Please, Sign In to add comment