hello everyone,
I’m trying to program a version of the game “curve fever” and need support with the collision.
My approach is to create a “Sprite” as movable “curve-head”. Depending on my sprite coordinates i create rectangles with every update in the update function. It works so far. Now i want to check the collide of my curve-head and my curve-body. But it is difficult because of the constant overlap between this both objects.
So i want to select the newest 10 ore 20 rectangles in my update-function to exclude then from the group to collide.
Here is my code:
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
debug: true
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var cursors;
var text;
var curve;
const prev = new Phaser.Math.Vector2();
var game = new Phaser.Game(config);
var startx;
var starty;
setInterval(update, 0);
function preload ()
{
this.load.image('dot', 'img/20_20.png');
}
function create ()
{
startx = 100;
starty = 300;
curve = this.physics.add.sprite(startx, starty, 'dot', null, 10000);
cursors = this.input.keyboard.createCursorKeys();
prev.x = curve.x;
prev.y = curve.y;
curve.body.collideWorldBounds=true;
curve.body.onWorldBounds=true;
this.physics.world.on('worldbounds', (body, up, down, left, right)=>
{
if(down || up || left || right ){
gameover();
}});
}
function update ()
{
var curvebody = this.add.graphics().fillStyle(0xb38b6d);
var collider = this.add.graphics().fillStyle(0xfffff);
const distance = 0.5;
curve.body.velocity.x = 0;
curve.body.velocity.y = 0;
curve.body.angularVelocity = 0;
this.physics.velocityFromRotation(curve.rotation, 1000, curve.body.acceleration);
if (curve.body.speed > 0);
{
const x = curve.x;
const y = curve.y;
if (Phaser.Math.Distance.Between(x, y, prev.x, prev.y) > distance)
{
prev.x = x;
prev.y = y;
curvebody.fillRect(curve.x , curve.y , 3, 3);
//collider.fillRect(curve.x + curve.body.newVelocity.x + 100 , curve.y + curve.body.newVelocity.y + 100, 1, 1);
//console.log(curve);
}
}
if (cursors.left.isDown)
{
curve.setAngularVelocity(-200);
}
else if (cursors.right.isDown)
{
curve.setAngularVelocity(200);
}
else
{
curve.setAngularVelocity(0);
}
this.physics.world.wrap(curve, 0);
console.log(curve.body.angularVelocity);
}
function gameover() {
console.log('lol');
}
Thank you!