Asteroids wrapping fail

Hello everyone, I still write about my Asteroids game. I created four bumpers for the asteroids wrapping , but sometimes the sensors fail to detect contact.

I had to create a function of self-destruction that continuously monitors the position of the asteroids in the case have exceeded the bumpers, but I don’t think is good for performance, I’d like it all worked cleanly. I would understand if it is my fault, or is possbile that in the presence of multiple collisions, a sensor may fail to detect some. How can I fix?

Can’t really help you without seeing some code. You’ve only vaguely described what you’ve done, but nothing we can actually help with. Are you going to post some code?

Listener for bumper sx:

[lua]

local function bsx(event)

    if event.phase==“began” then

        local vx,vy=event.other:getLinearVelocity( )

        if (event.other.id==“asteroid” and vx<0 then

            local wrap=function() event.other.x=X+10 end

            local timerWrap=timer.performWithDelay(10,wrap)

            

        end

    end

end

[/lua]

Hi @marcoguerrera84,

There’s no chance (well, it’s virtually impossible) that the sensors are occasionally “missing” some collisions. The only way that would occur is if an asteroid was moving so fast, it somehow skipped over the entire sensor bounds in a single time step.

Why are you checking for “vx<0” in your conditional code? What if the asteroids are moving to the right and thus have a positive linear x velocity? That would, of course, cause the event to fail.

Best regards,

Brent

If it’s impossible, I have to search the error in my code, this help me. I’ll make some test. 

I use the condition vx<0 because the left bumper get asteroids with negative vx. If the asteroid has been wrapped from the right bumper, he has vx>0 and he can go through left bumper to the visible screen.

Hi @marcoguerrera84,

You may want to consider a better approach for this. Instead of creating 4 bumpers outside the screen, and dealing with detection on all 4 bumpers and the x/y velocity the asteroid is going in, I suggest you create one screen-sized (or slightly larger) sensor region that covers the entire screen. Then, instead of detecting the “began” phase of collisions, detect the “ended” phase… that being when any asteroid exits the sensor region (which is the visible screen, basically). Then, on that event, you could detect its velocity and position it on the flip side of the screen going in the same direction.

Brent

Can’t really help you without seeing some code. You’ve only vaguely described what you’ve done, but nothing we can actually help with. Are you going to post some code?

Listener for bumper sx:

[lua]

local function bsx(event)

    if event.phase==“began” then

        local vx,vy=event.other:getLinearVelocity( )

        if (event.other.id==“asteroid” and vx<0 then

            local wrap=function() event.other.x=X+10 end

            local timerWrap=timer.performWithDelay(10,wrap)

            

        end

    end

end

[/lua]

Hi @marcoguerrera84,

There’s no chance (well, it’s virtually impossible) that the sensors are occasionally “missing” some collisions. The only way that would occur is if an asteroid was moving so fast, it somehow skipped over the entire sensor bounds in a single time step.

Why are you checking for “vx<0” in your conditional code? What if the asteroids are moving to the right and thus have a positive linear x velocity? That would, of course, cause the event to fail.

Best regards,

Brent

If it’s impossible, I have to search the error in my code, this help me. I’ll make some test. 

I use the condition vx<0 because the left bumper get asteroids with negative vx. If the asteroid has been wrapped from the right bumper, he has vx>0 and he can go through left bumper to the visible screen.

Hi @marcoguerrera84,

You may want to consider a better approach for this. Instead of creating 4 bumpers outside the screen, and dealing with detection on all 4 bumpers and the x/y velocity the asteroid is going in, I suggest you create one screen-sized (or slightly larger) sensor region that covers the entire screen. Then, instead of detecting the “began” phase of collisions, detect the “ended” phase… that being when any asteroid exits the sensor region (which is the visible screen, basically). Then, on that event, you could detect its velocity and position it on the flip side of the screen going in the same direction.

Brent