D
D
durnevdanya2017-10-26 20:31:13
JavaScript
durnevdanya, 2017-10-26 20:31:13

How to make an object "solid" on canvas?

Good evening. I'm making a js lib, and then I ran into a problem. I have a function called collisionDetectedBetween(object1, object2) which returns true or false depending on whether the objects on the canvas overlap or not.
It works, and now I need a function that will make one of the objects solid so that I cannot pass through it with another object. I use this option, but it does not work, it only works if I approach from the right, and from other sides this object stupidly teleports in different directions, as if the code cannot understand which side I am approaching from

makeSolid(object1, object2)
  {
      if(this.collisionDetectedBetween(object1, object2) && object1.x > object2.x - object2.width && object1.x < object2.x)
          object1.x = object2.x - object2.width;
      else if(this.collisionDetectedBetween(object1, object2) && object1.x < object2.x + object2.width && object1.x > object2.x)
          object1.x = object2.x + object2.width;
      else if(this.collisionDetectedBetween(object1, object2) && object1.y < object2.y + object2.height && object1.y > object2.y)
          object1.y = object2.y + object2.height;
      else if(this.collisionDetectedBetween(object1, object2) && object1.y > object2.y - object2.height && object1.y < object2.y)
          object1.y = object2.y - object2.height;
  }

What is the problem? Tried a lot, but nothing comes out.
Here is the whole project with my code and example
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxfox, 2017-10-26
@maxfox

What you have written in the code is not what you gave in the question. You have it like this:

makeSolid(object1, object2)
  {
    if(this.collisionDetectedBetween(object1, object2) && object1.x > object2.x - object2.width && object1.x < object2.x)
    {
      if(!this.collisionDetectedBetween(object1, object2))
        object1.x = object2.x - object2.width;
    }
    if(this.collisionDetectedBetween(object1, object2) && object1.x < object2.x + object2.width && object1.x > object2.x)
    {
      if(!this.collisionDetectedBetween(object1, object2))
        object1.x = object2.x + object2.width;
    }
    if(this.collisionDetectedBetween(object1, object2) && object1.y < object2.y + object2.height && object1.y > object2.y)
    {
      if(!this.collisionDetectedBetween(object1, object2))
        object1.y = object2.y + object2.height;
    }
    if(this.collisionDetectedBetween(object1, object2) && object1.y > object2.y - object2.height && object1.y < object2.y)
    {
      if(!this.collisionDetectedBetween(object1, object2))
        object1.y = object2.y - object2.height;
    }
  }

(Taken from github)
This, of course, will not work.
This is how it all works:
makeSolid(object1, object2) {
        if (!this.collisionDetectedBetween(object1, object2)) return;
    if(object1.x > object2.x - object2.width && object1.x < object2.x) {
            object1.x = object2.x - object2.width;
    }
    if(object1.x < object2.x + object2.width && object1.x > object2.x) {
            object1.x = object2.x + object2.width;
    }
    if(object1.y < object2.y + object2.height && object1.y > object2.y) {
            object1.y = object2.y + object2.height;
    }
    if(object1.y > object2.y - object2.height && object1.y < object2.y) {
            object1.y = object2.y - object2.height;
    }
  }

I won't comment on the code, it's just bad. And the fact that you cannot find simple errors in your own code is proof of that. I advise you to work on it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question