Jump to content

Sprite movement too quick for timer event to capture


Bavragor
 Share

Recommended Posts

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

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

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

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

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

 Share

  • Recently Browsing   0 members

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