Inaccuracy when moving game group based on a physics objects movement.

Hi all!

I’m having a little problem with some code that is bugging me. This is a very quick and dirty prototype for a game I want to try - keep in mind that I haven’t gotten very far yet. But i’ve hit this hurdle. I have a player object that can be shot into the air (using a drag gesture) - to keep it onscreen I check to see if the player object has gone past a threshold in the game world group, and then move the game world group according to keep the player onscreen.

Seems to work okay - except! When the player returns to the ground the Y position of the game group is different every time. Only by a bit, but it is quite noticeable. can anyone see where the inaccuracy is coming into it? The methodology used here is based off the egg breaker demo.

[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, 11);

– Setup background graphics
local sky = display.newImage(“sky.png”, true);
sky = 160;
sky = 240;

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

– Ground object
local ground = display.newImage(“ground.png”, true);
ground.x = 160;
ground.y = 440;
groundPhysics = { -160, 0, 160, 0, 160, 10, -160, 10 };
physics.addBody(ground, “static”, { friction=0.3, bounce=0.0, shape=groundPhysics });
gameGroup:insert(ground);

– Setup a sprite for the player
local sprite = require(“sprite”);
local spriteSheet1 = sprite.newSpriteSheet(“starcatSpriteSheet.png”, 64, 64);
local spriteSet1 = sprite.newSpriteSet(spriteSheet1, 1, 1);
sprite.add(spriteSet1, “sitting”, 1, 1, 1000, 0);
local playerInstance = sprite.newSprite(spriteSet1);
playerInstance:prepare(“sitting”);
playerInstance:play();
playerInstance.x = 100;
playerInstance.y = 400;
playerInstance.rotation = 0;
playerPhysics = { -8, 0, 12, 0, 12, 32, -8, 32 };
gameGroup:insert(playerInstance);
physics.addBody(playerInstance, “dynamic”, { density=.5, friction=0.15, bounce=0.0, shape = playerPhysics });
playerInstance.isFixedRotation = true;
– Setup some vars on the playerInstance
playerInstance.isFalling = false;
playerInstance.xVel = 0;
playerInstance.yVel = 0;

– Debug variables
local debugTextField1 = display.newText(" DEBUG: ", 50, 50, native.systemFont, 12);
debugTextField1:setTextColor(255, 255, 255);
debugTextField1.x = 100;
debugTextField1.y = 10;

local debugTextField2 = display.newText(" DEBUG: ", 50, 50, native.systemFont, 12);
debugTextField2:setTextColor(255, 255, 255);
debugTextField2.x = 100;
debugTextField2.y = 30;

local debugTextField3 = display.newText("", 50, 50, native.systemFont, 12);
debugTextField3:setTextColor(255, 255, 255);
debugTextField3.x = 100;
debugTextField3.y = 50;

– The main loop
function onEnterFrame(event)

– Get the players linear velocity
playerInstance.xVel, playerInstance.yVel = playerInstance:getLinearVelocity();

if playerInstance.yVel > 0 then
playerInstance.isFalling = true;
else
playerInstance.isFalling = false;
end

– 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 xForce = (-1 * (event.x - playerInstance.x)) * 2.75;
local yForce = (-1 * (event.y - playerInstance.y)) * 4;

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

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

end

end

end

– Setup the event listeners
Runtime:addEventListener(“enterFrame”, onEnterFrame);
playerInstance:addEventListener(“touch”, onTouch);[/lua] [import]uid: 41286 topic_id: 17136 reply_id: 317136[/import]