Bavragor Posted February 9, 2016 Share Posted February 9, 2016 Hi, As the title already reveals: I'm trying to capture when two objects are colliding for 90% of their coordinates. Yet the Phaser update method was too slow, so I called the collision function in a timer event that ticks every millisecond. Yet it still isn't registering the collision, which hardly seems possible because the sprite isn't moving that fast, anyone has a clue? The timer event: this.game.time.events.loop(1, function(){this.collides, (this.shootingCircle, this.targetCircle)}, this); The collision function: collides: function(a, b) { if(a.y < (b.y * 1.1) && a.y > (b.y * 0.9) && a.x < (b.x * 1.1) && a.x > (b.x * 0.9)) { if (this.alertSent == false) { alert('You Won'); this.alertSent = true; } var self = this; setTimeout(function() { self.moving = false; self.aiming = false; self.alertSent = false; self.game.state.start('Game'); }, 350); } }, Link to comment Share on other sites More sharing options...
fillmoreb Posted February 9, 2016 Share Posted February 9, 2016 You've got a tunneling problem. There are a few solutions, but they're kind of in-depth to go into here. Here's a web page that discusses this: When Bullets Move Too Fast Looks like the site is being spotty right now, so here's the google cached version as well. Google Cache Version zappa 1 Link to comment Share on other sites More sharing options...
Bavragor Posted February 10, 2016 Author Share Posted February 10, 2016 I understand the offered solutions, atleast the theory of it. But does anyone have a 'real' solution codewise? Or am I supposed to slow down my sprite's movement until the collision works properly. Buying box2d as a starter developer, merely for the ccd, seems to be a bit overextending. Link to comment Share on other sites More sharing options...
Bavragor Posted February 10, 2016 Author Share Posted February 10, 2016 Could I solve this problem by using the P2 Physics instead of the Arcade Physics? Link to comment Share on other sites More sharing options...
drhayes Posted February 10, 2016 Share Posted February 10, 2016 There are a couple of problems I can see. Tunneling is one of them, for sure. I'm not super familiar with p2, but probably not. If something is 16px wide and a bullet's speed is 1200 pixels per second, then the bullet is gonna tunnel through that thing. It will even do it at slower speeds if the physics simulation runs slower than 60fps. Also, JS can't do timer ticks every millisecond. The resolution of setTimeout and setInterval doesn't go lower than 10ms on any browser. This code: this.game.time.events.loop(1, function(){this.collides, (this.shootingCircle, this.targetCircle)}, this); ...isn't legal. Inside that callback you've written "this.collides, (this.shootingCircle, this.targetCircle)". That's not a function invocation but its still legal JS. You probably meant "this.collides(this.shootingCircle, this.targetCircle)" with no comma in between. In Arcade physics there's a property called "tilePadding", but I *think* it's only for tilemaps. Link to comment Share on other sites More sharing options...
Bavragor Posted February 10, 2016 Author Share Posted February 10, 2016 You can 't use this.collides(this.shootingCircle, this.targetCircle). Getting an error with 'callback...', can't recall properly, but have had extensive problems with it. Mainly you can't call a function that needs arguments inside the loop. If this.collides() would have been an option it would have worked. That aside could it be that the code I'm executing in this.collides(a, b ) requires too much time. Aka if I could simplify that code, it might work? Link to comment Share on other sites More sharing options...
Bavragor Posted February 10, 2016 Author Share Posted February 10, 2016 @drhayes Never mind about what I said in my previous post, you we're right. The code I wrote there wasn't 'correct'. I've changed it now. Thanks for the help. Link to comment Share on other sites More sharing options...
rcoaxil Posted February 10, 2016 Share Posted February 10, 2016 When your collidables move too fast, you only have two options: increase collision check rate, or make sweep collision test. First option may only take adjusting physics engine update rate, provided it has support for such feature. Second option also requires physics engine to have such feature and is also only takes checking a flag. As far as I'm aware, none of three default physics engines support second option, but increasing update rate is an option for P2 engine. Link to comment Share on other sites More sharing options...
Recommended Posts