Jump to content

Object never stops moving after collistion


Pau
 Share

Recommended Posts

Hello!

I am making a car racing game. You can play the prototype in this link:
https://pablomonteserin.com/apuntes/web/js/canvas/phaser/ex/coches/cars-foro-phaser-2/

My problem is when when the car i am controlling with the keyboard collisions with another car. The second car starts moving and never stops. I tried to put some friction, but it is not working. This is the important part of the code:

coches = this.physics.add.group();
player1 = coches.create(300,100,'car1');
player2 = coches.create(300,200,'car2');
player3 = coches.create(300,300,'car3');
player4 = coches.create(300,400,'car4');
this.physics.add.collider(coches, coches);

player1.setFrictionX(5)
player1.setFrictionY(5)

player2.setFrictionX(5)
player2.setFrictionY(5)

player3.setFrictionX(5)
player3.setFrictionY(5)

player4.setFrictionX(5)
player4.setFrictionY(5)

How can in make the collisioned car stops moving after a while?

Thank you in advance :)

 

 

Link to comment
Share on other sites

You're game is topdown so you'd need friction to work in the Z direction, not X and Y. Without seeing your code it's hard to tell, but depending on what Physics you're using you might be able to use setFrictionAir to slow your cars down.

Player1.setFrictionAir(1);

Failing that you might have to roll your own Z friction.

Link to comment
Share on other sites

Sorry, it is not working for me :(

This is the whole code, maybe it helps :)

Thank you in advance:

var velocity=0;
var juego;
var config = {
	type: Phaser.AUTO,
	width: 960,
	height: 640,
	scene: {
		preload: preload,
		create: create, 
		update: update
	},
	physics: {
		default: 'arcade',
		arcade: {
			debug: true,
			gravity: { y: 0 }
		}
	}
}
var game = new Phaser.Game(config);

function preload() {
	resize()
	window.addEventListener("resize", resize, false);
	this.load.image('car1', '../img/F1_amarillo_32x64.png');
	this.load.image('car2', '../img/F1_azul_32x64.png');
	this.load.image('car3', '../img/F1_rojo_32x64.png');
	this.load.image('car4', '../img/F1_verde_32x64.png');
	this.load.image('upbtn', '../img/acelerador.png');
}

function create() {
	cursors = this.input.keyboard.createCursorKeys();
	juego = this;
	coches = this.physics.add.group();
	player1 = coches.create(300,100,'car1');
	player2 = coches.create(300,200,'car2');
	player3 = coches.create(300,300,'car3');
	player4 = coches.create(300,400,'car4');
	this.physics.add.collider(coches, coches);

	player1.setFrictionX(5)
	player1.setFrictionY(5)

	player2.setFrictionX(5)
	player2.setFrictionY(5)

	player3.setFrictionX(5)
	player3.setFrictionY(5)

	player4.setFrictionX(5)
	player4.setFrictionY(5)
}


function findObjectsByType(type, tilemap, layer) {
	var result = [];
	tilemap.objects.forEach(function(element) {
		if (element.name == layer) {
			element.objects.forEach(function(element2) {
				if (element2.type == type) {
					element2.y -= tilemap.tileHeight;
					result.push(element2);
				}
			});
		}
	});
	return result;
}

function update(){
	if (cursors.up.isDown && velocity <= 200) {
		velocity+=7;
	}else if (velocity >= 7){
		velocity -= 7;
	}

	player1.body.velocity.x = velocity * Math.cos(Phaser.Math.DegToRad(player1.angle-180));
	player1.body.velocity.y = velocity * Math.sin(Phaser.Math.DegToRad(player1.angle-180));

	/*Rotation of Car*/
	if (cursors.left.isDown )
		player1.angle = player1.angle - 10*(velocity/1000);
	else if (cursors.right.isDown )
		player1.angle = player1.angle + 10*(velocity/1000);
}

function resize(width, height) {
	var canvas = document.querySelector("canvas");
	var windowWidth = window.innerWidth;
	var windowHeight = window.innerHeight;
	var windowRatio = windowWidth / windowHeight;
	var gameRatio = game.config.width / game.config.height;
	if (windowRatio < gameRatio) {
		canvas.style.width = windowWidth + "px";
		canvas.style.height = (windowWidth / gameRatio) + "px";
	} else {
		canvas.style.width = (windowHeight * gameRatio) + "px";
		canvas.style.height = windowHeight + "px";
	}
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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