collision help with the fishery animation

[code]

local physics = require(“physics”)
physics.start()

physics.setScale( 60 ) – a value that seems good for small objects (based on playtesting)
physics.setGravity( 0, 9) – overhead view, therefore no gravity vector
–physics.setDrawMode(“debug”);
_W = display.contentWidth;
_H = display.contentHeight;

local fishOriginal
– Background
local halfW = display.viewableContentWidth / 2
local halfH = display.viewableContentHeight / 2

– Create a table to store all the fish and register this table as the
– “enterFrame” listener to animate all the fish.
local bounceAnimation = {
container = display.newRect( 0, 0, display.viewableContentWidth, display.viewableContentHeight ),
reflectX = true,
}
display.setStatusBar( display.HiddenStatusBar )
local bg = display.newImageRect(“bkg.png”, _W, _H);
bg.x = _W * 0.5; bg.y = _H * 0.5;

function addWall( x, y, width, height )
local wall = display.newRect( 0, 0, width, height )
– give it a name so we don’t try to add joints to it
wall.name = “wall”
wall:setFillColor( 255, 255, 255 )
physics.addBody( wall, “static”, { friction=1, bounce=.1, density=1 } )
wall.x, wall.y = x, y
end
– create the walls round the edge of the screen
addWall( display.contentWidth/2, 0, display.contentWidth, 10 )
addWall( display.contentWidth, display.contentHeight/2, 10, display.contentHeight )
addWall( display.contentWidth/2, display.contentHeight, display.contentWidth, 10 )
addWall( 0, display.contentHeight/2, 10, display.contentHeight )
local ballBody = { density=0.8, friction=0.2, bounce=0.1, radius=10 }
local bottom = display.newImageRect(“bumper_end.png”,320,27 )
bottom.myName = “bottom”
bottom.x = 150
bottom.y = 450
physics.addBody( bottom, “static”, { friction=1, bounce=0, density=1 } )

local cueball = display.newImage( “fish.small.blue.png” )
cueball.x = display.contentWidth/2; cueball.y = 300
cueball.rotation = -90
physics.addBody( cueball, ballBody )
cueball.linearDamping = 0.3
cueball.angularDamping = 0.8
cueball.isBullet = true – force continuous collision detection, to stop really fast shots from passing through other balls
cueball.myName = “fish”
cueball.isFixedRotation = true

target = display.newImage( “target.png” )
target.x = cueball.x; target.y = cueball.y; target.alpha = 0
local function resetCueball()
cueball.alpha = 0
cueball.x = 160
cueball.y = 300
cueball.xScale = 2.0; cueball.yScale = 2.0
local dropBall = transition.to( cueball, { alpha=1.0, xScale=1.0, yScale=1.0, time=400 } )
end
– Fishies
local numFish = 4
local file1 = “fish.small.red.png”

local file2 = “fish.small.blue.png”


– Define touch listener for fish so that fish can behave like buttons.
– The listener will receive an ‘event’ argument containing a “target” property
– corresponding to the object that was the target of the interaction.
– This eliminates closure overhead (i.e. the need to reference non-local variables )
local buttonListener = function( event )
if “ended” == event.phase then
local group = event.target

– tap only triggers change from original to different color
local topObject = group[1]

if ( topObject.isVisible ) then
–local bottomObject = group[2]
topObject:removeSelf()
– Dissolve to bottomObject (different color)
transition.dissolve( topObject, bottomObject, 500 )

– Restore after some random delay
–transition.dissolve( bottomObject, topObject, 500, math.random( 3000, 10000 ) )
end

– we handled it so return true to stop propagation
return true
end
end

–[[
local function updateScores()
redScore.text = "RED - " … redTotal
yellowScore.text = "YELLOW - " … yellowTotal
end

– Handler for ball in pocket
local gameOver – forward declaration; function is below
local function inPocket( self, event )
event.other:setLinearVelocity( 0, 0 )
local fallDown = transition.to( event.other, { alpha=0, xScale=0.3, yScale=0.3, time=200 } )

if ( event.other.color == “white” ) then
timer.performWithDelay( 50, resetCueball )
elseif ( event.other.color == “red” ) then
redTotal = redTotal + 1
updateScores()
elseif ( event.other.color == “yellow” ) then
yellowTotal = yellowTotal + 1
updateScores()
elseif ( event.other.color == “black” ) then
gameOver()
end
end

– Create pockets
local pocket = {}
for i = 1, 3 do
for j = 1, 2 do
local index = j + ((i-1) * 2) – a counter from 1 to 6

– Add objects to use as collision sensors in the pockets
local sensorRadius = 20
pocket[index] = display.newCircle( -389 + (515*j), -436 + (474*i), sensorRadius )

– (Change this value to “true” to make the pocket sensors visible)
pocket[index].isVisible = false

physics.addBody( pocket[index], { radius=sensorRadius, isSensor=true } )
pocket[index].id = “pocket”
pocket[index].collision = inPocket
pocket[index]:addEventListener( “collision”, pocket[index] ) – add table listener to each pocket sensor

end
end
–]]
– Shoot the cue ball, using a visible force vector
local function cueShot( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Stop current cueball motion, if any
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0

target.x = t.x
target.y = t.y

startRotation = function()
target.rotation = target.rotation + 4
end

Runtime:addEventListener( “enterFrame”, startRotation )

local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil

elseif t.isFocus then
if “moved” == phase then

if ( myLine ) then
myLine.parent:remove( myLine ) – erase previous line, if any
end
myLine = display.newLine( t.x,t.y, event.x,event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

local stopRotation = function()
Runtime:removeEventListener( “enterFrame”, startRotation )
end

local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )

if ( myLine ) then
myLine.parent:remove( myLine )
end

– Strike the ball!
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )

end
end

– Stop further propagation of touch event
return true
end

– Add fish to the screen
for i=1,numFish do
– create group which will represent our fish, storing both images (file1 and file2)
local group = display.newGroup()

fishOriginal = display.newImageRect( file1,70,41 )
– fishOriginal.myName =“fish”
physics.addBody( fishOriginal,“kinematic”, { density=5.0, friction=0.3, bounce=0.4,radius =25} )
group:insert( fishOriginal, true ) – accessed in buttonListener as group[1]
local fishDifferent = display.newImage( file2,70,41 )
group:insert( fishDifferent, true ) – accessed in buttonListener as group[2]
fishDifferent.isVisible = false – make file2 invisible

– move to random position in a 200x200 region in the middle of the screen
group:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) )

– connect buttonListener. touching the fish will cause it to change to file2’s image
group:addEventListener( “collision”, cueball )

– assign each fish a random velocity
group.vx = math.random( 1, 5 )
group.vy = math.random( -2, 2 )

– add fish to animation group so that it will bounce
bounceAnimation[#bounceAnimation + 1] = group
end

– Function to animate all the fish
function bounceAnimation:enterFrame( event )
local container = self.container
container:setFillColor( 0, 0, 0, 0) – make invisible
local containerBounds = container.contentBounds
local xMin = containerBounds.xMin
local xMax = containerBounds.xMax
–local yMin = containerBounds.yMin
local yMin = 5
– local yMax = containerBounds.yMax
local yMax = 200
local orientation = self.currentOrientation
local isLandscape = “landscapeLeft” == orientation or “landscapeRight” == orientation

local reflectX = nil ~= self.reflectX
local reflectY = nil ~= self.reflectY

– the fish groups are stored in integer arrays, so iterate through all the
– integer arrays
for i,v in ipairs( self ) do
local object = v – the display object to animate, e.g. the fish group
local vx = object.vx
local vy = object.vy

if ( isLandscape ) then
if ( “landscapeLeft” == orientation ) then
local vxOld = vx
vx = -vy
vy = -vxOld
elseif ( “landscapeRight” == orientation ) then
local vxOld = vx
vx = vy
vy = vxOld
end
elseif ( “portraitUpsideDown” == orientation ) then
vx = -vx
vy = -vy
end

– TODO: for now, time is measured in frames instead of seconds…
local dx = vx
local dy = vy

local bounds = object.contentBounds

local flipX = false
local flipY = false

if (bounds.xMax + dx) > xMax then
flipX = true
dx = xMax - bounds.xMax
elseif (bounds.xMin + dx) < xMin then
flipX = true
dx = xMin - bounds.xMin
end

if (bounds.yMax + dy) > yMax then
flipY = true
dy = yMax - bounds.yMax
elseif (bounds.yMin + dy) < yMin then
flipY = true
dy = yMin - bounds.yMin
end

if ( isLandscape ) then flipX,flipY = flipY,flipX end
if ( flipX ) then
object.vx = -object.vx
if ( reflectX ) then object:scale( -1, 1 ) end
end
if ( flipY ) then
object.vy = -object.vy
if ( reflectY ) then object:scale( 1, -1 ) end
end

object:translate( dx, dy )
end
end

– Handle orientation of the fish
function bounceAnimation:orientation( event )
print( “bounceAnimation” )
for k,v in pairs( event ) do
print( " " … tostring( k ) … “(” … tostring( v ) … “)” )
end

if ( event.delta ~= 0 ) then
local rotateParameters = { rotation = -event.delta, time=500, delta=true }

Runtime:removeEventListener( “enterFrame”, self )
self.currentOrientation = event.type

for i,object in ipairs( self ) do
transition.to( object, rotateParameters )
end

local function resume(event)
Runtime:addEventListener( “enterFrame”, self )
end

timer.performWithDelay( 500, resume )
end
end

Runtime:addEventListener( “enterFrame”, bounceAnimation );
Runtime:addEventListener( “orientation”, bounceAnimation )

cueball:addEventListener( “touch”, cueShot )

would like to remove the fish when hit by the ball
[import]uid: 40990 topic_id: 22254 reply_id: 322254[/import]

You want to use collision detection for this. See Corona For Newbies Part 4 on Techority :slight_smile:

Also, I see your text is inside your code.

Start code with < lua > and end with < / lua >

No spaces.

:slight_smile: [import]uid: 52491 topic_id: 22254 reply_id: 88815[/import]

thanks peach for responding
I tried it, it only fires i time I want to be a hit a fish and remove it from the screen
It has something to what array and the way the fish animation coded. I don’t know how to access the fish through the group.
if you can suggest any other way of animating the fishery example so that I can use your collision will appreciate it

thanks [import]uid: 40990 topic_id: 22254 reply_id: 88902[/import]

You don’t appear to be spawning your fish in a table, which would help.

This is a little too long for me to read through but basically you need each fish to have a property, like “type”, eg;

fish.type = “fish”

Then on ball collision if event.other.type == “fish” then event.other:removeSelf()

Something like that :slight_smile: [import]uid: 52491 topic_id: 22254 reply_id: 88989[/import]

I understand what are saying spawning fish on a table. But I would like to you the animation in the fishery example. so you have any code where I can spawn objects in a table and handle collision accordingly
[import]uid: 40990 topic_id: 22254 reply_id: 89116[/import]

I am afraid I don’t know of any examples that handle all of that, no.

You would do as you are doing now, the only difference being that you’d add them to a table.

Work out your collision first is my advice :slight_smile: [import]uid: 52491 topic_id: 22254 reply_id: 89223[/import]