0

I am trying to detect if a character and an object inside an image collide. I am using a function that can parse the image and creates a collision array and another function that can detect if there is a collision or not in a specific location. My problem is that the isCollision function is never executed that's my jsfiddle : http://jsfiddle.net/AbdiasSoftware/UNWWq/1/

if (isCollision(character.x, character.y)) {
    alert("Collision");
}

Please help me to fix my problem.

2
  • There are errors in the console. Check that first. Commented Jan 24, 2014 at 15:59
  • fixed it please check Commented Jan 25, 2014 at 7:06

2 Answers 2

1

Add this in the top of your init() method and it should work:

FieldImg.crossOrigin = '';

As you are loading the image from a different origin CORS kicks in and you need to request cross-origin usage when using getImageData() (or toDataURL()).

See modified fiddle here.

Note: in you final code this is probably not gonna be necessary though as you probably want to include the images in the same domain as the page itself - in these cases you need to remove the cross-origin request unless your server is setup to handle this. Just something to have in mind for later if what worked suddenly don't...

Sign up to request clarification or add additional context in comments.

1 Comment

this error occurred when i just post the code inside jsfiddle but my main problem isn't there my problem is the iscollision function is not executing can u tell me why
0

Ok i found it :
You are loading a huge background image, and you draw part of it on a much smaller canvas.
You do visual collision detection using a binary view on the display canvas that you build in the process() function.
The issue comes when you want to test : to compute the pixel position of the player within the binary view, you are using y*width + x, but with the wrong width, that of the background image when it should be that of the view (cw).

 function isCollision(x, y) {
     return (buffer8[y * cw + x] === 1);
 }

.
move right and look in the console with this fiddle : http://jsfiddle.net/gamealchemist/UNWWq/9/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.