Changing particle emitter position while using SetEmitterTarget()

Hello,

I’ve been having some trouble getting my particle emitter to emit particles from correct position after setting it to follow my “player” object.

Here’s the case: I’ve created a turret object which has body and changeable weapon (one or more barrels). The body and barrels have been inserted in to same group which is my “player” object/group.

Then I’ve placed the player group to correct spot on the screen and rotating the whole group (body and barrel(s) are turning at the same time) and it’s working.

But when I set my emitter to follow this player group it emits particles from the center reference point (default). Of course I can change the reference point to, for example top center, but then my player group also rotates around that point => not what I want.

So what is the correct way of doing this? I do need my turret to consist of 2 or more objects because I want to be able to attach more stuff to it like additional turrets etc. Still I would like to use particle emitter(s) to emit ammunition…

Here’s something I’ve been doing so far:

[code]
local M = {}

local new = function ()

local stageHeight = display.contentHeight
local stageWidth = display.contentWidth


– Groups

local localGroup = display.newGroup()


– Your code here

– body of the turret which can have different type and number of barrels (weapons)
local body = display.newImage(“gun_turret_body.png”)
body.x = 0
body.y = 0

– defender/turret group
local defender = display.newGroup()

– table for weapons
weapon = {}

– create turret (1 barrel)
weapon[“turret”] = display.newImage(“turret_barrel.png”)
weapon[“turret”].x = 0
weapon[“turret”].y = -40
weapon[“turret”].isVisible = false
–weapon[“turret”]:setReferencePoint(display.TopCenterReferencePoint)

– create machinegun, 2 barrels
local barrel1 = display.newImage(“turret_barrel_small.png”)
local barrel2 = display.newImage(“turret_barrel_small.png”)
local machinegun = display.newGroup()
machinegun:insert(barrel1)
machinegun:insert(barrel2)
barrel1.x = -20
barrel2.x = 20
weapon[“machinegun”] = machinegun
weapon[“machinegun”].x = 0
weapon[“machinegun”].y = -50
weapon[“machinegun”].isVisible = false

– insert weapons into defender-group
defender:insert(weapon[“turret”])
defender:insert(weapon[“machinegun”])
defender:insert(body)

– place defender (turret group) to right place
defender.x = stageWidth/2
defender.y = stageHeight-30
defender.rotation = 0
–defender:setReferencePoint(display.TopCenterReferencePoint)
– Setting reference point for turret group makes following emitter to be in correct place but then turret rotation is wrong

– handle turret firing (only rotates defender/turret group for now)
local fireTurret = function(event, angle)
print(“fireTurret”)
if event.phase == “began” then
transition.to(defender,
{ rotation = angle, time = 300,
onComplete = function(self)
end
}
)
end
end

– assign firing function for each weapon
weapon[“turret”].fire = fireTurret
weapon[“machinegun”].fire = fireTurret

– CREATE AN EMITTER (NAME, SCREENW, SCREENH, ROTATION, ISVISIBLE, LOOP)
Particles.CreateEmitter(“TurretEmitter”, 0, 0, 0, true, true)

– DEFINE PARTICLE TYPE
local Properties = {}
Particles.CreateParticleType (“Shots”,
{
imagePath = “shot.png”,
imageWidth = 32,
imageHeight = 32,
velocityStart = 350,
autoOrientation = true,
killOutsideScreen = true,
lifeTime = 3000,
alphaStart = 1.00,
– APPLY PHYSICS:
PhysicsMaterial = { density = 0, friction = 0, bounce = 0 },
PhysicsProperties = { isFixedRotation = true, isSleepingAllowed = true, bodyType = “kinematic”, isSensor = true, name = “SHOT” }
} )

– FEED EMITTER (EMITTER NAME, PARTICLE TYPE NAME, EMISSION RATE, DURATION, DELAY)
Particles.AttachParticleType(“TurretEmitter”, “Shots”, 10, 99999, 0)

– TELL EMITTER TO FOLLOW
Particles.SetEmitterTarget( “TurretEmitter”, defender, true, 0 ) – follows defender/turret group atm but emitter is in the center…
–Particles.SetEmitterTarget( “TurretEmitter”, weapon[“turret”], true, 0 )
– If turret weapon is the target, the emitter will follow turret (barrel) but in its own coordinates 0, -40 so not shown in the screen
– So am I doing something wrong when constructing weapon’s this way and then rotation the whole defender/turret group?

local emitter = Particles.GetEmitter(“TurretEmitter”)
emitter.y = -70 – this does not seem to change emitter’s position because SetEmitterTarget is used…

– start turret emitter
Particles.StartEmitter(“TurretEmitter”, false)

– AUTO UPDATE PARTICLE ENGINE
Particles.StartAutoUpdate()

– Create player which holds only some player attributes
local player = {}
player.current_weapon = “turret” – current selected weapon, which will be set visible

– show weapon that is selected
weapon[player.current_weapon].isVisible = true;

– background image
local background = display.newImage(“background.png”)

– calculate angle for turret
local PI = 180 / (4*math.atan(1))
local fireWeapon = function( event )
local angle = ( math.atan2( (defender.y-event.y), (defender.x-event.x) ) * PI ) - 90
– fire selected weapon (rotate barrel atm with following emitter)
weapon[player.current_weapon].fire(event, angle)
end

– add event listener for weapon firing
background:addEventListener(“touch”, fireWeapon)


– MUST return a display.newGroup()

return localGroup

end

M.new = new

return M
[/code] [import]uid: 55867 topic_id: 17846 reply_id: 317846[/import]

I ended up modifying SetEmitterTarget method and added functionality to set emitterRadius and emitterAngle which can be used to achieve what I wanted to do :slight_smile: [import]uid: 55867 topic_id: 17846 reply_id: 68550[/import]

Yes, that’s a good way to do it.

Also note the latest update which adds an optional x- and y-offset to the SetEmitterTarget command.

[import]uid: 10504 topic_id: 17846 reply_id: 70758[/import]

Thanks for the reply and update :slight_smile:

I didn’t have time to test this new x and y offset functionality until just recently and it works but I needed my emitter(s) also to follow target’s rotation.

Here are couple of sample videos from my project to see how emitters are following rotated object (although emitters are not visible in these videos):

http://www.youtube.com/watch?v=tQEKZ5ltWBU (machinegun)
http://www.youtube.com/watch?v=m4jPBCXVpns (missile launcher)
[import]uid: 55867 topic_id: 17846 reply_id: 71454[/import]