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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
views:1455 update:2011/11/18 8:53:10
corona forums © 2003-2011