Hi All,
<note: Moved this here from Game programming forum cause it doesn’t like anyone uses that one anymore>
I’m a bit new to programming and have run into an issue with collision in a platforming game I’m making (a learning project for me).
I am working to simulate a “real programming scenario” as best I can with the limited knowledge I have, and to that effect am working with a number of interfaces and classes. I have no doubt I’ll look back in a year and wonder why I set things up exactly the way i did, but that’s why I’m learning
I have set up collision using hitTestPoint with points on top, right, left, and bottom of my player, against the background. These register just fine, and contact with the background provides a “true” result for all of my points.
The issue is that the player will go right through most surfaces (especially the ground) anyway. In my current build, the player will hit the floor, pause for half a second, and then continue on through like nothing was there.
As best I can tell, it has nothing to do with collision and everything to do with how I’m calculating and rendering player movement. I don’t think my character size is too small, collision surface is too thin, or speed is too high.
I would love it if someone could help me identify the problem and the underlying logic/cause of it. Thanks much and code is posted below
update function in main file that triggers calculation of player movement variables
private function update(e:Event):void {
_deltaTime = (getTimer() - _previousTime);
_previousTime = getTimer();
applyForces();
//trace(_deltaTime);
}
applyForces function actually calculates player movement variables in main file
function applyForces():void //sets values of forces on objects and prepares them to be moved {
var gravity:Number = _config.gravity;
var friction:Number = _config.friction;
var jumping:Boolean = _player.isJumping;
_player.moveSpeedHorizontal *= friction;
if(_player.collideTop)
{
if(_player.moveSpeedVertical < 0)
{
_player.moveSpeedVertical *= -.75;
}
}
if(_player.collideRight)
{
if(_player.moveSpeedHorizontal > 0)
{
_player.moveSpeedHorizontal *= -.75;
}
}
if(_player.collideLeft)
{
if(_player.moveSpeedHorizontal < 0)
{
_player.moveSpeedHorizontal *= -.75;
}
}
if(_player.collideBottom)
{
_player.moveSpeedVertical = 0;
}
else
{
_player.moveSpeedVertical += gravity;
}
//stop player movement completely to prevent oversliding
if(Math.abs(_player.moveSpeedHorizontal) < .05)
{
_player.moveSpeedHorizontal = 0;
}
render();
}
code in main file which actually renders player movement on screen
private function render():void //actually moves all game objects on the screen {
_player.x += (_player.moveSpeedHorizontal * _deltaTime); //move player right and left
_player.y += (_player.moveSpeedVertical * _deltaTime); //move player up and down
//trace(_player.x);
if(_canScroll)
{
//*IN THE WORKS* system for easing the screen view so the player isn't always exactly in the middle, creates effect like the "camera" is catching up to him. Only have logic figure out for it to work in one direction
if(_lastReleasedHorizontal == "right")
{
_view.x = (_player.x-(_view.width/2) + _scrollDelay);
}
else if(_lastReleasedHorizontal == "left")
{
_view.x = (_player.x-(_view.width/2) + _scrollDelay);
}
_view.y = _player.y-(_view.height/2); //actually controls "camera" view and scrolling, needed if you want camera to follow player
this.scrollRect = _view;
}
code in the player class which calculates movement variables to be passed to main class. I have getter and setter functions later in player code which retrieve the moveSpeed variables.
public function calculatePlayerMoveSpeed(e:Event) {
var movement:Number = _config.playerMovement;
var friction:Number = _config.friction;
if(_leftPressed && _moveSpeedHorizontal >= -_config.playerMaxSpeed)
{
_moveSpeedHorizontal -= movement;
}
if(_rightPressed && _moveSpeedHorizontal <= _config.playerMaxSpeed)
{
_moveSpeedHorizontal += movement;
}
}
World variables for friction, gravity, etc.
//world physics, these are default variables that will likely be adjusted depending on what level the player is on public var gravity:Number = .01;
public var friction:Number = .75; //will need to create multiple different friction values if I want to have different terrain types
public var drag:Number = .92;
//Player movement
public var playerMovement:Number = .20;
public var playerMaxSpeed:Number = .4;
public var playerMinspeed:Number = .05;
public var playerJump:Number = .5;