Landing after jumping

BlitzMax Forums/BlitzMax Beginners Area/Landing after jumping

I want to make my game maneuverable enough so that I do not have to write an "imagescollide" code for every image that comes in contact with my main character. But I just can't figure out how to do this without my character going through the other objects. My main goal is to make a side scrolling game similar to mario and donkey kong country.

If anyone could tell me a simple way of either setting an image so that I could make multiples of it where ever I please and be able to land on it after jumping or falling onto it successfully, I would be very happy. Thanks!

Also, I've been having trouble with my character floating in mid-air until I jump. After I have him jump, he will fall through. Is there any way of making my guy fall once he has barely walked off a ledge?
I think it might be because I set it so that after landing on an image, the character's y-coordinate inherited the image's y-coordinate (minus 75 pixels so that the character is standing ontop of the ground and not level with it).
Thanks to anyone that helps me with my questions.

As for the collision detection algorithm issue, I'd make a class for each of the various types of things in your game. Have a collision routine that returns a boolean if it happened. You'll need to make a matrix (or some way) to track all the objects to determine where the avatar is then check collsion in the matrix coords of where its at.

Just iterate through the objects until a collision (or multiple collisions) is found. You can then apply what code you need to then for the reaction to the collision.

Your movement should use a gravity algorithm with speed. This will keep your avatar from floating, as it constantly applies gravity. You can search on the web for gravity and speed algorithms. If you get stuck, I'll see what I have to give you help there. I'm at work atm, so I don't have it handy with me.

I'll see what I can find for you later tonight.

Ok, I did some searching around for you on this as I promised. I didn't find the exact example I was originally looking for, BUT this one definitely does what I was looking for!

Strict

Const Gravity:Float = 1.0
Global PlatformList:TList = CreateList()


Graphics 640,480,0
SetClsColor 100,150,255

Local p1:Player = Player.Create(320,240)

Platform.Create(320,300)
Platform.Create(320,100)
Platform.Create(250,200)

Platform.Create(320,400,700,80)
While Not KeyHit(KEY_ESCAPE)
	Cls
	
	For Local p:Platform = EachIn PlatformList
		p.Render()
	Next
	p1.Update()
	p1.Render()
	
	Flip
	'FlushMem
Wend


Type Platform
	Field X:Float
	Field Y:Float
	Field Width:Int
	Field Height:Int
	
	Function Create:Platform (_X:Float, _Y:Float, _Width:Int = 64, _Height:Int = 16)
		Local p:Platform = New Platform
		p.X = _X
		p.Y = _Y
		p.Width = _Width
		p.Height = _Height
		PlatformList.AddLast(p)
		Return p
	End Function
	
	Method Render()
		SetColor 0,128,0
		DrawRect X-(Width/2),Y,Width,Height
	End Method
	
End Type

Type Player
	Field X:Float
	Field Y:Float
	Field VX:Float
	Field VY:Float
	Field Width:Int
	Field Height:Int
	Field Controls:Int [4]
	
	Function Create:Player (_X:Float, _Y:Float, _Width:Int = 32, _Height:Int = 64)
		Local p:Player = New Player
		p.X = _X
		p.Y = _Y
		p.Width = _Width
		p.Height = _Height
		p.Controls = [KEY_UP,KEY_LEFT,KEY_RIGHT]
		Return p
	End Function
	
	Method Render()
		SetColor 255,128,0
		DrawRect X-(Width/2),Y-Height,Width,Height
	End Method
	
	Method Update()
		If KeyDown(Controls[1]) Then VX :+ -1 'LEFT
		If KeyDown(Controls[2]) Then VX :+ 1 'RIGHT

		'I'm most likely not using the best method
		'for jumping or collision response here
		If KeyHit(Controls[0]) And VY >= 0
			VY = -15
		End If

		Y :+ VY
		X :+ VX
		VY :+ Gravity
		VX :+ (0 - VX) * .05
		Local P:Platform = CollidedWithPlatform()
		If P
			If Y < P.Y + P.Height And Y > P.Y And VY > 0 Then
				Y = P.Y
				VY = 0
			End If
		End If
		
	End Method
	
	Method CollidedWithPlatform:Platform()
		For Local p:Platform = EachIn PlatformList
			If (p.X + p.Width/2 < X - Width/2)	Continue
			If (p.X - p.Width/2 > X + Width/2) Continue 
			If (p.Y + p.Height < Y - Height) Continue
			If (p.Y > Y) Continue 
			Return p
		Next
	End Method
End Type


The original thread is here as posted by altitudems. I updated it a bit (removed the FlushMem command, as its been since deprecated)

You can also read up on this tutorial

I hope this helps, and good luck with your game!