PDA

View Full Version : [AS3] Hit collision issues caused by screen scrolling


pondwater
09-10-2009, 01:35 AM
Over the past two weeks i've been attempting to implement a scrolling feature to my 2D game engine. When implementing the code i get a strange 'ghost surface problem'. This can basically be explained as if the X and Y coordinates become detached from the graphics as the screen scrolls more and more.

The scrolling code used is:

var stagePosition:Number = gamelevel.x + player.x;
var edgeDistance:Number = 200;
var rightEdge:Number = stage.stageWidth-edgeDistance;
var leftEdge:Number = edgeDistance;
if (stagePosition > rightEdge) {
gamelevel.x -= (stagePosition-rightEdge);
movementAdjust -= stagePosition-rightEdge;
if (gamelevel.x < -(gamelevel.width-stage.stageWidth)) {
gamelevel.x = -(gamelevel.width-stage.stageWidth);
}
}
if (stagePosition < leftEdge) {
gamelevel.x += (leftEdge-stagePosition);
if (gamelevel.x > 0) {
gamelevel.x = 0;
}
}

So far a few possibilities of what could be causing this i've tried to narrow down are:

POSSIBILITY 1:

Non tiled based environment - i wanted to create an engine that would work with any type of surface, level, wavy, sharp, etc. Therefore my hit collisions use loops to determine if a collision is within the next movement amount. For example, for the vertical movement, ie gravity, if the players falling speed is in 5 pixel intervals, the loops will test if a collision is within 1,2,3,4 or 5 pixels. if not, the movement will continue, if it detects a collision within that loops, the player will move the respective distance and stop.

A simplified version of this without if statements for hero graphic changes is:

// Check collision under hero
for (var i:int =1; i <= (hero.dy*2); i++) {
// Half i value to account for 0.5 gravity values
iModify = i/2;
// Collision test
if (platform.hitTestPoint(player.x, player.y+iModify, true)) {
// Move player right above ground
player.y += iModify - 0.5;
// Landing Graphic check
}
}

However to compensate for uneven ground surfaces with horizontal movement i implemented a system to check whether the player is should move slightly upwards or downwards. I set a threshold for this, i believe its around 5 pixels. This uses a loop as well. If the vertical change in the pixels directly in front of the character is within 5, then character will continue moving horizontal and the character will be pushed up or down the respective distance.

A simplified version of this code without graphic changes is (this is only the running left detections, it is similar for running right):

if (hero.runningLeft == 1) {
hero.moveCount = hero.runSpeed;
for (var h:int = 1; h <= hero.runSpeed * 2; h++) {

diagonalMove = false;
// diagonal up check
for (var r:int = 1; r <= hero.moveCount*2; r++) {
rModify = r/2;
//check 0-5 distances in 0.5 steps
if (platform.hitTestPoint(player.x-0.5, player.y, true)) {
if(platform.hitTestPoint(player.x-0.5, player.y - rModify, true)) {
} else {
player.x -= 0.5;
player.y -= r-0.5;
hero.moveCount -= 0.5;
diagonalMove = true;
break;
}
}
}
//diagonal down check
if (diagonalMove == false) {
for (r = 1; r <= hero.moveCount*2; r++) {
rModify = r/2;
//check 0-5 distances in 0.5 steps
if (platform.hitTestPoint(player.x-0.5, player.y, true)) {
if (platform.hitTestPoint(player.x-0.5, player.y + rModify, true)) {
} else {
player.x -= 0.5;
player.y -= r-0.5;
hero.moveCount -= 0.5;
diagonalMove = true;
break;

}
}

}
}
diagonalMove = false;
}
// horizontal move check
for ( r = 1; r <= ((hero.moveCount) * 2); r++) {
rModify = r/2;
if (platform.hitTestPoint(player.x-rModify, player.y, true)) {

player.x -= rModify-0.5;
// Reset Runspeed
hero.runningLeft == 0;
break;
}
// Move hero rest of moveSpeed
if (rModify == hero.moveCount) {
player.x -= hero.moveCount;
break;
}
}

Would this type of collision detection clash with the tiled based scrolling code i used? If so, any suggestions of a different type?

POSSIBILITY 2:

Am i referencing the instances correctly? Because it seems as tho certain instances would get detached or while the graphics move, the x and y properties do not change accordingly?

SOURCE FILES:

Here are links to the flash files in case you would like to take a look:

the .as
http://www.mediafire.com/?bbbn1dejbej

the .fla
http://www.mediafire.com/?0hxznb9beyn