Small Javascript maths routine writing to detect collision between game objects
- or -
Post a project like this3304
£40(approx. $50)
- Posted:
- Proposals: 2
- Remote
- #729956
- Completed
Description
Experience Level: Intermediate
Estimated project duration: 1 day or less
General information for the business: I am writing a simple game
Description of requirements/functionality: I am writing a simple Javascript 2D game in which two players drive dodgem car’s around the screen trying to bump into each other. The cars can be considered to be circles. I need a short routine writing which can detect collisions between the cars and determine which bumped into which. The solution will undoubtedly require use of Math.atan2. It should only be 10-20 lines of maths to solve this problem.
Be a little careful because the x,y position of each player is not transformed by a rotation in my code because I ‘cheat’ and use css transformation to rotate the car sprite.
Each player ‘car’ has the following properties held in a ‘Player’ object:
posX = horizontal position of top left corner of the dodgem (before rotation). Relates to the top left corner of the playing area being at 0
posY= verticle position of top left corner of the dodgem (before rotation). Relates to the top left corner of the playing area being at 0
xDir = decimal value from -1 to +1 where -1 means travelling directly left and +1 means travelling directly right (values in between indicate travelling at an angle)
yDir = decimal value from -1 to +1 where -1 means travelling directly up and +1 means travelling directly down (values in between indicate travelling at an angle)
angle = the angle in degrees that the dodgem is travelling (and therefore pointing) in. 0 degrees being pointing straight up. 90 degrees meaning pointing right. 180 down; 270 pointing left.
Radius = the size of the dodgem car (both have the same value)
What I need to know:
1) Have they collided i.e. is there an overlap?
2) Have they collided head on or within X degrees of head on
3) If they have collided side on or one has rear ended the other, which dodgem bumped into the other i.e. which ones front is hitting the other ones side or rear
The method would look something like this (where the two parameters passed in a Player objects):
function detectCollision(firstPlayer, secondPlayer) {
return { didCollide: true/false; wasHeadToHead: true/false; firstPlayerHitSecondPlayer: true/false}
}
Extra notes: Javascript
Description of requirements/functionality: I am writing a simple Javascript 2D game in which two players drive dodgem car’s around the screen trying to bump into each other. The cars can be considered to be circles. I need a short routine writing which can detect collisions between the cars and determine which bumped into which. The solution will undoubtedly require use of Math.atan2. It should only be 10-20 lines of maths to solve this problem.
Be a little careful because the x,y position of each player is not transformed by a rotation in my code because I ‘cheat’ and use css transformation to rotate the car sprite.
Each player ‘car’ has the following properties held in a ‘Player’ object:
posX = horizontal position of top left corner of the dodgem (before rotation). Relates to the top left corner of the playing area being at 0
posY= verticle position of top left corner of the dodgem (before rotation). Relates to the top left corner of the playing area being at 0
xDir = decimal value from -1 to +1 where -1 means travelling directly left and +1 means travelling directly right (values in between indicate travelling at an angle)
yDir = decimal value from -1 to +1 where -1 means travelling directly up and +1 means travelling directly down (values in between indicate travelling at an angle)
angle = the angle in degrees that the dodgem is travelling (and therefore pointing) in. 0 degrees being pointing straight up. 90 degrees meaning pointing right. 180 down; 270 pointing left.
Radius = the size of the dodgem car (both have the same value)
What I need to know:
1) Have they collided i.e. is there an overlap?
2) Have they collided head on or within X degrees of head on
3) If they have collided side on or one has rear ended the other, which dodgem bumped into the other i.e. which ones front is hitting the other ones side or rear
The method would look something like this (where the two parameters passed in a Player objects):
function detectCollision(firstPlayer, secondPlayer) {
return { didCollide: true/false; wasHeadToHead: true/false; firstPlayerHitSecondPlayer: true/false}
}
Extra notes: Javascript
Hugh F.
100% (5)Projects Completed
4
Freelancers worked with
4
Projects awarded
36%
Last project
19 May 2018
United Kingdom
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
There are no clarification messages.
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies