Game Development Community

Moving horizontal stretches jump

by Anthony Mills · in Torque 2D Beginner · 11/23/2014 (3:38 pm) · 6 replies

function ShooterControlsBehavior::updateHorizontal(%this)
{
	if (%this.moveLeft) {
		%dir = -1;
		%this.Velocity_X -= %this.Accel_X;
	}
	else if (%this.moveRight)
	{
		%dir = 1;
		%this.Velocity_X += %this.Accel_X;
	}
	else
	{
		%dir = -1;

		if (%this.Velocity_X > 0)
			%dir = 1;

		if (mAbs(%this.Velocity_X) > mAbs(%this.Decel_X))
			%this.Velocity_X -= %dir*%this.Decel_X;
		else
			%this.Velocity_X = 0;
	}

	if (mAbs(%this.Velocity_X) > mAbs(%this.MaxVelocity_X))
		%this.Velocity_X = %dir*%this.MaxVelocity_X;

	%this.owner.setLinearVelocityX(%this.Velocity_X);
} 
function ShooterControlsBehavior::updateVertical(%this)
{
   if(%this.jump && %this.owner.getCollisionDOWN())
   {
        %this.owner.setLinearVelocityY(%this.MaxVelocity_Y);
   }
   if(%this.owner.getLinearVelocityY() > 0 && !%this.owner.getCollisionDOWN() && !%this.jump)
   {
      %this.owner.setLinearVelocityY(%this.owner.getLinearVelocityY() - %this.decel_Y);
   }
}


This is my code for my update movement controls. updateMovement() calls these to. When I move to the left or right my jump is stretched far more that suppose to. I don't see what I am doing wrong.


Thanks in advace.

About the author

Recent Threads

  • Collisions (Solved)

  • #1
    11/24/2014 (6:04 am)
    Try fiddling with your player object's linear damping instead of using deceleration values - T2D's new physics system (box2d) doesn't work anything like the original Torque 2D. So setting velocity to (current - decel) will reset the object's velocity to that amount. You should probably be relying on the object's linear damping for deceleration.
    #2
    11/24/2014 (5:44 pm)
    I probably should have said its a 2d platformer I tried damping and in the air it doesn't help.

    https://drive.google.com/folderview?id=0B-a3QFri-PeWMWU1a3pEb1d6ZXM&usp=sharing


    This is the whole project. As anyone will see when you move the character left and right or jump everything is fine, but when you combine the to it is terrible if there is anything I'm doing wrong please let me know.
    #3
    11/26/2014 (6:59 pm)
    backup your files first :)


    in your createScript.cs, update

    $hero.setDefaultDensity( 1 );
    $hero.setDefaultFriction( 0 );


    in your startingArea main.cs

    update gravity to:
    myScene.setGravity(0, -100);




    shooterControls.cs

    Update
    %MaxX = 10000;
    %MaxY = 20000;

    %template.addBehaviorField(Accel_X, "X-Axis acceleration", float, %MaxX );
    %template.addBehaviorField(Decel_X, "X-Axis deceleration", float, %MaxX/20 );
    %template.addBehaviorField(decel_Y, "Y-Axis deceleration", float, %MaxY/2 );
    %template.addBehaviorField(MaxVelocity_X, "X-Axis max velocity", int, %MaxX);
    %template.addBehaviorField(MaxVelocity_Y, "Y-Axis max velocity", int, %MaxY);


    and at the end,
    function ShooterControlsBehavior::updateHorizontal(%this) {
    
    	%velX = %this.owner.getLinearVelocityX();
    	   
    	if (%this.moveLeft) {
    	
    		%dir = -1;
    		%this.Velocity_X -= %this.Accel_X;
    		%this.owner.setDefaultFriction( 0 );
    	}
    	else if (%this.moveRight) {
    	
    		%dir = 1;
    		%this.Velocity_X += %this.Accel_X;
    		%this.owner.setDefaultFriction( 0 ); 
    	}
    	else {
    	
    		%dir = -1;
    
    		if (%this.Velocity_X > 0)
    			%dir = 1;
    			
    		if (mAbs(%this.Velocity_X) > mAbs(%this.Decel_X)) {
    		
    			%this.Velocity_X -= %dir*%this.Decel_X;
    		}
    		else {
    		
    			%this.Velocity_X = 0;
    			
    			// don't stop moving while jumping in air
    			if(!%this.owner.isJumping) {
    			
    				// We are on ground, stop moving
    				%this.owner.setDefaultFriction( 1 ); 
    				%this.owner.applyLinearImpulse( - %velX * %this.MaxVelocity_X, 0);
    			}
    		}
    	}
    
    	if (mAbs(%this.Velocity_X) > mAbs(%this.MaxVelocity_X))
    		%this.Velocity_X = %dir*%this.MaxVelocity_X;
    
    	// did we change direction?
    	if(%this.lastDir $= %dir) {
    	
    		// nope, keep moving
    		%this.owner.applyLinearImpulse(%this.Velocity_X, 0);
    	}
    	else {
    	
    		// yes, new dir, apply some impulse to stop us so we can move in new dir
    		// if not jumping
    		//if(!%this.owner.isJumping) 
    			%this.owner.applyLinearImpulse( - %velX * %this.Velocity_X, 0); //use MaxVelocity_X for more stopping power
    	}
    	
    	%this.lastDir = %dir;
    } 
    
    
    function ShooterControlsBehavior::updateVertical(%this) {
    
    	if(%this.jump && %this.owner.getCollisionDOWN() && !%this.owner.isJumping) {
    	
    		//echo("jump");
    		%this.owner.isJumping = true;
    		%this.owner.applyLinearImpulse(0, %this.MaxVelocity_Y, -1, 1);
    	}
    	else if(%this.owner.isJumping) {
    	
    		%velY = %this.owner.getLinearVelocityY();
    		//echo("isJumping("@%this.decel_Y@") "@%velY);
    
    		//if(%velY < 0) // drop faster??
    		//	%this.owner.applyLinearImpulse( 0, -%this.decel_Y, 1, 1);
    		
    		// finished jumping?
    		if(%velY $= 0)
    			%this.owner.isJumping = false;
    	}
    	else {
    
    		%velY = %this.owner.getLinearVelocityY();
    		//echo("Falling("@%this.decel_Y@") "@%velY);
    		
    		// Falling??
    		if(%velY < 0 && !%this.owner.getCollisionDOWN()) // && !%this.jump)
    		{
    			//echo("fall");
    			//%this.owner.applyLinearImpulse( 0, -%this.decel_Y, 1, 1);
    		}
    	}
    }
    #4
    11/26/2014 (8:11 pm)
    @Christian M Weber just want to thank you so much for helping me it is fixed. also there was a bug in the code at line 56 i change that line to

    %this.owner.applyLinearImpulse( -%dir* %velX * %this.Velocity_X, 0);


    i found that when moving right and changing direction it shoved the player more right so i fixed that and i just have one question how do i make velX faster it just feels the start is a little slow.

    Again thanks to everyone who is helping this is my first game and I'm trying my best :)
    #5
    11/27/2014 (9:21 am)
    This gives the guy a lil push if he was standing still.

    update
    %MaxX = 15000;

    at the end of updateHorizontal()
    // standing still, now moving?
    	if(%this.lastVelX $= 0 && mAbs(%velX) > 0) {
    	
    		//echo("new move");
    		%this.owner.applyLinearImpulse(%this.Velocity_X * 4, 0);
    	}
    	
    	%this.lastVelX = %velX;
    	%this.lastDir = %dir;
    }


    #6
    11/27/2014 (9:59 am)
    bonus... double jump!

    function ShooterControlsBehavior::updateVertical(%this) {
    
    	if(%this.jump && %this.owner.getCollisionDOWN() && !%this.owner.isJumping) {
    	
    		//echo("jump");
    		%this.owner.isJumping = true;
    		%this.jump = false;
    		%this.owner.applyLinearImpulse(0, %this.MaxVelocity_Y, 0, 0);
    	}
    	else if(%this.owner.isJumping) {
    	
    		// double jump?
    		if(%this.jump && !%this.owner.isDoubleJumping) {
    		
    			//echo("double");
    			%this.owner.isDoubleJumping = true;
    			%this.owner.applyLinearImpulse(0, %this.MaxVelocity_Y * 2, 0, 0);
    		}
    		else {
    		
    			%velY = %this.owner.getLinearVelocityY();
    		
    			// finished jumping?
    			if(%velY $= 0) {
    				%this.owner.isJumping = false;
    				%this.owner.isDoubleJumping = false;
    			}
    		}
    	}
    	else {
    
    		//%velY = %this.owner.getLinearVelocityY();
    		//echo("Falling("@%this.decel_Y@") "@%velY);
    		// Falling??
    		//if(%velY < 0 && !%this.owner.getCollisionDOWN()) // && !%this.jump)
    		//{
    			//echo("fall");
    			//%this.owner.applyLinearImpulse( 0, -%this.decel_Y, 0, 0); // fall faster
    		//}
    	}
    }