HTML5 Game Physics Elastic Collisions

HTML5 Game Physics Acceleration And Elastic Collisions

This tutorial is the second in a series about HTML5 game physics. Game character movement becomes more realistic when acceleration is used. Acceleration is the rate at which the velocity of a game character changes with time.

The second tutorial will move the game character vertically downwards according to gravity. Air or fluid resistance will be provided by a set drag coefficient. The bottom will act as a non-moving object for collision with the moving game character.

This series is intermediate and assumes basic knowledge of physics and 2D graphics. The drag coefficient is based on the size and speed of the character. Elastic collisions conserves momentum and kinetic energy which is possessed due to motion.

This tutorial uses the HTML5 canvas tag and JavaScript.

    Tools are required:

  • Text editor.
  • Folder for web server.
  • Browser to view output.

HTML5 Elastic Collisions File

<!--
   HTML5_Game_Physics_Acceleration_And_Elastic_Collisions.html
   
   Copyright 2013 edward <http://Ojambo.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.
   
   
-->


<!DOCTYPE html>
<html xml:lang="en" lang="en">

<head>
	<title>Ojambo.com HTML5 Game Physics Acceleration & Elastic Collisions</title>
	<meta charset="utf-8" />
	<style type="text/css">
		canvas#myCanvas{
			width: 80%;
			height: 80%;
			background: green;
		}
	</style>
</head>

<body>
	<canvas id="myCanvas"></canvas>
	<script type="text/JavaScript">
		//Position, Size & Velocity
		var rect = {
			x: 0,
			y: 0,
			w: 20,
			h: 20,
			vx: 0,
			vy: 0
		}
		
		// Find Canvas Element
		var myCanvas = document.getElementById("myCanvas");
		// Call getContext() method for 2D drawing
		var ctx = myCanvas.getContext("2d");
		
		//Update Every 20 milliseconds
		setInterval(function() {
			// Draw Function
			draw();
		}, 20);
		
		// Draw Function
		function draw(){
			//Clear Display
			ctx.save();
			ctx.clearRect(0,0, myCanvas.width, myCanvas.height);
			ctx.restore();
			
			//Apply velocity to position (vx -> x)
			rect.x += rect.vx;
			rect.y += rect.vy;
			
			// Apply drag/friction to velocity
			rect.vx *= 0.99;
			rect.vy *= 0.99;
			
			// Apply gravity to velocity
			rect.vy += 0.25;
			
			// Collision
			if (rect.y + rect.h > myCanvas.height) {
				rect.y = myCanvas.height - rect.h;
				rect.vy = -Math.abs(rect.vy);
			}
			
			// Draw on the canvas
			ctx.save();
			//Fill style red colour
			ctx.fillStyle="#FF0000";
			//Rectangle
			ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
			ctx.restore();
		}
	</script>
</body>

</html>

The game character will be pulled downwards by gravity. The acceleration will be hampered by the drag coefficient. The bottom of the canvas is reached when the game character position is exactly above the bottom.

The bottom of the canvas acts as a non-moving object for collisions. The game character will bounce off the bottom of the canvas. The canvas height minus the game character height is the bottom of the canvas.

How to Use:

    Open Browser

  • Observe the canvas.
  • Observe the character moving down the canvas.
  • Observe the impact of the collision.

Demonstration:

Ojambo.com HTML5 Game Physics Elastic Collision Tutorial

Image Missing
Ojambo.com HTML5 Game Physics Acceleration & Elastic Collisions

Conclusion:

Game characters accelerate downwards due to gravity. The drag coefficient provides air resistance for characters based on their size and velocity.

Elastic collisions cause characters to change their acceleration. The exact point of collision impact can be determined by the character’s position and dimensions.

    Recommendations:

  1. Determine which objects moves after a collision.
  2. Give large objects different drag coefficients.
  3. Use a character’s dimensions to determine collision aftermath.

Tags: , , , , ,

This entry was posted on Wednesday, July 10th, 2013 at 12:00 am and is filed under Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “HTML5 Game Physics Elastic Collisions”

  1. Ojambo Ojambo – HTML5 Game Physics Elastic Collisions Video 0065 Says:

    July 12th, 2013 at 12:13 am

    […] Video for the Ojambo.com HTML5 Game Physics Elastic Collisions article. Part One of […]

  2. Ojambo HTML5 Game Physics Velocity Says:

    July 14th, 2013 at 1:36 pm

    […] Ojambo.com HTML5 Game Physics Elastic Collisions Part II […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>