How to animate (moving by default) and inanimate (make stationary on tap) colliding objects - iphone

Hey Guys I am making a game using corona sdk and so needed help with the lus code. In this program there are bubbles floating across the screen and colliding with each either as well as with the walls of the screen.
I am using 'Collision Filter' for the collisions and the masking operation and it is working well. But in this game I want a bubble to continuously move unless and until it is tapped upon. I thought of using the frame animation to animate each bubble and then add a separate function that will make it stationary when tapped.
But the problem is that at a time only 1 program seems to wrok fine. So,
1) either the bubbles collide, fall down, bounce against wall and eventually rest down.
2) The bubbles continuously keep moving across the screen, without colliding against each other, and instead pass through the other bubbles
What should I do to animate and inanimate(on tapping that bubble) a colliding bubble.
My code is below,
borderCollisionFilter = { categoryBits = 1, maskBits = 2 } -- collides with (4 & 2) only
local borderBodyElement = { bounce=1.0, filter=borderCollisionFilter }
local borderTop = display.newRect( 0, 0, 480, 1 )
borderTop:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderTop, "static", borderBodyElement )
local borderBottom = display.newRect( 0, 318, 480, 1 )
borderBottom:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderBottom, "static", borderBodyElement )
local borderLeft = display.newRect( 0, 0, 1, 320 )
borderLeft:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderLeft, "static", borderBodyElement )
local borderRight = display.newRect( 480, 1, 1, 320 )
borderRight:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderRight, "static", borderBodyElement )
--BUBBLES
local bubbleCollisionFilter = { categoryBits = 2, maskBits = 7 }
bubble = {bounce=0.94, radius=18,filter = bubbleCollisionFilter }
local bubble1 = display.newImage( "bubble.png", 50, 50 )
physics.addBody( bubble1, bubble )
local bubble2 = display.newImage( "bubble.png", 100, 230 )
physics.addBody( bubble2, bubble )
local bubble3 = display.newImage( "bubble.png", 180, 200 )
physics.addBody( bubble3, bubble )
local bubble4 = display.newImage( "bubble.png", 90, 30 )
physics.addBody( bubble4, bubble )
--MINIONS
minionCollisionFilter = { categoryBits = 4, maskBits = 2 }
minionBodyElement = { bounce=0.8, filter=minionCollisionFilter }
local c1 = display.newImage("str-minion-small.png")
c1.isVisible=false
physics.addBody( c1, "static", minionBodyElement )
local c2 = display.newImage("str-minion-mid.png")
c2.isVisible=false
physics.addBody( c2, "static", minionBodyElement )
local c3 = display.newImage("str-minion-big.png")
c3.isVisible=false
physics.addBody( c3, "static", minionBodyElement )
--SPAWNING
local function spawnDisk( event )
local phase = event.phase
local volumeBar = display.newLine( 0, 0, 1, 0 )
volumeBar.y = 400
volumeBar.x = 20
local v = 20*math.log(r:getTunerVolume())
local MINTHRESH = 30
local LEFTMARGIN = 20
local v2 = MINTHRESH + math.max (v, -MINTHRESH)
v2 = (display.contentWidth - 1 * LEFTMARGIN ) * v2 / MINTHRESH
volumeBar.xScale = math.max ( 20, v2 )
local l = volumeBar.xScale
local cnt1 = 0
local cnt2 = 0
local cnt3 = 0
local ONE =1
local val = event.numTaps
if "ended" == phase then
if l > 50 and l <=150 then
c1.x=math.random( 10, 450 )
c1.y=math.random( 10, 300 )
physics.addBody( c1, { density=1, radius=10.0 } )
c1.isVisible=true
cnt1= cnt1+ ONE
return c1
elseif l > 100 and l <=250 then
c2.x=math.random( 10, 450 )
c2.y=math.random( 10, 300 )
physics.addBody( c2, { density=2, radius=30.0 } )
c2.isVisible=true
cnt2= cnt2+ ONE
return c2
elseif l >=250 then
c3.x=math.random( 40, 450 )
c3.y=math.random( 40, 300 )
physics.addBody( c3, { density=2, radius=50.0 , bounce=0.0 } )
c3.isVisible=true
cnt3= cnt3+ ONE
return c3
end
end
end
buzzR:addEventListener( "touch", spawnDisk ) --
touch the Button to create minions

Listen for tap events and set objects to static:
http://developer.anscamobile.com/reference/index/bodybodytype
ADDITION: I hadn't bothered to run your code because you claimed the collision masking worked. Now that I actually have tried to run it, I got an error immediately.
First off, you need to require "physics" at the top of your code:
local physics = require("physics")
Then there is a timeout error just a few lines down because you didn't start the physics simulation. The second line of your code should be:
physics.start()
Now I'm going to assume those two lines are actually at the top of your code but you simply didn't paste them here, because I can't imagine you would write a hundred lines of code without ever running it.
However that still leaves more errors. Like, at the bottom of your code it references buzzR but there's no object buzzR defined anywhere.
Please either post code that works or say that you don't have code that works. Sorting through this mess is frustrating.

I have done one application to help with collision masking in Corona SDK, for free of course.
http://developer.anscamobile.com/forum/2011/09/12/coolmasking-take-total-control-over-collision-masking

Related

Im trying to make an animation trigger on roblox studio, can someone tell me how?

So when I try touch the trigger, the thing I'm trying to animate doesn't so the animation, I tried the animation id, anything else, can someone send me a model that has this, it will be nice if you can.
I made this with the gui elements already done u fill in what u need.
local library = {}
local function onClicked(frame, gui)
gui:SetVisible(false)
gui:ClearAllChildren()
end
function library:Create(parent)
local frame = Instance.new("Frame")
frame.Size = UDim2.new(1, 0, 1, 0)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BorderSizePixel = 0
local gui = Instance.new("TextLabel")
gui.Size = UDim2.new(0, 200, 0, 50)
gui.BackgroundColor3 = Color3.new(0.5, 0.5, 0.5)
gui.Position = UDim2.new(0.5, -100, 0.5, -25)
gui.Text = "Click the Button"
gui.TextColor3 = Color3.new(1, 1, 1)
gui.TextXAlignment = Enum.TextXAlignment.Center
gui.TextYAlignment = Enum.TextYAlignment.Center
gui.Font = Enum.Font.SourceSans
gui.TextSize = 24
gui.Parent = frame
local button = Instance.new("TextButton")
button.Size = UDim2.new(0, 50, 0, 25)
button.BackgroundColor3 = Color3.new(1, 0, 0)
button.Position = UDim2.new(0.5, -25, 0.85, 0)
button.Text = "X"
button.TextColor3 = Color3.new(1, 1, 1)
button.Parent = frame
button.MouseButton1Click:Connect(function()
onClicked(frame, gui)
end)
frame.Parent = parent
return frame
end

Part Size seems to be ignored

I have a part, created with
local p = Instance.new("Part")
p.Size = Vector3.new(2, 2, 2)
That part uses a mesh like
local m = Instance.new("SpecialMesh", p)
m.MeshType = Enum.MeshType.FileMesh
m.MeshId = "rbxassetid://7974596857"
which is a cube with rounded corners that I created in blender
When I put those beside each other, it seems like the Size property actually is ignored.
Why?
size 2
p1.Position = Vector3.new(0, 0, 0)
p1.Size = Vector3.new(2, 2, 2)
p2.Position = Vector3.new(5, 5, 0)
p2.Size = Vector3.new(2, 2, 2)
size 5
p1.Position = Vector3.new(0, 0, 0)
p1.Size = Vector3.new(5, 5, 5)
p2.Position = Vector3.new(5, 5, 0)
p2.Size = Vector3.new(5, 5, 5)
That's because special meshes have their own scaling property. If possible, use a MeshPart instead.

roblox CFrame not working properly

So here is the code:
local prt = Instance.new('Part',game.Workspace)
prt.Color = Color3.new(248, 248, 248)
prt.Size = Vector3.new(0.167, 5, 5)
prt.Rotation = Vector3.new(0, 0, math.rad(90))
prt.Anchored = true
prt.CFrame = CFrame.new(Vector3.new(19.1, 618.59, 116.85))
prt.Shape = "Cylinder"
what i want it to do is create a new part that is shaped like a cylinder, i want it to be a flat cylinder but it does the exact oppisite, it is standing up, i have tried many things.
The CFrame is setting the Rotation to 0,0,0. This is because you're doing CFrame.new() before prt.Rotation. If you did CFrame.new(Vector3.new(19.1,618.59,116.85)) * CFrame.Angles(0, 0, math.rad(90)), this would work and you can remove the "prt.Rotation" part.

Failing to draw on a Gtk.DrawingArea

I'm not able to draw, i've already read tutorials, i can't find the problem.
I simply have a correct UI drawed by Glade. Then i want to draw, for example 50 drawing areas. So i create a Grid with 50 cells; for each cell there is a vertical box (with a drawing area and a label inside each one). But i can't seen anything drawed.
class collega_GUI:
def __init__(self):
try:
self.__builder = Gtk.Builder()
self.__builder.add_from_file('UI2.glade')
self.__Grid = Gtk.Grid()
self.__Grid.set_margin_left(20)
self.__Grid.set_margin_right(20)
self.__Grid.set_row_spacing(10)
self.__Grid.set_column_spacing(15)
self.__Grid.set_column_homogeneous(True)
self.__GridBox = self.__builder.get_object('box11')
self.__GridBox.pack_end(self.__Grid, 1, 1, 20)
indirizzi_ip = []
for i in range(50):
indirizzi_ip.append(str(i))
cpu_info = {}
for ip in indirizzi_ip:
cpu_info[ip] = dict()
left = 0
right = 0
for ip in indirizzi_ip:
cpu_info[ip]['drawing_area'] = Gtk.DrawingArea()
cpu_info[ip]['drawing_area'].set_size_request(100, 100)
cpu_info[ip]['drawing_area'].set_name(ip)
box = Gtk.VBox(False, 5)
box.add(cpu_info[ip]['drawing_area'])
label = Gtk.Label(ip)
box.add(label)
self.__Grid.attach(box, left, right, 1, 1) #object,left,right,top,bottom
cpu_info[ip]['drawing_area'].connect("draw", self.__draw)
label.show()
cpu_info[ip]['drawing_area'].show() #the draw should start now!
box.show()
# 5 drawing areas in a row
left += 1
if left == 5:
right += 1
left = 0
self.__builder.get_object('Azioni_Window').show()
Gtk.main()
except Exception as xe:
logging.error('%s' % str(xe))
sys.exit()
def __draw(self, widget, context):
context.set_source_rgb(0.9, 0, 0.1) #rosso
context.set_source_rgb(0.1, 0.9, 0) #verde
context.set_source_rgb(0.8, 0.7, 0) #giallo
context.set_source_rgb(0.8, 0.7, 0.8) #inattivo
context.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height())
context.fill()
if __name__=='__main__':
try:
UINX=collega_GUI()
except Exception:
sys.exit()
You're missing
self.__Grid.show()
And hence nothing in the grid is shown.
In general it's easier to just call show_all() on some top-level container rather than trying to remember to show() every individual widget.

How to animate (moving by default) and inanimate (make stationary on tap) colliding objects

Hey tired this new code to animate objects (here bubbles) and make them stationary using sprites. The code is as below,
local ui = require("ui")
local gameUI = require("gameUI")
local easingx = require("easingx")
require "sprite"
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setScale( 60 )
local backgroundPortrait = display.newImage( "sky.png", 0, 0 )
local backgroundLandscape = display.newImage( "sky.png", 80, 80 )
backgroundLandscape.isVisible = false
local disp = backgroundLandscape
local function selectBubble( event )
local tapped = event.target --event.target is how Corona points to the tapped bubble
if ( tapped.bubbleSelected == false ) then
local vx,vy = tapped:getLinearVelocity()
tapped.xVel = vx --stores the current velocity into bubble's "xVel" variable
tapped.yVel = vy --likewise for yVel
tapped:setLinearVelocity( 0,0 ) --set bubble's velocity to 0!
tapped.currentFrame = (3)
tapped.bubbleSelected = true
elseif ( tapped.bubbleSelected == true ) then
tapped:setLinearVelocity( tapped.xVel, tapped.yVel ) --read previous velocity and set
tapped.bubbleSelected = false
end
end
--BUBBLE1
local bubble1 = sprite.newSprite( spriteSet1 )
bubble1.x = 100
bubble1.y = 100
physics.addBody(bubble1, {bounce=0.04, filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 2, 4 )
bubble1:addEventListener( "tap", selectBubble )
bubble1.bubbleSelected = false
bubble1:prepare("bubble")
bubble1:play()
--BUBBLE2
local bubble2 = sprite.newSprite( spriteSet1 )
bubble2.x = 210
bubble2.y = 20
physics.addBody(bubble2, {bounce=0.05, filter = bubbleCollisionFilter})
bubble2:setLinearVelocity( 2, 4 )
bubble2:prepare("bubble")
bubble2:play()
--BUBBLE3
local bubble3 = sprite.newSprite( spriteSet1 )
bubble3.x = 100
bubble3.y = 17
physics.addBody(bubble3, {bounce=0.02, filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 1, 2 )
bubble3:prepare("bubble")
bubble3:play()
--BUBBLE4
local bubble4 = sprite.newSprite( spriteSet1 )
bubble4.x = 310
bubble4.y = 20
physics.addBody(bubble4, {bounce=0.4, filter = bubbleCollisionFilter})
bubble4:setLinearVelocity( 2, 4 )
bubble4:prepare("bubble")
bubble4:play()
The problem is firstly the code doesn't seem to work. Secondly though the bubble changes color on tap (this color is same for most). Yet, each bubble has a unique letter on it. How to get this to work. Please help.
It's hard to know what exactly you're asking because your question is pretty vague (what does "animate" mean? And what do you mean by "doesn't seem to work"? explain these terms because they can mean multiple things) but I think you want to set the physics bodies to "static" in the tap event listener. Refer here for what I'm talking about:
http://developer.anscamobile.com/reference/index/bodybodytype
So inside the selectBubble() function you would type something like tapped.bodyType="static"