Physics and Groups.

Right, I’m currently moving a static platform down the screen using this code:

local tPrevious = system.getTimer()  
  
 local function move(event)  
  
 local tDelta = event.time - tPrevious  
 tPrevious = event.time  
  
 local yOffset = ( 0.01 \* tDelta )  
  
 stand.y = stand.y - yOffset  
  
 end  
  
 -- Gets the background moving  
 Runtime:addEventListener( "enterFrame", move )  
  

Now I’m experiencing bugs using this method, as any items dropped on this platform behave weirdly with each other. It’s like the physics assigned to them has gone. I presume this is because I am moving the stand directly using its y coords rather than the physics engine.

I have also tried welding items together once they collide, but this won’t work as the items are still being acted upon by the physics engine and the game will crash upon trying to create the joint.

I’ve tried a few other methods, such as inserting the stand and items dropped onto it into a group, and moving this down. Now this method works to a certain extent. But I have physics objects that can’t be in this group and they behave weirdly.

Now I’m thinking of two other possibilities, could I move the stand down the screen using the physics engine some how? I still need to maintain control over it, so setting it to to dynamic and letting it fall won’t work. I have also tried grouping all of the other physics objects into a group, then moving this group up the screen instead. Now this group does move up the screen without issues with the physics engine but I need to know two things: can I set the screen to always follow a group? and once I have inserted an item into a group, can I remove it from the group, without removing the item completely?

Thanks for reading, if you have any other ideas how I could accomplish this let me know.

Cheers. [import]uid: 68741 topic_id: 13863 reply_id: 313863[/import]

Post more code, I can’t get at what you are asking :slight_smile:

You sound like you are having the problem I had, put some generic shapes in and post some plug n play code we can test in the corona sim and let’s see what happens?

-ng [import]uid: 61600 topic_id: 13863 reply_id: 50951[/import]

Basically, that stand moves down by using the function above. Because the stand is a physics object and I’m moving it down using the y coord, not the physics engine, I get bugs such as:

other physics rectangles dropped onto it don’t retain there physics attributes, they will slide into one another.

So I need a method to move this stand down, using the physics engine really.

or if thats not possible an answer to these:

" I need to know two things: can I set the screen to always follow a group? and once I have inserted an item into a group, can I remove it from the group, without removing the item completely?

Thanks for reading, if you have any other ideas how I could accomplish this let me know." [import]uid: 68741 topic_id: 13863 reply_id: 50960[/import]

body:applyForce( )
body:applyLinearImpulse( )
will both allow you to move the platform and allow other objects to interact correctly with it.
Maybe add a timer to stop the platform after a pre-determined time/distance, or stop it when it reaches it’s stopping point. [import]uid: 49842 topic_id: 13863 reply_id: 50980[/import]

  
 local function move(event)  
  
 stand:setLinearVelocity( 0, standSpeed, stand.x, stand.y ) --set it between 5 and 20.   
  
 end  
  
 Runtime:addEventListener( "enterFrame", move )  
  
  

Just a few more questions please, the stand moves down the screen, but it sort of jumps down. It causes all of the objects on the stand to jump up every time it.

Is there anyway to make it move down the screen smoother?

also I would like standSpeed to increment overtime, so say my average game lasts 3 minutes, how can set this variable to change from 5-20 over 3 minutes?

Thanks.
[import]uid: 68741 topic_id: 13863 reply_id: 51115[/import]

I’m out and about but did you include the following?
The set positionIterations is defaulted to 8 (if you don’t put anything) but if you increase it to 16 (I use 32 and start cutting back)things will act differently.

Case in point, I had a rotating platform at a medium speed, and the platform would keep “pushing” through a falling ball that would hit it. I changed the iterations to 32, and BAM the physics started acting right.

For your issue, I know what you are saying now. I have problems when I have an elevator type of platform that goes up and down and when I use tilt controls based on gravity (left to right only) things don’t act right when the platform is coming up.

local physics = require(“physics”)
physics.start(true)
physics.setScale( 60 )
physics.setGravity( 0, 10)
physics.setPositionIterations(8)
I’m going to now just start answering random questions you had that I know (im really new, never programmed as of 90 days as of today hehe, but I’ve learned a lot and made a ton of stupid mistakes).
"can I set the screen to always follow a group?"

Yes you can: (Look at Camera Follow Circle at the bottom of the lua code, you can adapt it however you like)

Here is an example I created for something else, has a camera listener and other stuff. Not sure if the best way to go about it, but for me it’s working :slight_smile:
Ok, Here it is. It’s kind of scattered, but I cleaned it up (it was over 1100 lines, I removed my “secret sauce” and pasted everything in that seemed relevant. I put some in groups, one object I didn’t (eplat8) and it’s commented pretty heavily.

I also included some fun animation things too.

Have a go with it! :slight_smile: (Again, it’s not as clean as I would like, but It’s still fun. I’m working on being tidy )

Ok back to work :expressionless:

Btw, it’s all plug n play using system generated shapes (I like to do this so people don’t go HEY WHERE THE ##$%@#$@#$ ARE THE IMAGES GRRRR!). Ok here we go:

display.setStatusBar( display.HiddenStatusBar )  
  
--Nicholas Golden's crazy animation weird pack thingy  
  
--This is a bunch of stuff I have learned, decided to put into a big honking demo thing.  
--Many contributors helped me with it from corona.  
local physics = require("physics")  
physics.start()  
  
-- Scale set to 60, also added "true" on all display objects to prevent sleepiness  
physics.setScale( 60 )  
  
--Set only Y gravity and 0 to X  
  
physics.setGravity( 0, 9.8 )  
  
physics.setPositionIterations(8)  
  
-- initial gravity points downwards  
  
--Load sound track   
  
local soundtrack = audio.loadStream("media/Place your sound here")  
  
---If you want to loop, you use -1 otherwise you can specify a value  
  
audio.play(soundtrack, {loops=-1})  
-- Create master display group (for global "camera" scrolling effect)  
  
local game = display.newGroup();  
game.x = 0  
game.y = 0  
--Don't forget to insert everything into a group that you want to scroll!  
--Load images   
--In order 1st one listed goes in back, 2nd in front of previous, etc  
--This is to keep everything "layered" Lua reads top down, so yea.  
  
local back = display.newRect(0, 0, 2912, display.contentHeight)  
back:setFillColor(165, 210, 255)  
game:insert (back)  
  
local ball = display.newCircle( 0,0,40,40, true)  
ball.x =630; ball.y = 20  
physics.addBody( ball, { density=1.0, friction=10, bounce=0.2, radius=40 } )  
ball.isBullet = true  
game:insert( ball )  
  
--+++++++++++++++++++++++++++++++Level Borders++++++++++++++++++++++++++++++  
--++++++++++++++++++++++++++++++++BEGIN++++++++++++++++++++++++++++++++++  
  
----464 w 2912 L - level size  
--images (X, Y = placement X, Y = size)  
--left border  
local lside = display.newRect( 0, 0, 10, 640 )  
lside:setFillColor(255,255,255)  
physics.addBody( lside,"static", { friction=3, bounce=0.3, isGround = true } )  
game:insert (lside)  
lside.rotation = 180  
  
--right border  
local rside = display.newRect( 2912,0,10, 640 )  
rside:setFillColor(255,255,255)  
physics.addBody( rside,"static", { friction=3, bounce=0.3, isGround = true } )  
game:insert (rside)  
rside.rotation = 0  
  
--top border  
local tside = display.newRect( 0, 0, 2912, 10 )  
tside:setFillColor(255,255,255)  
physics.addBody( tside,"static", { friction=3, bounce=0.3, isGround = true } )  
game:insert (tside)  
tside.rotation = 0  
  
--bottom border  
local bside = display.newRect( 0, 630, 2912, 20 )  
bside:setFillColor(255,255,255)  
physics.addBody( bside,"static", { friction=3, bounce=0.3, isGround = true } )  
game:insert (bside)  
bside.rotation = 0  
--+++++++++++++++++++++++++++++++Level Borders++++++++++++++++++++++++++++++  
--++++++++++++++++++++++++++++++++++END+++++++++++++++++++++++++++++++++  
  
--moving objects  
  
local mplat1 = display.newRect( 600, 480, 300, 10 )  
mplat1:setFillColor(255,0,255)  
physics.addBody( mplat1,"static", { friction=3, bounce=0.3, isGround = true } )  
mplat1.rotation = 0  
game:insert (mplat1)  
  
local rotateAmount = 0  
local time = system.getTimer()  
local function animate( event )  
mplat1.rotation = mplat1.rotation + -5  
 local now = system.getTimer()  
 local elapsed = now - time  
 local rotationAngle = rotateAmount \* (elapsed / 1000.0) \* 10  
 time = now  
 mplat1.rotation = mplat1.rotation + rotationAngle  
end  
  
Runtime:addEventListener( "enterFrame", animate )  
--elevator up and down  
eplat1 = display.newRect( 0, 430, 80, 50)  
eplat1:setFillColor(25,200,35)  
eplat1.x = 500  
game:insert (eplat1)  
physics.addBody( eplat1,"static", { friction=3, bounce=0.3, isGround = true } )  
  
function goUp1 ()  
transition.to(eplat1, { time=1500, y=(eplat1.y - 150), onComplete=goDown1} )  
end   
  
 -- changing time will dictate how fast the object goes right on return  
function goDown1()  
transition.to(eplat1, { time=300, y=(eplat1.y + 150), onComplete=goUp1} )  
end  
 goUp1()  
   
  
 eplat3 = display.newRect( 0, 580, 80, 50)  
 eplat3:setFillColor(180,100,135)  
eplat3.x = 1900  
game:insert (eplat3)  
physics.addBody( eplat3,"static", { friction=3, bounce=0.3, isGround = true } )  
function goUp3 ()  
transition.to(eplat3, { time=1500, y=(eplat3.y - 150), onComplete=goDown3} )  
end   
 -- changing time will dictate how fast the object goes right on return  
function goDown3()  
transition.to(eplat3, { time=300, y=(eplat3.y + 150), onComplete=goUp3} )  
end  
 goUp3()  
   
--STATIC OBJECTS  
  
local rect6 = display.newRect( 0, 200, 200, 40 )  
rect6:setFillColor(200,10,90)  
physics.addBody( rect6,"static", { friction=3, bounce=0.3, isGround = true } )  
rect6.rotation = 45  
game:insert (rect6)  
--Added a bunch of non physics properties for you to track notts\_forest\_  
  
--SPaced them out to see the scrolling effect.  
  
 eplat4 = display.newRect( 500, 80, 80, 50)  
 eplat4:setFillColor(80,100,135)  
 game:insert (eplat4)  
  
 eplat5 = display.newRect( 800, 80, 80, 50)  
 eplat5:setFillColor(80,100,135)  
 game:insert (eplat5)  
  
 eplat6 = display.newRect( 1100, 80, 80, 50)  
 eplat6:setFillColor(80,100,135)  
 game:insert (eplat6)  
   
 eplat7 = display.newRect( 1500, 80, 80, 50)  
 eplat7:setFillColor(80,100,135)  
 game:insert (eplat7)  
  
 eplat8 = display.newRect( 300, 80, 80, 50)  
 eplat8:setFillColor(255,0,135)  
--NOT in group "game" see how it DOESN'T move?  
--I use this for "Score Board" etc  
   
--+++++++++++BEGING TILT CONTROL Code   
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--ONLY allows left and right control of ALL objects in a level. If you have a world which is   
--platforms and ramps etc, and the only thing that is movable is your object this will work.   
--otherwise if you have things hanging from chains, ropes or things that react to physics this will affect those items  
--If you want to SPECIFICALLY control the object ONLY, you'll need another control method and put listeners on the object etc  
  
system.setAccelerometerInterval( 100 ) -- set accelerometer to maximum responsiveness  
  
function onTilt( event )  
  
 physics.setGravity( ( 9.8 \* event.yGravity ), ( 9.8 ) )  
  
end  
Runtime:addEventListener( "accelerometer", onTilt )  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--+++++++++++++++++++++END TILT CONTROL Code +++++++++++++++++++++++++++  
---+++CAMERA FOLLLOW CIRCLE+++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
local function moveCamera()  
--orig line if (ball.x \> 200 and ball.x \< 2040) then   
 if (ball.x \> 480 and ball.x \< 2912) then  
 game.x= -ball.x + 480  
  
 --ball.x \> K and game.x= -ball.x + K  
 --K values should match, otherwise the screen will "snap"  
 --game.x= -ball.x 200 says where it shows up on screen (position)  
  
 end  
end  
  
local function moveCamera1()  
if (ball.y \> 0 and ball.y \< 0) then  
 game.y= -ball.y + 0   
--game.y = -ball.y 200 says where it shows up on screen (position)  
 end  
 end  
  
Runtime:addEventListener( "enterFrame", moveCamera )  
--TEST remove if necessary  
Runtime:addEventListener( "enterFrame", moveCamera1 )  
  
---END ++++ CAMERA FOLLOW BALL+++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
  

And build settings
(create build.settings file)

settings = {   
   
 orientation =  
 {  
 default = "landscapeLeft",  
 supported =  
 {  
  
 },  
 },  
}  

And config.lua

[code]
application =
{
content =
{
width=640,
height=960,
fps = 30,
scale = “letterbox”
},
}

Ok have fun lol.
[/code] [import]uid: 61600 topic_id: 13863 reply_id: 51127[/import]