Angry Bunny Slingshot... collision detection between projectile and flying objects not working!

9 replies [Last post]
pdichone's picture
pdichone
User offline. Last seen 14 hours 35 min ago. Offline
Joined: 10 Jul 2011

Hello fellow Corona Developers!

I have been having some major issues with implementing the "Angry Bunny" slingshot example by FIXDIT Ltd. (Thank you for sharing your code! :).

Anyways, I have the slingshot part to quite well. However, when I try to make the projectile to trigger and event when it hits a dynamic object flying from the bottom of the screen, it works rather strangely. What I mean is that I have noticed that most of the times the projectile goes THROUGH the objects that's supposed to hit and create the event (collision). In other cases, the projectile, according to some print I have included within the collision function, hits the flying dynamic objects, but at a rather strange, x,y location: the collision happens at a different X and Y location on the screen.

Here's the projectile.lua file, from the code example by FIXDIT Ltd.

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
 -- 
-- Abstract: Part of a tutorial documenting how to put together an 'Angry Birds'/'Hot Cross Bunnies' elastic catapult in CoronaSDK.
-- Visit: http://www.fixdit.com regarding support and more information on this tutorial.
-- Hot Cross Bunnies is now available in the iTunes App Store: http://itunes.apple.com/app/hot-cross-bunnies/id432734772?mt=8
--
-- Version: 1.0
-- 
-- Copyright (C) 2011 FIXDIT Ltd. All Rights Reserved.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of 
-- this software and associated documentation files (the "Software"), to deal in the 
-- Software without restriction, including without limitation the rights to use, copy, 
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
-- and to permit persons to whom the Software is furnished to do so, subject to the 
-- following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in all copies 
-- or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
-- DEALINGS IN THE SOFTWARE.
 
-- main.lua (Part of a tutorial documenting how to put together an 'Angry Birds'/'Hot Cross Bunnies' elastic catapult in CoronaSDK.)
 
module(..., package.seeall)
 
  _W = display.contentWidth
     _H = display.contentHeight
-- Pass state reference
state = {};
-- Bullet starts off in-active
ready = false;
-- Pass audio references
shot = {};
band_stretch = {};
 
 
pop = {}
 
local tin_sound           = audio.loadSound("tin_sound.mp3")
 
local playTinSound = function()
          audio.play(tinsound)
        end
        
        function newProjectile()
 
                -- Import easing plugin
        local easingx  = require("easing");
        
        -- Bullet properties
        local bun_bullet = {
                
                name = "fruta",
                type = "bullet",
                density=0.15,
                friction=0.2,
                bounce=0.5,
                size = 26,
                rotation = 1
                
        }
        
        -- Init bullet
        local bullet = display.newImageRect("" .. bun_bullet.name .. ".png", 40,40);
        -- Place bullet
        bullet.x = 149; bullet.y = _H + 123 ;
 
        bullet.myName = "bullet"
        bullet.hit          = "bullet"
        bullet.type        = "bullet"
        bullet.isBullet  = true
        
                -- Set up physical properties   
        physics.addBody(bullet, "kinematic", {density=bun_bullet.density, friction=bun_bullet.friction, bounce=bun_bullet.bounce, radius=bun_bullet.size});
 
        bullet.linearDamping = 0.3;
        bullet.angularDamping = 0.2;
        bullet.isBullet = true;
        bullet.isSensor = true;
        
-- add collision here to see..
           --OnCollision With floor
        local function onBulletCollision(self, event) 
                 bullet.bodyType = "dynamic"
                     if (event.other.hit == "obstacle") then
                          audio.play(tin_sound)
                           print("Hit BARREL")
                     end
                  if ( event.other.hit == "vase" ) then
                        audio.play(tin_sound)
                           print("BULLET!")
                        self:removeSelf()
                        return true
                 
                  end
                
            end
             bullet.collision = onBulletCollision
                bullet:addEventListener( "collision" , bullet )
        
        -- Transition the bullet into position each time it's spawned   
        transition.to(bullet, {time=900, y=_H - 140, transition = easingx.easeOutElastic});
        
                return bullet;
end

And here's the targetVase.lua file which creates a vase object that I later use to spawn flying vases (IGNORE FUNCTION checkThreshold():

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module(..., package.seeall);
 
 _W = display.contentWidth
 _H = display.contentHeight
--Thresholds fed from game.lua
thresholds = {};
 
--Pass the state machine reference
state = {};
 
--Helper function for comparing balloon height (obj1) to threshold (obj2)
local function checkThreshold(obj1,obj2)
        if(obj1 and obj2) then
                if(obj1.y < obj2[1].boundary and obj1.y > obj2[2].boundary) then
                        
                        --Fire a method once, if you want
                        if(obj1.multiplier == nil) then
                                --Do something
                        end
                        
                        --In case you want to see the threshold change in the terminal window
                        --print(obj2[1].multiplier .. ": thresh 1");
                        obj1.multiplier = obj2[1].multiplier;
                
                elseif(obj1.y < obj2[2].boundary and obj1.y > obj2[3].boundary) then
                        
                        --Fire a method once, if you want
                        if(obj1.multiplier == obj2[1].multiplier) then
                                obj1:flash();
                        end
                        
                        --In case you want to see the threshold change in the terminal window
                        --print(obj2[2].multiplier .. ": thresh 2");
                        obj1.multiplier = obj2[2].multiplier;
                elseif(obj1.y < obj2[3].boundary and obj1.y > -10) then
                        
                        --Fire a method once, if you want
                        if(obj1.multiplier == obj2[2].multiplier) then
                                obj1:flash();
                        end
                                                                
                        --In case you want to see the threshold change in the terminal window
                        --print(obj2[3].multiplier .. ": thresh 3");
                        obj1.multiplier = obj2[3].multiplier;
                elseif(obj1.y < -10) then
                        if(obj1.timer) then
                                obj1:pop();
                        end
                end
        end
end
 
function newVase(velocity)
  
      local vase  = display.newImageRect("vase.png", 45, 50)
        
              vase.x = math.random(_W * 0.5 + 139, _W*0.5 + 149)
          vase.y = 400
          vase.rotation = -90
          physics.addBody(vase, { radius = 25});
          --physics.addBody(cherries, {density=1.2, bounce = 0, radius = 5})
          vase.type = "vase"
          vase.hit    = false
          vase.multiplier = nil
          vase.points = 1
          vase.myName = "vase"
          
--[[local balloon = display.newCircle(0, 0, 40);
        balloon:setFillColor(m.random(1,255), m.random(1,255), m.random(1,255));
balloon.x = _W/2; balloon.y = _H+20;
        
        --Set up some custom properties
        balloon.type = "balloon";
        balloon.hit = false;
        balloon.multiplier = nil;
balloon.points = 1;--]]
        
        --Set up properties to store transition.to's
        vase.tween = {start={},finish={}};
        function vase:flash()
                local xS = self.xScale;
                local yS = self.yScale;
                local factor = 0.3
                
                local function tweenBack()
                        transition.to(self, {time=150, xScale = 1, yScale = 1})
                end
                self.tween.finish = tweenBack;
                self.tween.start = transition.to(self, {time=100, xScale = xS + factor, yScale = yS + factor, onComplete = self.tween.finish});
        
        end
        
        
        
                
          -- Collision Detection for the bullet
          function vase:collision(e) 
                if(e.phase == "began") then
                          
                           if (e.other.type == "bullet") then
                                     
                                      print("Hit Vase!!")
                                      audio.play(tin_sound)
                                      -- self: removeSelf()
                                       
                                    local function collide()
                                        --do points and stuff here..
                                        
                                    end
                                     local tmr = timer.performWithDelay(1, collide, 1)
                                     
                                     elseif(e.other.type == "rightWall") then
                                         print("RIGHT WALL") 
                           end
                elseif(e.phase == "ended") then
                
                                --if bullet collides with vase
                                   if (e.other.type == "vase") then
                        
                                         local function collide()
                                                --e.other.
                                         end
                                         
                                         elseif (e.other.type =="floor") then
                                             local function collide()
                                                self:removeSelf()
                                                self = nil
                                             end
                                             local tmr = timer.performWithDelay(1,collide, 1)
                                   end
                end
         
          end --end of bullet function collision
        
        function vase:pop()
        
                --Tell the state machine that a pop has happened
                state:dispatchEvent({name="change", state="pop"})
                
                --Cancel the object's timer
                if(self.timer) then
                        timer.cancel(self.timer);
                        self.timer = nil;
                end
                
                --immediately hide the object;
                self.isVisible = false
                
                if(self.tween.start) then
                        self.tween.start = nil;
                end
                
                if(self.tween.finish) then
                        self.tween.finish = nil;
                end
                
                self.tween = nil;
                
                --Finally, remove the object
                self:removeSelf();
                self = nil;
                collectgarbage("collect");
        end
        
        --Start checking the y threshold continuously using the helper function defined earlier
        vase.timer = timer.performWithDelay(20, function(e)
                checkThreshold(balloon, thresholds);
        end, -1)
        
        physics.addBody(vase, "kinematic", {density=0, bounce=0, friction=0, radius=40});
        vase:setLinearVelocity(0,velocity * -2.5);
        
        return vase;
 
end

And here's PART of main.lua file where I call all other files for instantiating the vase and slingshot etc...

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
--------------------------
        --THE HEART OF SLINGSHOT-
        --------------------------
 
 
 -- BULLET COLLISION
--SPAWN CHERRIES UPWARDS--
local spawnCherries = function()
      physics.setGravity(0,   - 0.6);
      --cherries = display.newImage("cherries.png", math.random(206), 406);
       cherries  = display.newImageRect("vase.png", 45, 50)
             -- cherries = display.newImageRect("cherries.png", 40,40)
        --  cherries.x = math.random( 12, 50)
              cherries.x = math.random(_W * 0.5 + 139, _W*0.5 + 149)
          cherries.y = 400
          cherries.rotation = -90
          physics.addBody(cherries, { radius = 25});
          --physics.addBody(cherries, {density=1.2, bounce = 0, radius = 5})
          cherries.hit = "cherries"
          cherries.myName = "cherries"
 
local function splashPoints300(object) 
        gushCircle = display.newImageRect ( "points_300.png",64, 64 )
            gushCircle.x = _W*0.5 + 200
            gushCircle.y  = _H*0.5 + 12
        
        function transtion1( event )
            trans = transition.to(gushCircle, { tim = 1000, alpha = 0.8, onComplete=finishFunction1})
        end
        
        function finishFunction1( event )
                  trans = transition.to(gushCircle, { time = 10100, alpha = 0, onComplete=transition1})
        end
                  
                  transition.to(gushCircle, { time = 1600,  y = gushCircle.y - 110, x = gushCircle.x +20, alpha = 0,onComplete= transition1} )
        
end
        
        local function splashAnimation800(object)
         
             audio.play(tin_sound)
                 print("Vase Hit from splash!")
            gushCircle = display.newImageRect ( "grapes_splash.png",64, 64 )
            gushCircle.x = _W*0.5 + 200
            gushCircle.y  = _H*0.5 + 59
        
        function transtion1( event )
            trans = transition.to(gushCircle, { time = 1000, alpha = 0.8, onComplete=finishFunction1})
        end
        
        function finishFunction1( event )
                  trans = transition.to(gushCircle, { time = 10100, alpha = 0, onComplete=transition1})
        end
                  
                  transition.to(gushCircle, { time = 1600,  y = gushCircle.y - 110, x = gushCircle.x + 24, alpha = 0,onComplete= transition1} ) 
        
        
          
end
           
           --onCollision With barrel
            local function onObstacleCollision(self, event) 
                  
                  if ( event.other.hit == "obstacle" ) then
                        --audio.play(tin_sound)
                        --splashAnimationGrapes()
                        print("HIT OBSTACLE!")
                        splashAnimation800()
                        splashPoints300()
                           --print("Vase Hit!")
                        self:removeSelf()
                        return true
                         elseif ( event.other.hit =="rightWall") then
                                        print("HIT WALL!")
                                
                        
                  end
                
            end
            obstacle.collision = onObstacleCollision
            obstacle:addEventListener( "collision" , obstacle )
           
            --OnCollision With floor
            local function onCherriesCollision(self, event) 
                  
                  if ( event.other.hit == "bullet" ) then
                        --audio.play(tin_sound)
                        --splashAnimationGrapes()
                        splashAnimation800()
                        splashPoints300()
                           --print("Vase Hit!")
                        self:removeSelf()
                        return true
                         elseif ( event.other.hit =="rightWall") then
                                 self:removeSelf()
                                        print("HIT WALL!")
                                
                        
                  end
                
            end
            cherries.collision = onCherriesCollision
            cherries:addEventListener( "collision" , cherries )
        end
 
 
local function spawnVase(number)
                
                local function spawn(e)
                        --Create an instance of a balloon
                        --and assign it a random linear velocity
                --      local v = vases.newVase(m.random(100,370));
                        local v = vases.newVase(m.random(50, 100))
                        
                        --Store the balloon instance in the balloons table
                        --using the table number as the index
                        vaso[v] =v;
                        vaso[v].x = m.random(_W * 0.5 + 139, _W*0.5 + 149);
                        --Flag the balloon for removal later
                        vaso[v].remove = true;
                        
                        --insert the balloon into the foreground group
                        localGroup:insert(vaso[v]);
                        
                        --cancel and nil timer when done
                        if(e.count == number) then
                                timer.cancel(tmr);
                                tmr = nil;
                        end
                        
                end
                
                tmr = timer.performWithDelay(2000, spawn, number);
        
        end
        
 
 
local function spawnProjectile()
 
        -- If there is a projectile available then...
        if(projectile.ready)then
        
                projectiles_container = projectile.newProjectile();
                -- Flag projectiles for removal
                projectiles_container.ready = true;
                projectiles_container.remove = true;
                
                -- Reset the indexing for the visual attributes of the catapult.
                slingshot_container:insert(slingshot_strut_back);
                slingshot_container:insert(projectiles_container);
                slingshot_container:insert(slingshot_strut_front);
                
                ---Add an event listener to the projectile.
                projectiles_container:addEventListener("touch", projectileTouchListener);
                
        end
 
end
--[[
 
GAME STATE CHANGE FUNCTION
 
]]--
function state:change(e)
 
        if(e.state == "fire") then
        
                --- You fired...
                -- new projectile please
                spawnProjectile();
                        
        end
 
end
 
    -------------------
    ---Change Scene-----
    -------------------
    local moveToScene = function(event)
 
      --Example scene change with parameters
      --director:changeScene( { label="Scene Reloaded" }, "screen2","moveFromRight" )
 
      --Example scene change without parameters
      --director:changeScene( "screen1", "crossfade" )
 
 
    end
 
        ------------------
        -- Code here
        ------------------
         background_image = display.newImageRect("mode_3_bg.png", 480, 320)
         
         slingshot_strut_front = display.newImage("sling_left.png", true)
         slingshot_strut_back = display.newImage("sling_right.png", true)
         --spring                                        = display.newImageRect("spring_good.png", 98, 98)
         obstacle       = display.newImageRect("barrel.png", 64, 64)
         obstacle.myName = "obstacle"
         obstacle.hit  = "obstacle"
 
         
         leftWall                      = display.newRect(0,0,0, _H);
     leftWall.myName        = "leftWall"
     rightWall                    = display.newRect(_W, 0, 1, _H);
     rightWall.hit                 = "rightWall"
     
         --invisible_floor           = display.newImageRect("invisible_floor.png", 980, 1)
         
 
        --====================================================================--
        -- INITIALIZE, Every Display Object must get shoved into the local Display Group
        -- Example:     localGroup:insert( background )
        --====================================================================--
        local initVars = function ()
        
                localGroup:insert(background_image)
                localGroup:insert(leftWall)
                localGroup:insert(rightWall)
                localGroup:insert(obstacle)
                --localGroup:insert(invisible_floor)
                
                slingshot_container:insert(slingshot_strut_front)
                slingshot_container:insert(slingshot_strut_back)
                --slingshot_container:insert(projectiles_container)
                
                --localGroup:insert(spring)
                
                
                -- Transfer variables to the projectile classes
         projectile.shot = shot
         projectile.band_stretch = band_stretch
                
                --POSITION OBJECTS
                background_image.x = _W*0.5
                background_image.y = _H*0.5
                
                 --[[invisible_floor.x = _W;
             invisible_floor.y = _H;
             physics.addBody ( invisible_floor, "static", {bounce=0} )
             invisible_floor.hit = "floor"
                 invisible_floor.myName = "floor"--]]
                
                --slingshot
                slingshot_strut_front.x = 138
                slingshot_strut_front.y = _H - 123
                slingshot_strut_front.rotation = -30
                
                slingshot_strut_back.x = 189
                slingshot_strut_back.y = _H - 147
                slingshot_strut_back.rotation = -23
                
                obstacle.x = _W*0.5 + 120
                obstacle.y = _H - 45
                
                --[[spring.x = _W*0.5 + 230
                spring.y  = _W*0.5 - 200
                spring.hit = "trampoline"
            physics.addBody(spring, "static", { bounce = 1.4, friction = 0.4})
                spring.rotation = 230--]]
                
                -- Move catapult up
        slingshot_container.y = 98;
        slingshot_container.x = -98 
        
        
        
        --WRAPPIN WALLS  WITH  PHYSICS ENGINE --
       physics.addBody(leftWall, "static", {bounce = 0.01});
       physics.addBody(rightWall, "static", {bounce = 0.01});
       physics.addBody(obstacle, "static", {bounce = 0.04})
       
       
       ----LISTENERS---
       -- Tell the projectile it's good to go!
          projectile.ready = true;
          -- Spawn the first projectile.
         spawnProjectile();
        -- Create listnener for state changes in the game
           state:addEventListener("change", state);
           
          timerSpawnCherries = timer.performWithDelay( 2000, spawnCherries, 0) 
          -- spawnVase(21)
        
 
    end

I have been trying to figure this one out for a while now, and I can't seem to find the solution for this. Essentially, all I want to be able to do is have the player stretch the slingshot( which is working fine), and launch the projectile, which in return will hit flying objects and destroy/break that object (or objects if it hits more than one at once). That's all I want to be able to do. Your help will be highly appreciated.

Thank you in advance.

Replies

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

Wow, that's long - you would be better off looking at Premium Support for this because to go through your code would take a good amount of time.

Alternatively you could try to isolate the parts of your code that aren't working correctly and share those - that would be much easier for people to have a crack at.

Peach :)

pdichone's picture
pdichone
User offline. Last seen 14 hours 35 min ago. Offline
Joined: 10 Jul 2011

You are absolutely right, Peach! I should not have put all that code in here... what was I thinking :(. I was pretty desperate, I guess! :) My apologies.

I will follow your advice. Thanks again, Peach!

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

Heh, no need to apologize, I'd just like to see it get sorted which is a lot easier with less code ;)

Peach :)

Incicive
User offline. Last seen 12 weeks 4 days ago. Offline
Joined: 16 Dec 2011

I have the same problem, I cannot get the projectile to collide with physics body,

any fixs yet ?

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

Incicive - read the first comment. Code is too long, isolate problem then ask.

If projectile isn't colliding you need to also specify details - is it flying straight though, what body type is it, are objects sleeping, etc.

Incicive
User offline. Last seen 12 weeks 4 days ago. Offline
Joined: 16 Dec 2011

Might it have anything to do with the "Bounce=0.5" ?
Because on the tutorial for collisions it mentioned they have to be equal to collide, but i tried it and it doesn't seem to work.

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

Bounce doesn't have to be equal to collide, no.

There it a tutorial on collision on Techority, Corona for Newbies - Part 4 :)

Incicive
User offline. Last seen 12 weeks 4 days ago. Offline
Joined: 16 Dec 2011

Still trying too figure this out :S

I don't think it has anything too too with the Kinematic or dymaic or even static settings,
But I'm confused completely, could you link me this newbies tutorial please ?

peach pellen's picture
peach pellen
User offline. Last seen 1 day 12 hours ago. Offline
Staff
Joined: 12 Apr 2011

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.