Jump to content

Physics check on individiual objects.


Juice
 Share

Recommended Posts

In my game I have a player, and I have several collision boxes, I want to know how to run a collision check on the box i'm closest to, or how to run them individually.

 

My code:

var player = {	x:32,	y:32,	size:32,	grav:0,	xspeed:0,	terminalVel:10,	onGround:false,	friction:0.8,	update:function() {		if (this.onGround == true) {			this.grav = 0;		} else if (this.onGround == false) {			this.grav += 0.4;		}		if (this.xspeed > this.terminalVel) {			this.xpseed = this.terminalVel;		}		if (this.grav > this.terminalVel) {			this.grav = this.terminalVel;		}		this.xspeed *= this.friction;		this.x += this.xspeed;		this.y += this.grav;		// Collision		for (var key in boxes) {			// Vertical			if ((this.y+this.grav >= boxes[key].y-32 && this.y <= boxes[key].y) && (this.x <= boxes[key].x+boxes[key].width)) {				this.onGround = true;				this.y = boxes[key].y-32;			} else {				this.onGround = false;			}		}	}};function basicBox(x,y,width,height,color) {	this.color = color;	this.x = x;	this.y = y;	this.width = width;	this.height = height;	this.draw = function() {		g.fillStyle = this.color;		g.fillRect(this.x,this.y,this.width,this.height);	}}var boxes = [	new basicBox(32,128,32,32,"blue"),	new basicBox(64,256,32,64,"orange"),	new basicBox(256,32,128,64,"red"),];function run() { // game loop    for (var key in boxes) {        boxes[key].draw();    }    player.update();}

I am pretty new to javascript, so how would I go about doing this?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...