Jebbs

BlitzMax Forums/BlitzMax Beginners Area/Jebbs

Topic- Is it possible to use speed to create a higher jump?

I want my game character to be able to jump higher when the he is running than when he is walking. I know how to jump and how to get my character to run; I just can't add them together to create height.

I would also like to get my character to bounce back off walls when he hits them. Let's say he was running straight at a wall and when he hits it, he walks a few steps backwards.

If anyone has any insight about what I should do, I would be eternally grateful if they would post it. Thanks!

PS- Maybe a way of creating momentum might be a solution for making these effects.

Here's an example I have knocked up for you that should demonstrate the basics of quick and easy pseudo character physics. There may be better ways to do this, but this seems to tick all of your requirements above...

Hope you are OK with OO ;-):

SuperStrict

AppTitle = "Simple Collision Example"
Graphics 800,600

Global mainPlayer:TMainPlayer = New TMainPlayer

Repeat
	
	If KeyDown(KEY_LEFT);mainPlayer.SlowDown()
			
	If KeyDown(KEY_RIGHT);mainPlayer.SpeedUp()
		
	If KeyHit(KEY_SPACE) Or KeyHit(KEY_UP);mainPlayer.Jump()
	
	Cls
	
	TCharacter.UpdateCharacters()
	
	SetColor 255,128,64
	DrawText "Horizontal Speed: " + mainPlayer.speed[0] + "  Vertical Speed: " + mainPlayer.speed[1], 0, 0
	DrawText "Horizontal Position: " + mainPlayer.position[0] + "  Vertical Position: " + mainPlayer.position[1], 0, 12
	
	Flip;Delay 5
	
Until AppTerminate() Or KeyHit(KEY_ESCAPE)


Type TCharacter
	
	Global lstCharacter:TList = New TList
	
	Field position#[] = [0.0,0.0]
	Field speed#[] = [0.0,0.0], speedJumpMultiplier# = 1.5
	Field gravity# = 5.0, resistance# = 0.75
	
	Function UpdateCharacters()
		For Local tmpCharacter:TCharacter = EachIn lstCharacter
			tmpCharacter.Update()
			tmpCharacter.Draw()
		Next
	EndFunction
	
	Method New()
		lstCharacter.AddLast Self
	EndMethod
	
	Method Update()
		UpdatePosition();UpdateSpeed()
	EndMethod
	
	Method Jump()
		'If the character should jump, then increase the vertical speed by 20, and a multiple of the horizontal speed.
		If position[1] = 0 Then speed[1] = 20 + Abs(speed[0])*speedJumpMultiplier
	EndMethod
	
	Method SpeedUp()
		'If the character is moving left we want to decellerate at a smaller rate than if we are already running
		'in the same direction (up to a maximum speed of 15).
		If speed[0] < 0 Then speed[0]:+0.75 Else speed[0] = Min(speed[0]+1.5,15)
	EndMethod
	
	Method SlowDown()
		'If the character is moving right we want to decellerate at a smaller rate than if we are already running
		'in the same direction (up to a maximum speed of 15).
		If speed[0] > 0 Then speed[0]:-0.75 Else speed[0] = Max(speed[0]-1.5,-15)
	EndMethod
	
	Method Draw();EndMethod
	Method UpdateSpeed();EndMethod
	Method UpdatePosition();EndMethod

EndType

Type TMainPlayer Extends TCharacter
	
	Global width% = 20
	Global height% = 40
	
	Method Draw()
		'Select a colour for our character depending on his current action.
		Select True
			Case position[1]>0	'Jumping
				SetColor 0,0,255
			Case speed[0]<> 0		'Moving
				SetColor 0,255,0
			Default			'Standing Still
				SetColor 255,255,255
		EndSelect
		'Draw our make-shift character - note, position[1] refers to a vertical position
		'with the bottom of the screen being 0, and the top of the screen with a value of GraphicsHeight().
		DrawRect position[0],GraphicsHeight()-position[1]-height,width,height
	EndMethod
	
	Method UpdateSpeed()
		'If our character is in the air, we accelerate the vertical speed otherwise the vertical speed is 0.
		If position[1] > 0 Then speed[1]:-gravity Else speed[1] = 0
		'If our character is moving then slowly decrease the speed to simulate resistance.
		If speed[0] <> 0 Then speed[0]:-(speed[0]/Abs(speed[0]))*resistance
		'If our character is about to travel off the screen, then we reverse the direction travelling to simulate rebounding.
		If (position[0]<=0 And speed[0] < 0) Or (position[0]>=(GraphicsWidth()-width) And speed[0] > 0) Then speed[0] = -(speed[0])
	EndMethod
	
	Method UpdatePosition()
		'Add the horizontal speed to the current position, and ensure the position is within the screen.
		position[0]:+speed[0]
		position[0] = Min(Max(position[0],0), GraphicsWidth()-width)
		
		'Add the vertical speed to the current position, and ensure the position is not below the bottom of the screen.
		position[1]:+speed[1]
		position[1] = Max(position[1],0)
	EndMethod
	
EndType


I've tried to document the code well, so that you can see what is happening.

Perfect! Thanks a lot SebHoll. That was just what I was looking for.