-1
\$\begingroup\$

Thanks to Alexandre Desbiens I have awesome collision detection. I can just slide right along walls. However if I try to go left into a wall I get stuck. But the good news is I can just back out and everything is okay. But it would be nice to have a consistent behavior when running into walls. Here is what my tryMove function looks like.

tryMove: function() {
      var game = roguelike.game;
      var speed = game.library.speedPerSecond(this.speed);
      //Left
      if(this.moving.left
        && !this.collision(speed, 0)
        && !this.collision(speed, 1)) {
        this.x -= speed;
      }
      //Up
      if(this.moving.up
        && !this.collision(0, -speed)
        && !this.collision(1, -speed)) {
        this.y -= speed;
      }
      //Right
      if(this.moving.right
        && !this.collision(speed + 1, 0)
        && !this.collision(speed + 1, 1)) {
        this.x += speed;
      }
      //down
      if(this.moving.down
        && !this.collision(0, speed + 1)
        && !this.collision(1, speed + 1)) {
        this.y += speed;
      }
    }

I added the second collision test to check two different points of the player one on both corners of the players moving direction. I really appreciate it. Thanks.

\$\endgroup\$

closed as off-topic by Sean Middleditch, Anko, Seth Battin, Josh Jun 29 '15 at 15:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Sean Middleditch, Anko, Seth Battin, Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ What are the parameters of collision, and what is it supposed to do? \$\endgroup\$ – Vaillancourt Jun 13 '15 at 17:07
  • \$\begingroup\$ collision(xOffset, yOffset) checks a collision with walls at the player's position + the offset. \$\endgroup\$ – Alexandre Desbiens Jun 13 '15 at 21:06
  • \$\begingroup\$ @AlexandreDesbiens TBH, you should not have to answer to comments on OP's question :P (but since the op's question seems to be built on your previous answers....) \$\endgroup\$ – Vaillancourt Jun 14 '15 at 7:14
  • \$\begingroup\$ @AlexandreVaillancourt Yeah, that was I was thinking. By having a link in his question, it makes tracing previous posts easier, I guess... \$\endgroup\$ – Alexandre Desbiens Jun 14 '15 at 14:41
  • \$\begingroup\$ Why did this question get down voted? \$\endgroup\$ – zachdyer Jun 14 '15 at 17:04
1
\$\begingroup\$

Try putting -speed when doing a left or up movement and speed when doing a down or right movement:

tryMove: function() {
    var game = roguelike.game;
    var speed = game.library.speedPerSecond(this.speed);
    //Left
    if(this.moving.left
        && !this.collision(-speed, 0)) {
        this.x -= speed;
    }
    //Up
    if(this.moving.up
        && !this.collision(0, -speed)) {
        this.y -= speed;
    }
    //Right
    if(this.moving.right
        && !this.collision(speed, 0)) {
        this.x += speed;
    }
    //down
    if(this.moving.down
        && !this.collision(0, speed)) {
        this.y += speed;
    }
}

When doing a collision check on the X asis, going left will move your character of -speed pixels. You must then check the collision at the position -speed in order to catch any present wall.

I also have fixed the down and right collisions, you didn't need to put +1 to speed.

\$\endgroup\$
  • \$\begingroup\$ Once again Alexandre Desbiens answers my question. That was a nice catch I did not even notice that. I appreciate it much! \$\endgroup\$ – zachdyer Jun 14 '15 at 2:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.