Why bullet object is not bouncing off walls at proper angle?

I’m firing a bullet and when it strikes a wall it bounces off at a 90 degree angle instead of at the proper angle. But when I fire a second bullet and it strikes the first bullet, it does bounce off the other bullet at the proper angle.

How do I make the walls behave like the bullets as it relates to ricochets?

Here’s a portion of my code when I fire the bullet, then when the bullet collides with a wall, and then it ricochets off the wall:

function fireBullet(source, angle) bullet = display.newImage("bullet.png") bullet.myName="bullet" bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle physics.addBody( bullet, "dynamic",{density=.0, bounce=1, friction=1,radius=15}) bullet.isFixedRotation = true local direction = rotateTo( {x=2,y=0}, angle ) bullet:applyForce( direction.x, direction.y, bullet.x, bullet.y ) bullet:addEventListener( "collision", bulletHit) end local locationX local locationY function bulletHit(event,angle) locationX = event.target.x locationY = event.target.y print("bullet hit count is ",count) if ( event.phase == "began" ) then timer.performWithDelay(1,function(other,target,angle) return ricochet(self,event)end) end end function ricochet(self,event ) event.target:applyForce(( event.target.x-event.x), (event.target.y-event.y), event.target.x,event.target.y ) bullet.isBullet = true print("bullet angle is ", bullet.direction) bullet.rotation= bullet.rotation\*2 print("this is bullet rotation angle after it should have changed- ",bullet.rotation) count=count+1 return true end

Hi @hackbrew,

May I ask why you’re doing all the math and applying force? Why not just let the physics engine handle it internally?

Best regards,

Brent

Hi @Brent Sorrentino,

I’m not sure either, as I’m a newbie to this.  I will try to eliminate those things and see how it runs.

Thanks,

Hackbrew

Hi @Brent Sorrentino,

Okay, I modified the code and removed a bunch of stuff.  Here’s the code which is pretty basic, but it still exhibits the same problem.

I also included the scene:create function thinking that it may have something to do with the issue.  Where else should I look?

function fireBullet(event, angle) bullet = display.newImage("bullet.png") bullet.myName="bullet" bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle physics.addBody( bullet, "dynamic",{density=.0, bounce=1, friction=1,radius=15}) bullet:addEventListener( "collision", bulletHit) end function bulletHit(event,angle) if ( event.phase == "began" ) then count=count+1 end end 

function scene:create( event )

    local sceneGroup = self.view

    – Called when the scene’s view does not exist

    – 

    – INSERT code here to initialize the scene

    – e.g. add display objects to ‘sceneGroup’, add touch listeners, etc

background = display.newImage(“bg.png”)

sceneGroup:insert(background)

topbar = display.newImage(“bar.png”)

topbar.myName = “topbar”

topbar.x = -10

topbar.y = 10

sceneGroup:insert(topbar)

bottombar = display.newImage(“bar.png”)

bottombar.x = -10

bottombar.y = 310

bottombar.myName = “bottombar”

physics.addBody(bottombar, “static”)

sceneGroup:insert(bottombar)

leftside = display.newImage(“side.png”)

leftside.x = 5

leftside.y = 0

physics.addBody(leftside,“static”)

leftside.myName = “leftside”

sceneGroup:insert(leftside)

rightside = display.newImage(“side.png”)

rightside.x = 470

rightside.y = 0

physics.addBody(rightside,“static”)

rightside.myName = “rightside”

sceneGroup:insert(rightside)

end

Thanks,

Hackbrew

Give the ‘bullet’ a circular body by specifying a radius.  Then, you’ll get ricochets that are closer to what you expect.

You may also need to play with the bounce and friction settings for the ‘bullet’

In the fireBullet function I did have the radius equal to 15 like… (physics.addBody( bullet, “dynamic”,{density=.0, bounce=1, friction=1,radius=15}) ).  I modified the code to be pretty basic.  So now when I fire a bullet, the bullet hits the wall and bounces at a fairly correct angle on the first bounce, but beyond the first bounce it pretty much just bounces vertically up and down in a straight line.  Although, when a bullet that’s been fired collides with another bullet, it seems to bounce at a correct angle.  So I’m not sure why or what’s causing that? 

function fireBullet(source, angle) bullet = display.newImage("bullet.png") bullet.myName="bullet" bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle physics.addBody( bullet, "dynamic",{density=.0, bounce=1, friction=1, radius=15}) bullet:applyForce( 5, 5, bullet.x, bullet.y ) bullet:addEventListener( "collision", bulletHit) end function bulletHit(event,angle) if ( event.phase == "began" ) then count=count+1 end end

I’ll look into this, but one thing I noticed is you are trying to set density to 0.  You shouldn’t do that.  Mass directly correlates to density and if you have no mass (or 0), then your calculations will break down.  If you want the bullet to not ‘push’ the wall with its mass, make the wall bodies “kinematic”.

Meanwhile, consider trying these things:

  • Try friction 0

  • Visually inspect/verify the body shape by enabling ‘hybrid’ rendering in physics.

  • Try setting the isFixedRotation attribute to see how this affects subsequent bounces.

Thanks @roaminggamer, setting the friction to 0 did the trick!

Hi @hackbrew,

May I ask why you’re doing all the math and applying force? Why not just let the physics engine handle it internally?

Best regards,

Brent

Hi @Brent Sorrentino,

I’m not sure either, as I’m a newbie to this.  I will try to eliminate those things and see how it runs.

Thanks,

Hackbrew

Hi @Brent Sorrentino,

Okay, I modified the code and removed a bunch of stuff.  Here’s the code which is pretty basic, but it still exhibits the same problem.

I also included the scene:create function thinking that it may have something to do with the issue.  Where else should I look?

function fireBullet(event, angle) bullet = display.newImage("bullet.png") bullet.myName="bullet" bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle physics.addBody( bullet, "dynamic",{density=.0, bounce=1, friction=1,radius=15}) bullet:addEventListener( "collision", bulletHit) end function bulletHit(event,angle) if ( event.phase == "began" ) then count=count+1 end end 

function scene:create( event )

    local sceneGroup = self.view

    – Called when the scene’s view does not exist

    – 

    – INSERT code here to initialize the scene

    – e.g. add display objects to ‘sceneGroup’, add touch listeners, etc

background = display.newImage(“bg.png”)

sceneGroup:insert(background)

topbar = display.newImage(“bar.png”)

topbar.myName = “topbar”

topbar.x = -10

topbar.y = 10

sceneGroup:insert(topbar)

bottombar = display.newImage(“bar.png”)

bottombar.x = -10

bottombar.y = 310

bottombar.myName = “bottombar”

physics.addBody(bottombar, “static”)

sceneGroup:insert(bottombar)

leftside = display.newImage(“side.png”)

leftside.x = 5

leftside.y = 0

physics.addBody(leftside,“static”)

leftside.myName = “leftside”

sceneGroup:insert(leftside)

rightside = display.newImage(“side.png”)

rightside.x = 470

rightside.y = 0

physics.addBody(rightside,“static”)

rightside.myName = “rightside”

sceneGroup:insert(rightside)

end

Thanks,

Hackbrew

Give the ‘bullet’ a circular body by specifying a radius.  Then, you’ll get ricochets that are closer to what you expect.

You may also need to play with the bounce and friction settings for the ‘bullet’

In the fireBullet function I did have the radius equal to 15 like… (physics.addBody( bullet, “dynamic”,{density=.0, bounce=1, friction=1,radius=15}) ).  I modified the code to be pretty basic.  So now when I fire a bullet, the bullet hits the wall and bounces at a fairly correct angle on the first bounce, but beyond the first bounce it pretty much just bounces vertically up and down in a straight line.  Although, when a bullet that’s been fired collides with another bullet, it seems to bounce at a correct angle.  So I’m not sure why or what’s causing that? 

function fireBullet(source, angle) bullet = display.newImage("bullet.png") bullet.myName="bullet" bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle physics.addBody( bullet, "dynamic",{density=.0, bounce=1, friction=1, radius=15}) bullet:applyForce( 5, 5, bullet.x, bullet.y ) bullet:addEventListener( "collision", bulletHit) end function bulletHit(event,angle) if ( event.phase == "began" ) then count=count+1 end end

I’ll look into this, but one thing I noticed is you are trying to set density to 0.  You shouldn’t do that.  Mass directly correlates to density and if you have no mass (or 0), then your calculations will break down.  If you want the bullet to not ‘push’ the wall with its mass, make the wall bodies “kinematic”.

Meanwhile, consider trying these things:

  • Try friction 0

  • Visually inspect/verify the body shape by enabling ‘hybrid’ rendering in physics.

  • Try setting the isFixedRotation attribute to see how this affects subsequent bounces.

Thanks @roaminggamer, setting the friction to 0 did the trick!