Any platform gurus around?

Hi again, I am learning quick but sometimes I stumble into game logic problems :slight_smile:

I am building a platform game with moving platforms. Yes, there are several of discussion about this issue here - but none of them seems to get me there.

So, the solution now looks like this.

  1. Physics
  2. One moving platform of type kinetic.
  3. At a collision with the platform and player i run the following code.
transition.to(player, {x = platform.x, time=0})  

So, far everything works as it should. My object with physics attachs to the platform, but the game character is always attach to the middle of the platform - whatever he lands. Ok, thats exactly what the code tells him to do, but how can I make him attach where he lands?

Joakim [import]uid: 81188 topic_id: 19557 reply_id: 319557[/import]

Here is some very basic code (main.lua) for letting a player jump through a platform and land on it.
The process is based on turning the platform into a sensor during the time the player is on its way through the platform, and then turn the platform solid again, letting the physics engine take care of the rest.

You make the player jump by pulling down and letting go.

Again this is very basic code, but I hope it will be of some help.

[lua]-- main.lua
– Setup the game
– Turn off the status bar
display.setStatusBar(display.HiddenStatusBar);

– Setup graphics
local physics = require(“physics”);
physics.start();
physics.setDrawMode(“hybrid”);
physics.setGravity(0, 9.8);
physics.setPositionIterations(16);
physics.setVelocityIterations(6);

– Setup the background group
local gameGroup = display.newGroup();
gameGroup.x = 0;

– Ground object
local ground = display.newRect(0, 0, 320, 5);
ground.x = 160;
ground.y = 470;
ground.objectType = “ground”
groundPhysics = { -160, 0, 160, 0, 160, 10, -160, 10 };
physics.addBody(ground, “static”, { density=1.0, friction=0.0, bounce=0.0, shape=groundPhysics });
gameGroup:insert(ground);

– Setup a sprite for the player
playerInstance = display.newRect(0, 0, 32, 32);
playerInstance.x = 100;
playerInstance.y = 400;
playerInstance.rotation = 0;
gameGroup:insert(playerInstance);
physics.addBody(playerInstance, “dynamic”, { density=0.25, friction=0.15, bounce=0.0 });
playerInstance.isFixedRotation = true;
playerInstance.objectType = “player”;

– Setup some vars on the playerInstance
playerInstance.isFalling = false;
playerInstance.xVel = 0;
playerInstance.yVel = 0;

– Test jump platform
local jumpPlatform = display.newRect(80, 200, 100, 50);
jumpPlatform.objectType = “jumpPlatform”
gameGroup:insert(jumpPlatform);
physics.addBody(jumpPlatform, “static”, { friction=0.3} );

– The main loop
function onEnterFrame(event)

– Move the game world to keep the player onscreen
if playerInstance.y <= 200 then
gameGroup.y = -playerInstance.y + 200;
end
end

– When the player makes a tap event
function onTouch(event)

local t = event.target;
local phase = event.phase;

if phase == “began” then
– Make the object the topmost object
local parent = t.parent;
parent:insert(t);
display:getCurrentStage():setFocus(t);

– Prevents spurious messages being sent
t.isFocus = true;

– Store the initial position
t.x0 = event.x - t.x;
t.y0 = event.y - t.y;

elseif t.isFocus == true then

if phase == “moved” then

elseif phase == “ended” or phase == “cancelled” then

local yForce = (event.y - playerInstance.y) * -0.1;

playerInstance:applyLinearImpulse( 0, yForce, playerInstance.x, playerInstance.y);

display.getCurrentStage():setFocus(nil);
t.isFocus = false;

end

end

end

local function onPreCollision(self, event)
local player = self;
local collidedWith = event.other;

if (collidedWith.objectType == “ground”) then
return true; – we don’t care about the ground
end

–check to see if player is above the platform
if (player.y < collidedWith.y) then
return true; – player should remain on platform
end

print(“preCollision”);

–set player to sensor while travelling through object
player.isSensor = true;
end

local function onCollision(self, event)
local player = self;
local collidedWith = event.other;
local phase = event.phase;

if (collidedWith.objectType == “ground”) then
return true; – we don’t care about the ground
end

if (phase == “began”) then
print(collidedWith.objectType…" began")

elseif (phase == “ended”) then
print(collidedWith.objectType…" end")
player.isSensor = false;
end
end

– Setup the event listeners
playerInstance.collision = onCollision;
playerInstance.preCollision = onPreCollision;
playerInstance:addEventListener(“collision”, playerInstance);
playerInstance:addEventListener(“preCollision”, playerInstance);

Runtime:addEventListener(“enterFrame”, onEnterFrame);
playerInstance:addEventListener(“touch”, onTouch);[/lua]
PS. The basis for this code was posted by another developer on this forum who had difficulty with platform logic that I helped out with and added to it.
[import]uid: 70847 topic_id: 19557 reply_id: 75503[/import]

Great, that approach seems to be very usable!

Never thought of changing from sensor to solid object.

And if I don’t want the character to jump through the platform, I can change it from sensor to solid when the player is at same level or above - that approach would work?

Happy x-mas, Joakim
[import]uid: 81188 topic_id: 19557 reply_id: 75506[/import]

Ooops. After re-reading my post I made a small mistake. I’m turning the player into a sensor, not the platform. Not really a big diff, but that’s what the code does…

Well, if you don’t need to jump through the platform, then you don’t need to change the player to a sensor at all.
As long as the player and the platform are physics objects, then the physics engine will take care of making sure that the player will land and stay on the platform. Actually, you don’t even need collision detection for that to happen. It should all happen “automagically” :slight_smile:

God Jul. [import]uid: 70847 topic_id: 19557 reply_id: 75509[/import]

God jul själv i stugan :slight_smile:

Well, I can’t get my character to attach properly to the platform since it is static and moving around a bezier path. My character don’t glue on to it because of the physic behavior, and just slipping off. Any other suggestions?

Joakim

[import]uid: 81188 topic_id: 19557 reply_id: 75533[/import]

Hello Joakim. did you find a solution? to the problem of the platform?.
I’m trying to do something similar, and like always your my character adiere half of the platform, and that is way too forced, I do not like.

See this is what I am trying to do but I imagine they are similar to problems you had.

http://developer.anscamobile.com/forum/2012/03/29/problem-joint-using-platform-transition-during-collision-event

Any solution?

Thank you.
Greetings Nko.
[import]uid: 88254 topic_id: 19557 reply_id: 97317[/import]