Doubt in javascript collisions

Asked

Viewed 146 times

1

I’m starting programming now, and I have the task of making a simple control with the keyboards on a "map" which is an image, and the cart can bypass the streets, the map is the attached image but n I am able to think of a logic to make the collisions using the x/y coordinatesinserir a descrição da imagem aqui the areas in green, IE, everything that is not street the cart n could walk my cart has size 40x20 pixels.

PS: I don’t want the code ready, I want a help to think about logic or some easier way to do it that n is using x/y

  • Colision Detection still has enough science behind, to work with speed/acceleration, etc. So it depends on the realism of the implementation you want to give. There’s a lot of stuff on the net about it, there’s even a simple example in the MDN itself for 2D games

2 answers

1

From this image, generate a new image, where the streets are white and the rest is black. The idea is this, where it’s white, you can go, where it’s black is blocked. Whenever you want to move the trolley you check in this image, first, if the new position continues in the white position. If yes, you move, otherwise you refuse the movement (collision)

  • Hmmm, it’s a good one too. Thank you! But I’ve delivered the work, thank you very much!

0


I’ve done something like this before. But I had to create a scenario like this with comic books, each comic represented something that could be bumped or not.

In your case, you could separate this image into small squares, trying to separate all the asphalt as much as possible. And in your code you assemble the image, and each square with asphalt you assign a value. When walking with the car in the image, you check if it is colliding with the value that is on asphalt, if it is not you do not let move.

There are easier ways and tools to do this, but as it is for your study, this is a great task.

Good luck!

  • So, I thought I’d do this, divide all the blocks into small objects and check if the cart invades their space, using matrix, but the teacher asked necessarily q was using if testing the positions x/y. However I have no idea how to do. In the correction I was wrong, it is to use x/y. testing every time a key is triggered. up/down/left/right, if he is next n should walk. But thank you! If you can think of something thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.