Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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;
}
}
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question