Physics problem

I have a few questions,

1, How do I set different physics properties to each params.type for the platforms?

2, Should I require the physics in the main.lua or in the game.lua, what is best?

At the moment, all platforms have the same physics properties, when I add a platform to the game.lua the player and platform doesn’t collide with each other but instead the player fall right through it.

I’m using the director class and the _W, _H variables are declared in the main.lua

My game module;
game.lua

module(..., package.seeall)  
  
local player = require("player")  
local platform = require("platform")  
  
function new()  
  
 local localGroup = display.newGroup()  
  
 local platform = platform.new({type = "startPlatform"});  
 platform.x = \_W/2;  
 platform.y = \_H/2;   
  
 -- When setting up the physics properties here the player collides with the platform.   
 --physics.addBody(platform, "static", startPlatform)  
  
 localGroup:insert(platform)  
  
 local player = player.new({type = "normal"})  
  
 localGroup:insert(player)  
  
 return localGroup  
end  
  

My player module;
player.lua

module(..., package.seeall)  
  
function new(params)  
  
 local localGroup = display.newGroup()  
  
 local img;  
  
 if(params) then  
  
 if(params.type == "normal") then  
 img = "images/dude.png";  
  
 end  
  
 end  
  
 local player = display.newImageRect(img, 35, 64)  
 player:setReferencePoint(display.CenterReferencePoint)  
 player.x = \_W/2   
 player.y = \_H/2;  
 physics.addBody(player, "dynamic", {density =1.0, friction = 0.1, bounce = 0.25 , radius = 32})  
 player.isFixedRotation = false;  
  
 localGroup:insert(player);  
  
 -- Set up the Accelerometer values in Portrait  
  
 local motionX = 0  
 local motionY = 0   
  
 local function onAccelerate( event )  
  
 motionX = 20 \* event.xGravity;  
 motionY = 20 \* event.yGravity;  
 end  
  
 Runtime:addEventListener ("accelerometer", onAccelerate);  
  
  
  
 local function movePlayer (event)  
 player.x = player.x + motionX;  
 player.y = player.y - motionY;  
 end  
  
 Runtime:addEventListener("enterFrame", movePlayer)  
  
 -- A screen wrapper.  
 local function screenWrapper (event)  
  
  
 if player.x \< 0 then   
 player.x = display.contentWidth  
 end  
  
  
 if player.x \> display.contentWidth then  
 player.x = 0  
 end  
  
  
 if player.y \< 0 then   
 player.y = display.contentHeight   
 end  
  
  
 if player.y \> display.contentHeight then   
 player.y = 0  
 end  
  
 end  
  
 Runtime:addEventListener("enterFrame", screenWrapper)  
  
  
  
 return localGroup  
end  

My Platform module;

platform.lua

  
module(..., package.seeall)  
function new(params)  
  
 local localGroup = display.newGroup()  
  
 local img;  
  
 startPlatform = { density=1.0, friction=0.1, bounce=0.5}  
 normalPlatform = { density=1.0, friction=0.6, bounce=0.5}  
 movingPlatform = { density=1.0, friction=1.0, bounce=1.5}  
  
 if(params) then  
  
 if(params.type == "startPlatform") then  
 img = "images/platform1.png";  
  
  
 elseif(params.type == "normalPlatform") then  
 img = "images/platform2.png";  
 elseif(params.type == "movingPlatform") then  
 img = "images/platform3.png";  
  
 end  
  
 end  
  
 local platform = display.newImageRect(img, 64, 8)  
 platform:setReferencePoint(display.CenterReferencePoint);  
 physics.addBody(platform, "kinematic", startPlatform)  
 platform.isFixedRotation = true  
 platform.isSensor = true;  
  
 localGroup:insert(platform);  
  
 return localGroup  
end  
  

[import]uid: 34126 topic_id: 10418 reply_id: 310418[/import]

I can help you with your ‘third’ question. The player is falling straight through because all your platforms and your player are in a different displayGroups. All the objects that need to interact with each other physically, need to be in the same group. Also, shouldn’t the kinematic in your platform.lua be static? [import]uid: 54716 topic_id: 10418 reply_id: 37927[/import]

I removed the groups within the modules and have just groups inside the game.lua now and it works now. I want the platform to be “kinematic” and set as isSensor because I want the player to be able to pass through from under the platforms but not from above.

I haven’t figured that out how to make just yet, I want to set individual physics properties first.

Here’s another couple of question;

1, I want to set up a moveCamera function that follows the player and scroll the gameGroup by the amount the player moves vertically, what is the best way of doing that?

2, Since the player will have power ups that boost the movement in the y-direction by various amount, how do I set that up so they affect/add to the moveCamera function?

3, I have two backgrounds I want to loop, now I have positioned them so bg2 is above bg1. How do I make them loop as the player moves?

  
-- The Jump/Boost variables.  
normal = player.height \*4;  
boost1 = normal + player.height \*4;  
boost2 = normal + player.height \*8;  
boost3 = normal + player.height \*12;  
boost4 = normal + player.height \*28;  
  
 local bg1 = backgrounds.new({type = "bg1"});  
 bg1.x = 0;  
 bg1.y = 0;   
 gameGroup:insert(bg1)  
  
 local bg2 = backgrounds.new({type = "bg2"});  
 bg2.x = 0;  
 bg2.y = -480;   
 gameGroup:insert(bg2)  
  
gameGroup.y = 0  
  
 local function moveCamera()  
 if (player.y \> 80 and player.y \< 80) then  
 gameGroup.y = -player.y - normal;  
 end  
 end  
  
  
 Runtime:addEventListener( "enterFrame", moveCamera )  

So I will make a canJump function where I set some sort of collision with the platform and if the player collide with a platform then I will applyLinearImpulse( 0, player.height*4, player.x, player.y )

function canJump(event)  
 if canJump then  
 player:applyLinearImpulse( 0, normal, player.x, player.y )  
 end  
end  
   
Runtime:addEventListener("enterFrame", canJump)  

I’m not sure yet if I should have all the player logic inside the player module or in the game module, is it best to have all collision inside the game module?

thanks. [import]uid: 34126 topic_id: 10418 reply_id: 37993[/import]

Here’s a test to make the player jump but it just make the player spasm/freak out. The player goes crazy and jumps 1000mph.

What is the best way to set up so the player jumps on collision with the platform?

[code]
local function onCollision(self, event)
– Player hit the platform

if self.name == “player” and event.other.name == “startPlatform” then

self:applyLinearImpulse(0, 100, player.x, player.y )

end
end
player.collision = onCollision
player:addEventListener(“collision”, player)
[/code] [import]uid: 34126 topic_id: 10418 reply_id: 38003[/import]

I would assume that you would compare y values to determine if the player passes through it or not. If player.y > platform.y then the player would be able to pass through, and then after the player clears the platform, you would turn that platform back into a solid. Just talking, lol.
Off the top of my head, I would probably cycle through all the platforms on the screen and if the player is lower than the platform(s)then I would set the platform(s) to isSensor and not when the player is higher. [import]uid: 54716 topic_id: 10418 reply_id: 38097[/import]

I’m still trying to figure out how to add the physics properties within the if(params) but I can’t get it to work, I just got a copy of Physics Editor to simplify and get the shapeDefs more precise.

Can someone help me how to set up the properties inside the if(params)?

Thanks.

[code]
function new(params)
– Get the physics data from the shapeDefs.lua.
local physicsData = (require “shapeDefs”).physicsData()

local img;

if(params) then

if(params.type == “startPlatform”) then
img = “images/platform1.png”;
–physics.addBody( platform, “kinematic”, physicsData:get(“startPlatform”) )

elseif(params.type == “normalPlatform”) then
img = “images/platform2.png”;
–physics.addBody( params.type, “kinematic”, physicsData:get(“normalPlatform”) )

elseif(params.type == “movingPlatform”) then
img = “images/platform3.png”;
–physics.addBody( img, “kinematic”, physicsData:get(“movingPlatform”) )
end

end

local platform = display.newImageRect(img, 64, 8)
platform:setReferencePoint(display.CenterReferencePoint);
physics.addBody( platform, “kinematic”, physicsData:get(“startPlatform”) )
platform.isFixedRotation = true
–platform.isSensor = true;

return platform;
end

[/code] [import]uid: 34126 topic_id: 10418 reply_id: 38175[/import]

I haven’t figured out how to set the physics properties individually yet so I moved along to the jumping part.

I have the collision inside my game.lua and after reading the docs Ansca suggests that it is best to keep the collision local. I have tried post, pre and on collision buy the only one that actually makes the player jump is “collision” but then it fires of endlessly and I just want the impulse to fire when platform and players feet hit the platform and then when the linearImpulse ended I want the gravity to do the rest and make it fall down again.

Also, I set the platform to be “kinematic” and marked it isSensor = true, because I want the player to be able to pass through it on the way up but not on the way down since I want the platform to be jumpable.

Can someone please show me how to do this?

[code]
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
player:applyLinearImpulse(0, normal, player.x, player.y )
print( self.name … ": collision began with " … event.other.name )

elseif ( event.phase == “ended” ) then
–player:applyLinearImpulse(0, normal, player.x, player.y )
self:removeEventListener( “collision”, self )

print( self.name … ": collision ended with " … event.other.name )

end
end

player.collision = onLocalCollision
player:addEventListener( “collision”, player )

platform.collision = onLocalCollision
platform:addEventListener( “collision”, platform )
[/code] [import]uid: 34126 topic_id: 10418 reply_id: 38318[/import]

I’ll keep all my questions in the same post so others can find them and use the code easily and it seems like there are others trying to make a Doodle Jump style game.

In doodle jump, when you tilt the device left and right the doodler image flip so it faces the direction the device is titled.

How do I make the image changes in corona? [import]uid: 34126 topic_id: 10418 reply_id: 38490[/import]