Functions, Transitions and Physics.

Hi all.

I have a problem I’m struggling to wrap my head around. Let me briefly set the picture first.

In my program, I use a function to add a load of objects to a table, along with a variable for their movementspeed.

I then have another function, called by a timer, which randomly generates an object from the table, displays it on the top of the screen and sets up a transition to move it off the bottom of the screen according to it’s movementspeed.

What I’m after is to set it up so that if my player object collides with an object, it changes the movementspeed variable and the randomly generated objects slow down.

I guess my question is: Once the function is called by the timer, and the object is moving down screen, can I amend the speed at which it moves? If so, what is the best method for doing so, and if not, has anyone found a work around to this issue before?

Thanks

Dan [import]uid: 67933 topic_id: 12590 reply_id: 312590[/import]

I’m not 100% sure what you need, but I think what you want is

body:setLinearVelocity( xVelocity, yVelocity

and

body:getLinearVelocity

This would only before display items that have physics bodies associate.

You could also control the body manually by changing the body type to kinematic and then moving the body your self with each frame. kinematic bodies still recieve collision events. [import]uid: 29520 topic_id: 12590 reply_id: 46053[/import]

hmmm…

Yeah I think this is quite different from how I usually set something like this up.

Usually I would add all of these items to a display group, with the physics attached to them. The terrain/craters would be static objects (they would not move within the physics environment). The car could be kinematic or whatever and you only move the car and any other dynamic objects.

You can then also move the WHOLE display group (the entire scene) to keep the car in a certain spot on the screen if you want.

Basically think of it as a panning camera moving with the car along the static terrain. When static items fall off the screen, you can remove them to save memory and performance. [import]uid: 29520 topic_id: 12590 reply_id: 46060[/import]

Ok, So I’ve been trying to test your suggestion but have hit a bit of a brick wall.

The craters spawn and are added to a group, then the group is moved on the y axis by a runtime event listener.

The problem now is that the y position the craters spawn at also moves, like it’s part of the group. Is there any way to stop this?

Here’s the complete code:

--hides iphone status bar  
--display.setStatusBar(display.HiddenStatusBar)  
  
physics = require("physics")  
physics.start()  
physics.setGravity(0,0)  
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.  
  
--cache content dimensions and math.random for better performance  
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
terrainspeed = 5  
maxCrater = 5  
crad30 = 30  
crad60 = 60  
crad100 = 100  
local centerX = display.contentWidth/2;  
  
local craterTable = {} -- create table to store craters  
local cgroup02 = display.newGroup() -- crater layer  
function initCrater() --adds crater images to craterTable  
 local crater301 = {}  
 crater301.imgpath = "crater301.png"; --Set Image Path for crater  
 crater301.movementSpeed = 10000; --Determines the movement speed of   
 crater301.rad = 30  
 table.insert(craterTable, crater301); --Insert crater into craterTable  
  
 local crater601 = {}  
 crater601.imgpath = "crater601.png"; --Set Image Path for crater  
 crater601.movementSpeed = 10000; --Determines the movement speed of crater  
 crater601.rad = 60  
 table.insert(craterTable, crater601); --Insert crater into craterTable  
  
 local crater1001 = {}  
 crater1001.imgpath = "crater1001.png"; --Set Image Path for crater  
 crater1001.movementSpeed = 10000; --Determines the movement speed of crater  
 crater1001.rad = 100  
 table.insert(craterTable, crater1001); --Insert crater into craterTable  
  
 end --END initStar()   
  
function getRandomCrater() -- generates craters randomly from a list  
 local temp = craterTable[mRand(1, #craterTable)] -- Get a random crater from craterTable  
 local randomCrater = display.newImage(temp.imgpath) -- Set image path for object  
 randomCrater.myName = "crater" -- Set the name of the object to crater  
 physics.addBody( randomCrater, { friction=0, bounce=0, radius = temp.rad / 2 } )  
 randomCrater.isSensor = true  
 cgroup02:insert(randomCrater) -- add randomCrater to cgroup02, (crater group/layer 2)  
 randomCrater.movementSpeed = temp.movementSpeed; -- Set how fast the object will move  
 randomCrater.x = mRand(0,\_W) -- Set starting point of crater at a random X position  
 randomCrater.y = \_H / \_H - randomCrater.contentWidth -- Start the crater off screen  
end--END getRandomcrater()  
  
function movecraters()  
 cgroup02.y = cgroup02.y + terrainspeed  
end  
  
initCrater()  
  
Timer1 = timer.performWithDelay(750,getRandomCrater, -1)  
  
Runtime:addEventListener("enterFrame", movecraters)  

Thanks for your help.

Dan [import]uid: 67933 topic_id: 12590 reply_id: 46153[/import]

thanks for the reply.

To explain more fully, I have a little car which is kinematic, and a series of craters which are randomly generated and also kinematic. At the moment I have the motion of the craters controlled by a transition in the “getrandomcrater” function. that function is controlled by a timer which spawns a crater every x milliseconds.

What I want is to control the speed of the random objects by changing a variable called “terrainspeed” when the car collides with a crater, so that everything slows down. When the collision ends, terrainspeed returns to normal and moves at the initial rate again.

Would moving the craters using the physics velocity snippets above still allow me to use transitions or would I completely need to re-code the movement?

My code currently looks like this:

 function getRandomCrater() -- generates craters randomly from a list  
 local temp = craterTable[mRand(1, #craterTable)] -- Get a random crater from craterTable  
 local randomCrater = display.newImage(temp.imgpath) -- Set image path for object  
 randomCrater.myName = "crater" -- Set the name of the object to crater  
 physics.addBody( randomCrater, { friction=0, bounce=0, radius = temp.rad / 2 } )  
 randomCrater.isSensor = true  
 cgroup02:insert(randomCrater) -- add randomCrater to cgroup02, (crater group/layer 2)  
 randomCrater.movementSpeed = temp.movementSpeed; -- Set how fast the object will move  
 randomCrater.x = mRand(0,\_W) -- Set starting point of crater at a random X position  
 randomCrater.y = \_H / \_H - 100 -- Start the crater off screen  
 --randomCrater.rotation = mRand(0,360) -- Rotate the object  
 CraterMove = transition.to(randomCrater, {  
 time=randomCrater.movementSpeed,   
 y= \_H + 100,  
 onComplete = function(self) self.parent:remove(self); cgroup02:remove(randomCrater); self = nil; end  
 }) -- Move the Crate  

Thanks again

[import]uid: 67933 topic_id: 12590 reply_id: 46058[/import]