Search the Community

Showing results for tags 'collision'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • HTML5 Game Coding
    • News
    • Game Showcase
    • Coding and Game Design
  • Frameworks
    • Phaser
    • Pixi.js
    • Babylon.js
    • Panda 2
    • melonJS
    • Haxe JS
    • Kiwi.js
  • General
    • General Talk
  • Business
    • Collaborations (un-paid)
    • Jobs (Hiring and Freelance)
    • Services Offered

Found 489 results

  1. Tiled/Sprite collision

    Hi, I want to ask how to collide sprite with a tile in layer on map created in tiled. So far I have this, but it doesnt work /* global demo, game, Phaser */ demo.stateMaze = function(){}; var ball; var playerSpeed = 3; var map; demo.stateMaze.prototype = { preload: function() { game.load.tilemap('map-maze', 'assets/maps/hole_in_a_park/maze/mapa.json', null, Phaser.Tilemap.TILED_JSON); game.load.image('tileset-maze', 'assets/maps/hole_in_a_park/maze/tileset.jpg'); game.load.image('ball','assets/imgs/gula.png'); }, create: function() { game.stage.backgroundColor = '#FFFFFF'; //Starting physics game.physics.startSystem(Phaser.Physics.ARCADE); map = game.add.tilemap('map-maze'); map.addTilesetImage('tileset','tileset-maze'); //CallBACK map.setTileIndexCallback(15, this.collisionHandler(), this); normal = map.createLayer('normal'); normal.resizeWorld(); wall = map.createLayer("wall"); wall.resizeWorld(); normal.renderSettings.enableScrollDelta = true; wall.renderSettings.enableScrollDelta = true; ball = game.add.sprite(0, 80, 'ball'); ball.anchor.setTo(0.5, 0.5); //Enabling ball physics game.physics.enable(ball); game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT; game.input.onDown.add(function(){ if (game.scale.isFullScreen) { game.scale.stopFullScreen(); } else { game.scale.startFullScreen(false); } }); }, update: function() { //Collide game.physics.arcade.collide(ball, wall); if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { ball.scale.setTo(1, 1); ball.x += playerSpeed; } if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { ball.scale.setTo(-1, 1); ball.x -= playerSpeed; } if(game.input.keyboard.isDown(Phaser.Keyboard.UP)) { ball.y -= playerSpeed; } if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { ball.y += playerSpeed; } }, collisionHandler: function() { console.log("COLLISION!!!"); } }; But it never write COLLISION!!! so can anyone tell me what to do ?
  2. Hi There, I am having this issue with phaser(2.9.4) where collision with a tilemap only work with the base layer. In my example I have the collide-able world and then I wanted to add a background underneath that was part of the tilemap. Here is my code(excluding boot and preload) Game.preload = function () { game.stage.backgroundColor = "000000"; }; Game.create = function () { Game.scale = 5; game.physics.startSystem(Phaser.Physics.ARCADE); game.physics.arcade.gravity.y = 850; Game.cursors = game.input.keyboard.createCursorKeys(); Game.map = game.add.tilemap('map'); Game.map.addTilesetImage('tiles_1', 'tiles'); Game.back = Game.map.createLayer('Background'); Game.back.setScale(Game.scale); Game.back.renderSettings.enableScrollDelta = false; Game.spawn = Game.map.createLayer('Spawn'); Game.spawn.setScale(Game.scale); Game.spawn.renderSettings.enableScrollDelta = false; Game.decor = Game.map.createLayer('Decor'); Game.decor.setScale(Game.scale); Game.decor.renderSettings.enableScrollDelta = false; Game.layer = Game.map.createLayer('Collideable'); Game.layer.setScale(Game.scale); Game.layer.renderSettings.enableScrollDelta = false; Game.layer.resizeWorld(); Game.map.setCollisionByExclusion([]); Game.player = game.add.sprite(game.world.centerX, game.world.centerY, 'player'); Game.player.scale.setTo(Game.scale); //Game.player.anchor.setTo(0.5); game.physics.enable(Game.player, Phaser.Physics.ARCADE); Game.player.body.collideWorldBounds = true; game.camera.follow(Game.player); var enemyPos = Game.findTiles(952, true); Game.enemies = game.add.group(); for (var enemies in enemyPos) { var enemy = game.add.sprite(enemyPos[enemies][0] * Game.scale * 16, enemyPos[enemies][1] * Game.scale * 16, 'enemy'); Game.enemies.add(enemy); enemy.scale.setTo(randomInt(1, 5)); game.physics.enable(enemy, Phaser.Physics.ARCADE); enemy.body.collideWorldBounds = true; enemy.speed = randomInt(150, 250); enemy.interval = 0; enemy.update = function () { this.interval++; if (this.interval >= 20) { this.interval = 0; if (this.x > Game.player.x) { this.body.velocity.x = -this.speed; } else if (this.x < Game.player.x) { this.body.velocity.x = this.speed; } if (this.y > Game.player.y && this.body.blocked.down) { this.body.velocity.y = -600; } } } } }; Game.update = function () { game.physics.arcade.collide(Game.player, Game.layer); //game.physics.arcade.collide(Game.player, Game.enemies); game.physics.arcade.collide(Game.enemies, Game.layer); //game.physics.arcade.collide(Game.enemies, Game.enemies); Game.player.body.velocity.setMagnitude(Math.min(1000, Game.player.body.velocity.getMagnitude())); if (Game.cursors.left.isDown) { Game.player.body.velocity.x = -500; } else if (Game.cursors.right.isDown) { Game.player.body.velocity.x = 500; } else { Game.player.body.velocity.x = 0; } if (Game.cursors.up.isDown && Game.player.body.blocked.down) { Game.player.body.velocity.y = -600; } else if (Game.cursors.up.isDown && Game.player.body.touching.down) { Game.player.body.velocity.y = -600; } }; Game.findTiles = function (id, destroy) { var mapArray = Game.spawn.getTiles(0, 0, game.world.width, game.world.height); var found = []; var needToFindID = id; for (var blocks in mapArray) { if (needToFindID == mapArray[blocks].index) { found.push([mapArray[blocks].x, mapArray[blocks].y]); if (destroy) { Game.map.removeTile(mapArray[blocks].x, mapArray[blocks].y, Game.spawn).destroy(); } } } return found; }; function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } Any help would be greatly appreciated.
  3. Hello everyone, Do you know if it is possible to add a custom collision handler for world bound collisions with the arcade physics system? I know that the onWorldBounds signal exists, but from what I've tried it seems to be fired at the beginning of the collision and it does not prevent the velocity changes from being made further down the road. Am I missing something? For clarification what I am trying to achieve is to add random direction displacement to the velocity of a ball each time it collides with the world bounds. Thank you for time.
  4. I have a function which detects collision but when the player is moving too fast or if it is too small or both, the player clips through the wall. If i can have help on how to improve/change/redo the code to make it work at higher speeds and when the player is smaller function checkPos(obj1,obj2){ if (obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && //COLLISION FROM BOTTOM obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y) { if (obj1.x > obj2.x+obj2.width-(player.speed*2) && obj1.y + obj1.height > obj2.y+player.speed*2 && obj1.y+player.speed*2 < obj2.y + obj2.height) { // hit a block on the right obj1.x = obj1.x + player.speed } if (obj1.x+obj1.width-(player.speed+4) < obj2.x) { // hit a block on the left obj1.x = obj1.x - player.speed } if (obj1.y > obj2.y+obj2.height-(player.speed*2) && obj1.x + obj1.width > obj2.x + player.speed*2 && obj1.x+player.speed*2 < obj2.x + obj2.width){ // hit a block on the bottom obj1.y = obj1.y + player.speed } if (obj1.y+obj1.height-(player.speed*2) < obj2.y){ // hit a block on the top obj1.y = obj1.y - player.speed } } }
  5. SceneLoader

    Greetings all, I am have been a 'C' and Assembler systems coder for over 35 years. I am retired now and I want to try my hand at writing a game. I have been reading the documentation a great deal as well as looking at many examples. I have also been in communication with Dad72 who is a member here. He has been very kind. That being said I have some questions regarding the mechanics of Babylon.js. I am a real noob when it comes to this framework as I have always programmed 'on the wire' and never used a scripted language that is not strictly type cast so please forgive my ignorance. My goal is to build a 3D game that has a detective who runs around and solves crimes. He will go into several areas looking for clues and then use those clues to solve the crime. As an example, the detective may be sent to a warehouse where he will open a safe after finding the combination and then see some clues that will lead him to a suspects home. There will be human models, as well as different things that can be picked up examined and used. The story may be outside sometimes as well as inside. This is a very basic description of the mechanics of the game but I think you can see where I am going. I need some advice on what documentation I should read. What forums I should frequent and perhaps even some examples. I am really at a loss as my area is low level operating system programming in Windows, Linux, and DOS. I do have some questions about what I think may help me, so if you would not mind I would like to ask them. I have been doing some reading and stumbled upon SceneLoader.Append. Is it possible to build an entire level in Blender, then export that with the Babylon.js Blender exporter, then load that with SceneLoader.Append? Is that the best way to build the game I described. After a level is loaded can I then set up collision detection with actions? Can I bring in different models with their textures and place them in the scene after it is loaded? As an example, opening a container will expose a model that was not there before. Can I also delete models from the scene after some action. I have no idea how to accomplish any of this. I cannot load a terrain and place a model on it and keep it from falling through the terrain. I can't even build walls for a room and set up collision detection within the room. The one thing I do have is lots of time as I am retired and a clear understanding of programming principles. Thank you so much for your time, it is greatly appreciated. Regards, Richard
  6. Collision mask in Phaser

    hi guys, Why there is no collision mask (like in construct 2d) in phaser.I think that will be a good feature.
  7. On colliding with a box the camera sees through the box. In the following PGs I have used camera.ellipsoidOffset to place the camera at the middle of the ellipsoid (line 86) and used two viewposts. The lower viewport uses a second camera slightly above and behind the main camera. I have added a cone to represent the main camera with an ellipsoid around it. I have also customized the key input behaviour. Up and down arrow keys move the camera forward and backward , left and right arrow keys turn the camera. I have also lowered the speed of the camera. Face a box and walk towards it until it collides and will not move forward. You can see through the box. https://www.babylonjs-playground.com/#DHMD1C There is also another mystery as the camera moves forward it rises upward as can be seen by the spheres parent to the main camera. At the start you can only see the top of the sphere then as it moves the whole of the sphere becomes visible. You can see the changes in y in the console. Perhaps they are to do with my customised input manager but no - I removed it and did a PG with default behaviour same problem https://www.babylonjs-playground.com/#DHMD1C#1 TLDR; These issues came to light as I am in the middle of writing a PBT (playground based tutorial) for camera collisions, gravity and customized camera inputs and have been following this thread with interest. Last week I never noticed the gravity issue or the see through the box issue. Now whether changes instigated as a result of this thread has altered some property of the camera or I am mis-remembering what I saw a week ago or was just too focused on the code for the PBT I am not sure but the mystery is there now.
  8. Hi there, I'm playing around with the v3 API and wondered how to collide with a layer of a tilemap. Here is what I've already tried: 1) Get tiles data (there is probably a better way / API helper for that like forEachTile) and then use setCollision. let tilesData = (this.platformLayer.data.gameObject.map.tiles) this.map.setCollision(tilesData, true, false, this.platformLayer) 2) Do the opposite by excluding an empty array. this.map.setCollisionByExclusion([], true, false, this.platformLayer) 3) API v2 style. this.physics.arcade.collide(this.player, this.platformLayer); Here is my game config, if someone have the time to spot what I'm doing wrong. Thanks! let config = { type: Phaser.AUTO, parent: 'content', width: 800, height: 600, pixelArt: true, backgroundColor: '#2d2d2d', physics: { default: 'impact', impact: { setBounds: true, debug: true }, }, scene: [ Boot, Preload, TileMapTest ] } let game = new Phaser.Game(config) And here is my Scene /* globals __DEV__ */ class TileMapTest extends Phaser.Scene { constructor () { super({ key: 'TileMapTest' }) if (__DEV__) { console.log('TileMapTest scene created.') } this.map = null this.player = null this.cursors = null this.rockLayer = null this.waterLayer = null this.platformLayer = null this.stuffLayer = null } preload () { // Hero this.load.image('hero', './assets/sprites/hero.png') // Tilemap this.load.image('kenny_platformer_64x64', './assets/tilemaps/tiles/kenny_platformer_64x64.png') this.load.tilemapJSON('multiple-layers-map', './assets/tilemaps/maps/multiple-layers.json') } create () { if (__DEV__) { console.log('TileMapTest scene entered.') } this.createTileMapLayers() this.createPlayer() this.cameras.main.startFollow(this.player) this.cameras.main.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels) this.physics.world.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels) // ---> @todo collide with platformLayer } update (time, delta) { this.updatePlayer(time, delta) } createPlayer () { this.player = this.physics.add.sprite(200, 200, 'hero', 4).setOrigin(0, 0.15) this.player.setActive() this.player.setMaxVelocity(500) this.player.setFriction(1000, 100) this.player.body.accelGround = 1200 this.player.body.accelAir = 600 this.player.body.jumpSpeed = 500 // this.player.body.collideWorldBounds = true this.cursors = this.input.keyboard.createCursorKeys() } createTileMapLayers () { this.map = this.make.tilemap({ key: 'multiple-layers-map' }) let tiles = this.map.addTilesetImage('kenny_platformer_64x64') this.rockLayer = this.map.createStaticLayer('Rock Layer', tiles, 0, 0) this.waterLayer = this.map.createStaticLayer('Water Layer', tiles, 0, 0) this.platformLayer = this.map.createStaticLayer('Platform Layer', tiles, 0, 0) this.stuffLayer = this.map.createStaticLayer('Stuff Layer', tiles, 0, 0) } updatePlayer (time, delta) { let accel = this.player.body.standing ? this.player.body.accelGround : this.player.body.accelAir if (this.cursors.left.isDown) { this.player.setAccelerationX(-accel) } else if (this.cursors.right.isDown) { this.player.setAccelerationX(accel) } else { this.player.setAccelerationX(0) } // Jump if (this.cursors.up.isDown && this.player.body.standing) { this.player.setVelocityY(-this.player.body.jumpSpeed) } } } export default TileMapTest
  9. Collision Tile Layer

    Hey guys, please help with my collision problem. https://github.com/Matheus-Bard/invaders_life/tree/master/invaders_V0.2 thks for the help! gameState.js load.js
  10. How to use collision in phaser

    I've been trying to get my collision to work in phaser, I created my map in tiled so as far as i know i'm you use something like this "map.setCollisionBetween(264, 265, true, 'foreground');" I got the coordinates by calculating the id's from the tiles from the png file that i used in tiled. It creates the collision but not where i want it. I will insert the png file i used. so the tiled id goes like from the top left and right being 264 and 265. The bottom left and right is 297 and 298 the collision is working like this: My script looks like this: Any ideas why this is happening or how i can fix it? Thanks
  11. So a gotcha of the moveWithCollisions method is that it uses an ellipsoid mesh to calculate collisions of your mesh instead of the original mesh dimensions. I see a clear way to remove your original mesh but how do we remove the ellipsoid mesh from the scene? I'm worried that keeping these ellipsoids even when my original mesh has been disposed of will hurt performance. Thank you so much.
  12. Random Located Sprite Collision

    Hi everyone, I am trying to add collision to sprites that are randomly spawned in the map of the game. // New object of class Player the sprite name is 'ship' player = new Player("keyboardMouse", game.world.centerX, game.world.centerY, 200, 200, "12341", game.add.sprite(game.world.randomX, game.world.randomY, 'ship'), //Sprite handler object Weapons.sniperWeapon.weapon, // Weapon handler object ThrustEmitters.ourEmitter.emitter // Thrust emmiter handler object ); // loop to randomly place the sprites for (var i = 0; i < 200; i++){ var asteroid_brown = game.add.sprite(game.world.randomX, game.world.randomY, 'asteroid'); var rock = game.add.sprite(game.world.randomX, game.world.randomY, 'rock'); } In theory, I would want to make something like the resources in moomoo.io (trees, rocks, gold etc.) You should be able to add collision so that when the player cannot just travel through them and also when shot at you would get 1 of the specific resource. Also if this can be done in a better way, how would you do it? Thanks! reee.mov
  13. My sprites are often passing through polygons that they should be bouncing off of. I've tried to research this and all I could find is that the fps may be too low to always detect the collision but surely the game engine should be able to handle this? Interestingly, if I use setCircle() my colliding sprites never pass through even with much higher velocity. Why would a polygon I define behave differently than setCircle() ? Here is a demonstration: //phaser.io/sandbox/xeGQxHis
  14. Sprite collision

    I am currently making a game that you can check out at http://spalaxys.online and I would like to add little asteroids in a fixed random location in which the player cannot go through. I have looked at the phaser examples as well as watched some videos, but I cannot get it working. What is the best way to go about this? I have linked my files so you can check them out. I would really appreciate any help. This is the code that I am using: (create.js) function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.renderer.clearBeforeRender = false; game.renderer.roundPixels = true; game.add.tileSprite(0, 0, 30000, 30000, 'space'); //game.add.tileSprite(0, 0, 1920, 1920, 'background'); game.world.setBounds(0, 0, 30000, 30000); //Possible weapons Weapons = { sniperWeapon: new Weapon("Sniper weapon", 0, 1000, 0, null, null, false, 5000, 90, game.add.weapon(5, 'long-bullet')), uziWeapon: new Weapon("Uzi", 20, 50, 0, null, null, false, 1500, 90, game.add.weapon(500, 'bullet')), AK47LikeWeapon: new Weapon("AK47-like", 5, 100, 0, null, null, false, 1000, 90, game.add.weapon(100, 'round-bullet')), }; ThrustEmitters = { basicEmitter: new ThrustEmitter(game.add.emitter(game.world.centerX, game.world.centerY, 400), ['fire1', 'fire2', 'fire3'], 0.3), ourEmitter: new ThrustEmitter(game.add.emitter(game.world.centerX, game.world.centerY, 1000), ['fire01', 'fire02', 'fire03'], 0.5), }; // Creating a new object of class Player player = new Player("keyboardMouse", game.world.centerX, game.world.centerY, 200, 200, "12341", game.add.sprite(game.world.randomX, game.world.randomY, 'ship'), //Sprite handler object Weapons.sniperWeapon.weapon, // Weapon handler object ThrustEmitters.ourEmitter.emitter // Thrust emmiter handler object ); game.physics.enable(player.sprite, Phaser.Physics.ARCADE); player.spawn(); game.camera.follow(player.sprite); player.sprite.maxVelocity = player.maxVelocity; // Game input cursors = game.input.keyboard.createCursorKeys(); var style = { font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" }; var text = game.add.text(game.world.centerX - 500, game.world.centerY - 300, "Move with W,S and mouse. \n Switch to touch/click movement with TAB. \nToggle debug with SPACE. \nShoot with LMB. \ \nSwitch weapons with 1, 2, 3.", style); text.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2); //Make resources group //var resources = game.add.group(); //Run the show_resources function located in main.js for (var i = 0; i < 250; i++){ var asteroid_brown = game.add.sprite(game.world.randomX, game.world.randomY, 'asteroid'); var rock = game.add.sprite(game.world.randomX, game.world.randomY, 'rock'); } game.physics.enable(asteroid_brown, Phaser.Physics.ARCADE); game.physics.enable(rock, Phaser.Physics.ARCADE); // We'll set the bounds to be from x0, y100 and be 800px wide by 100px high text.setTextBounds(0, 100, 800, 100); // this.spaceButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR); // this.WButton = this.input.keyboard.addKey(Phaser.KeyCode.W); // this.SButton = this.input.keyboard.addKey(Phaser.KeyCode.S); // this.AButton = this.input.keyboard.addKey(Phaser.KeyCode.A); // this.DButton = this.input.keyboard.addKey(Phaser.KeyCode.D); // this.oneButton = this.input.keyboard.addKey(Phaser.KeyCode.ONE); // this.twoButton = this.input.keyboard.addKey(Phaser.KeyCode.TWO); Keys = { space: this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR), w: this.input.keyboard.addKey(Phaser.KeyCode.W), s: this.input.keyboard.addKey(Phaser.KeyCode.S), one: this.input.keyboard.addKey(Phaser.KeyCode.ONE), two: this.input.keyboard.addKey(Phaser.KeyCode.TWO), three: this.input.keyboard.addKey(Phaser.KeyCode.THREE), four: this.input.keyboard.addKey(Phaser.KeyCode.FOUR), five: this.input.keyboard.addKey(Phaser.KeyCode.FIVE), six: this.input.keyboard.addKey(Phaser.KeyCode.SIX), seven: this.input.keyboard.addKey(Phaser.KeyCode.SEVEN), tab: this.input.keyboard.addKey(Phaser.KeyCode.TAB), } // WEAPON CHANGING Keys.one.onDown.add(function(){player.setWeapon(Weapons.sniperWeapon.weapon)}, this); Keys.two.onDown.add(function(){player.setWeapon(Weapons.uziWeapon.weapon)}, this); Keys.three.onDown.add(function(){player.setWeapon(Weapons.AK47LikeWeapon.weapon)}, this); // OTHER KEYS Keys.space.onDown.add(function () { showDebug = true; }, this); Keys.tab.onDown.add(function () { if (player.movementType == "keyboardMouse") { console.log("touchMouse") player.movementType = "touchMouse" } else { console.log("keyboardMouse") player.movementType = "keyboardMouse" } }, this); game.input.keyboard.addKeyCapture([ Phaser.Keyboard.W, Phaser.Keyboard.A, Phaser.Keyboard.S, Phaser.Keyboard.D, Phaser.Keyboard.SPACEBAR, Phaser.Keyboard.ONE, Phaser.Keyboard.TWO, Phaser.Keyboard.THREE, Phaser.Keyboard.FOUR, Phaser.Keyboard.FIVE, Phaser.Keyboard.TAB, ]); } update.js: function update() { player.sprite.rotation = game.physics.arcade.angleToPointer(player.sprite); if (player.movementType == "keyboardMouse") { if (Keys.w.isDown) { game.physics.arcade.accelerationFromRotation(player.sprite.rotation, player.positiveAcceleration, player.sprite.body.acceleration); // this.isEmmiter=true; this.txtW2 = "Is W still down? YES"; if (!this.isThrustEmitter) { player.emitter.start(false, 3000, 5); this.isThrustEmitter = true; } } else if (Keys.s.isDown) { game.physics.arcade.accelerationFromRotation(player.sprite.rotation, (-1) * player.negativeAcceleration, player.sprite.body.acceleration); this.txtS2 = "S still down? YES" this.isThrustEmitter = false; } else { player.sprite.body.acceleration.set(0); this.txtW2 = "Is W still down? NO"; this.txtS2 = "S still down? YES" this.isThrustEmitter = false; } } // if (this.WButton.downDuration(250)) // { // this.txtW = "W was pressed 250 ms ago? YES"; // } // else // { // this.txtW = "W was pressed 250 ms ago? NO"; // } // if (this.SButton.downDuration(250)) // { // this.txtS = "S was pressed 250 ms ago? YES"; // } // else // { // this.txtS = "S was pressed 250 ms ago? NO"; // } if (game.input.activePointer.isDown) { player.weapon.fire(); if (player.movementType == "touchMouse") { game.physics.arcade.accelerationFromRotation(player.sprite.rotation, player.positiveAcceleration, player.sprite.body.acceleration); if (!this.isThrustEmitter) { player.emitter.start(false, 3000, 5); this.isThrustEmitter = true; } } } else { if (player.movementType == "touchMouse") { this.isThrustEmitter = false; player.sprite.body.acceleration.set(0); } } //THRUST EMITTER if(this.isThrustEmitter) { //player.emitter.on = true; } else { player.emitter.on = false; } // We have to calculate the vector for thrust emitter var px = Math.cos(player.sprite.rotation) * 1000; var py = Math.sin(player.sprite.rotation) * 1000; px *= -1; //So it goes from the 'back' of the ship py *= -1; //So it goes from the 'back' of the ship player.emitter.minParticleSpeed.set(px / 2, py / 2); player.emitter.maxParticleSpeed.set(px, py); player.emitter.emitX = player.sprite.x; player.emitter.emitY = player.sprite.y; // bullets.forEachExists(screenWrap, this); } I would like the player ('ship' sprite) to not go through the asteroids ('asteroid_brown' and 'rock' sprites) but whenever I try to add physics to them or try to change them the game stops working. Thanks in advance, Fede
  15. Demo sourecode

    Hello evryone, i try to better understand collisions & mehs interactions. I found this demo on the babylon.js 101 docs: http://www.babylon.actifgames.com/moveCharacter/ https://doc.babylonjs.com/babylon101/cameras,_mesh_collisions_and_gravity Is it possible to get the source code ?
  16. Hi, I realize this is a topic that was covered before but I couldn't find a solution and wasn't sure if there was one now. I have several imported meshes in a scene (relatively complex ones) and I can drag and drop one at a time. I want to check whether the mesh I am dragging has collided with a different one. In that case it is blocked and can't be dragged further. The problem is I would like to check this precisely, meaning exactly when two meshes touch each other, and not just by using a bounding box. Is there any way to do this? Thanks for your help!
  17. Hello, i played with the car demo on playground. But im not able to apply collisions to the car. Can anyone tell what i do wrong ? Playground I added: scene.collisionsEnabled = true; // Line 6 carBody.checkCollisions = true; // Line 104 // Also to the wheels wheelFI.checkCollisions = true; // Line 96 wheelRO.checkCollisions = true; // Line 92 wheelRI.checkCollisions = true; // Line 87 wheelFO.checkCollisions = true; // Line 82 // Cubes on ground boxes.checkCollisions = true; // Line 138 But dosnt work, i dont know why.
  18. Physics bug

    Box2D always calls contact callback twice. Here's an example: 185.50.68.103 Ship with collision category 2 (ship at left) calls contact callback once while ship with collision category 1 (default) calls it twice. I've been trying to fix this for weeks but couldn't find any solution in web, i really need you guys to help me. function create() { game.physics.startSystem(Phaser.Physics.BOX2D); game.stage.backgroundColor = '#2d2d2d'; // User-controlled ship ship1 = game.add.sprite(250, 300, 'ship'); game.physics.box2d.enable(ship1); ship1.body.setCircle(14); // Ship with collision category 2 (triggers contact callback only once, it shouldn't even trigger it) ship2 = game.add.sprite(200, 200, 'ship'); game.physics.box2d.enable(ship2); ship2.body.setCircle(14); ship2.body.setCollisionCategory(2); // Ship with collision category 1 *default, i guess* (triggers contact callback TWICE) ship3 = game.add.sprite(300, 200, 'ship'); game.physics.box2d.enable(ship3); ship3.body.setCircle(14); game.physics.box2d.setBoundsToWorld(true, true, true, true, false); ship1.body.setCategoryContactCallback(1, function(b1,b2,f1,f2,begin){ if (begin){ total += 1; hitText.text = 'Contact callback called '+total+' times'; console.log("CONTACT: ", b2) } }, this); cursors = game.input.keyboard.createCursorKeys(); game.add.text(5, 5, 'Use arrow keys to move.', { fill: '#ffffff', font: '14pt Arial' }); total = 0; hitText = game.add.text(5, 30, 'Contact callback called 0 times', { fill: '#ffffff', font: '14pt Arial' }); } function update() { if (cursors.left.isDown) { ship1.body.rotateLeft(300); } else if (cursors.right.isDown) { ship1.body.rotateRight(300); } else { ship1.body.setZeroRotation(); } if (cursors.up.isDown) { ship1.body.thrust(300); } else if (cursors.down.isDown) { ship1.body.reverse(300); } } function render() { game.debug.box2dWorld(); }
  19. Let's say you have a sprite with a collision body of 16px/16px and all the areas to move in the map are exactly 16px/16px as well. It's very hard to maneuver the sprite to be at exactly in the opening to avoid collision letting the player move through it. For example, I attached a gif I made of how Bomberman solves this problem: Bomberman allows the player to clip slightly into the corner of the tile when they get close to the edge so the player starts moving diagonally into the open gap so then the player doesn't have to worry about lining up perfectly. I've looked into doing that but collision detection is AABB on tiles. I also can't just set the players x/y coordinates to be in the right spot because that will break any other collision physics going on. So is there a way to round the corners like in Bomberman or to use velocity to position the player in the opening of the lane perfectly or any other ideas I'm not thinking of? Thanks
  20. My player sprite doesn't collide with the tilemap at all. I've also tried enabling physics for the layers and map, which doesn't work either. I also checked all other topics in this forum about collisions with a sprite and tilemap, but I still can't figure out the error. Here's my code: import { findObjectsByType, movePlayerTo } from './src/map.js'; import { checkPlayerMovement } from './src/movement.js'; const game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render }, false, false); //The player sprite and properties let player, properties = {}; //Settings const worldScale = 2; //Helpers let cursors; //Layers let map, background, walls; function preload() { //Load tiles game.load.spritesheet('tiles', 'assets/Tileset.png', 16, 16); game.load.tilemap('SafeZone1', 'assets/SafeZone1.json', null, Phaser.Tilemap.TILED_JSON); } function create() { //LOGIC game.stage.backgroundColor = "#1c1117"; game.physics.startSystem(Phaser.Physics.ARCADE); setMap('SafeZone1'); player = spawnPlayer(50, 50); cursors = game.input.keyboard.createCursorKeys(); movePlayerTo(player, findObjectsByType('playerStart', map, 'objects')[0], worldScale); game.camera.follow(player, game.camera.FOLLOW_TOPDOWN_TIGHT); //Spawn the player function spawnPlayer(x, y) { const p = game.add.sprite(x, y, 'tiles', 132); game.physics.enable(p); p.scale.setTo(worldScale); p.body.collideWorldBounds = true; return p; } //Add the map with the given name and manipulate it accordingly. function setMap(mapName) { map = game.add.tilemap(mapName); map.addTilesetImage('Tileset', 'tiles'); map.setCollisionBetween(0, 300); //All tiles walls = map.createLayer('walls'); walls.scale.setTo(worldScale); background = map.createLayer('background'); background.scale.setTo(worldScale); background.resizeWorld(); } } function update() { game.physics.arcade.collide(player, map); if(cursors) checkPlayerMovement(player, cursors, 150); } function render() { game.debug.bodyInfo(player, 5, 20); } map.js export function findObjectsByType(type, map, layer) { var result = []; map.objects[layer].forEach(function (element) { if (element.properties.type === type) { element.y -= map.tileHeight; result.push(element); } }); return result; } export function createFromTiledObject(element, group) { var sprite = group.create(element.x, element.y, element.properties.sprite); Object.keys(element.properties).forEach(function (key) { sprite[key] = element.properties[key]; }); } export function movePlayerTo(player, element, worldScale) { player.x = element.x * worldScale; player.y = element.y * worldScale; } movement.js export function checkPlayerMovement(player, cursors, speed) { const vel = player.body.velocity; vel.x = 0, vel.y = 0; if(cursors.left.isDown) vel.x = -speed; if(cursors.right.isDown) vel.x = speed; if(cursors.up.isDown) vel.y = -speed; if(cursors.down.isDown) vel.y = speed; }
  21. So I'm getting these weird collision glitches, where I get knocked back randomly. This only seems to happen when I set my sprite's body's width to 8, as opposed to the sprite's width of 16. I'm using Arcade physics, if that makes a difference.
  22. https://www.babylonjs-playground.com/#SVHR8A I'm trying to create a simple (at least in my mind) scene where I have a ball rolling within the confines of a single mesh "room" I built in blender and exported as .gltf. However, no matter what I do, the ball seems to always go through the imported mesh and hit the ground below (which I kept to make sure the ball doesn't fall forever). Am I doing something wrong? In the playground there isn't a physics engine but locally I've tried cannon.js after reading that it supports MeshImpostors but I get the same result with or without a physics engine.
  23. Destroying coins in collision

    Hi guys! I am newbie to phaser and i would like to ask question and guide me in my project. I am currently developing a game for my project. My game will collect coins and coins that have question to be answered and will be evaluated if the user can play next level. Im using Tiled. And i am having trouble when the collision on the coins it increase the score multiple times before it will gone. Here is my code. Hope you guys help me. getCoin:function(){ map.putTile(-1,layer.getTileX(player.x), layer.getTileY(player.y)); score += 10; scoreText.text = 'Score: ' + score; } It replaces the tile with a tile that didnt exist so i appears to be gone.
  24. P2 sprite collision group pain

    Hi all, Been banging my head against the wall for ages with this. Default P2 collision works, I clearShapes() on each sprite to get rid of these boundary boxes and loadPolygon() to get my custom boundaries (defined in sprite_physics.json). However the collision groups appear not to be working as my player sprite goes straight through/under the enemy. Can anyone see where I'm going wrong? // in index.html (function() { var game = new Phaser.Game(1024, 768, Phaser.AUTO, null) game.state.add('Game', Game); game.state.start('Game'); })(); // in game.js var Game = function (game) { }; Game.prototype = { preload: function () { this.game.load.image('player', 'assets/player.png'); this.game.load.image('enemy', 'assets/enemy.png'); this.game.load.physics('sprite_physics', 'assets/sprite_physics.json'); }, create: function () { this.game.physics.startSystem(Phaser.Physics.P2JS); this.playerCollisionGroup = this.game.physics.p2.createCollisionGroup(); this.enemyCollisionGroup = this.game.physics.p2.createCollisionGroup(); this.createPlayer(); this.createEnemy(); }, update: function () { // <snip> do some steering stuff </snip> }, createPlayer: function () { this.player = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY + 300, 'player'); this.game.physics.p2.enable(this.player, true); // so I can see the polygon's boundaries // Gets rid of current bounding box this.player.body.clearShapes(); // BUT THEN need to add collision cos default p2 collision is wiped with clearShapes() // Add boundary shape from PhysicsEditor this.player.body.loadPolygon('sprite_physics', 'player'); // Seems to do nothing :( this.player.body.setCollisionGroup(this.playerCollisionGroup); this.player.body.collides([ this.enemyCollisionGroup ]); }, createEnemy: function () { this.enemy = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'enemy'); this.game.physics.p2.enable(this.enemy, true); this.enemy.body.clearShapes(); this.enemy.body.loadPolygon('sprite_physics', 'enemy'); this.enemy.body.setCollisionGroup(this.enemyCollisionGroup); this.enemy.body.collides([ this.playerCollisionGroup ]); } }
  25. Hi I am beginner in babylonJs and javascript. I´m doing this, but, I have a lot of questions about how to do certain things and if I´m on the right track http://www.babylonjs-playground.com/#J28NFN#1 1) how can build a line from one object to another? 2) the ball need continue to top and moving on board. who i can do it? 3) who show line in moving clicked mouse and hide after PointerUp 4) who can i destroy a object if ball colide? I am studying the tutorials and I see that this tool is incredibly powerful. Thanks for any help