Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Bullets spawn but no movement when shot.
Started by n8anquirke Apr 07 2019 02:02 PM

2 replies to this topic
lua shooting mechanics
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK

 [lua] function shoot:tap(e)
    local bullet = display.newImageRect('bullet.png', 15, 10)
		bullet.x = player.x
		bullet.y = player.y
    bullet.name = 'bullet'
    bullet.isSensor = true

-- rotation
local enterFrame
enterFrame = function( event )

		local position = puggle.mouse:getPosition( true )
		local angle = puggle.maths:angleBetweenVectors( bullet, position )
		player.rotation =  angle - 90
		local vector = puggle.maths:vectorFromAngle( angle )
		bullet.x = bullet.x + vector.x
		bullet.y = bullet.y + bullet.y

Runtime:addEventListener( "enterFrame", enterFrame ) [/lua]

Hi all, I am currently trying to produce bullets and shoot them in the direction of my mouse cursor. They currently spawn on the players position and the image of the bullet appears but it does not move. I have tried a few methods seen on these forums posts but nothing worked yet. Should i use some sort applyForce code or is there an easier way to move this. It is not linear movement. My player can rotate 360 degrees just in case i confuse anyone. These bullets will also be shot at an enemy so I assume its easier to give the enemy collision and not the bullets themselves. Thanks in advance.

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 429 posts
  • Corona SDK

In my current project this is done using setLinearVelocity.

local vx,vy = angle2VecDeg( player.rotation-90 )
local pvx, pvy = player:getLinearVelocity()
bullet:setLinearVelocity ( pvx + vx * bulletSpeed, pvy + vy * bulletSpeed ) 

As the player can be moving at varying speeds I have to add the x/y velocity of the player to the x/y velocity of the bullet so that the bullet will always fire forwards regardless of the speed of the player.

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 18 posts
  • Corona SDK

I don’t use the physics engine a lot so I could be wrong but I believe the reason your bullets aren’t moving is because Corona doesn’t like you moving physics object in code.

The way I would approach things if I where doing it is by moving the bullets at the players current speed plus the speed you want the bullets to go that way the bullet is always firing forwards no matter the speed of the player just like Appletreeman said and I would write my own collision detection function and that way the physics engine would no longer be needed. (Unless you want some kind of rag doll system or something similar in which case Appletreeman’s solution would be best)

Hopefully that was helpful :)


Also tagged with one or more of these keywords: lua, shooting, mechanics