cant fall off platform. HELP :):)

Blitz3D Forums/Blitz3D Beginners Area/cant fall off platform. HELP :):)

GameCoder(Posted 2003) [#1]
Heres my code guys. I can jump, But when i hit the platform i cant fall of it. To stay on the platform I have set the character Y pos to the same as the platforms.

I use a 2 pixel line on the platform and on the characters feet to test for collision.


Graphics 640,480
SetBuffer BackBuffer()

;>>>>>>>>globals<<<<<<<<<<<<<<<;
Global dude 					= LoadImage ("dude.png")
Global platform 				= LoadImage ("platform.png")
Global dl						= LoadImage ("dudeline.png")
Global pl 						= LoadImage ("platline.png")
Global px						= 450
Global py						= 375
Global dudeX#				= 300
Global dudeY#				= 390
Global canjump				= 0
Global jump 					= 0
Global jumpheight#		= 10
Global fall 						= 0
Global collide 				= 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;>>>>>>>>>constants<<<<<<<<<<<<;
Const gravity#				= .4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

While Not KeyHit ( 1 )
	Cls

		Locate 0,0
		Print "Collide = " + collide

		DrawImage platform,px,py
		DrawImage pl,px,py
		DrawImage dl,dudeX# + 4,dudeY# + 84
		DrawImage dude,dudeX#,dudeY#

				
			;<<<<Function call >>>>;
			jump()
			;------------------------------------
			;<<<<Function call>>>>>;
			ground()
			;------------------------------------;
			;<<<<Function Call>>>>>;
			movelr()
			;------------------------------------;
			;<<<<Function Call >>>>;
			collision()
			;------------------------------------;

	Flip

Wend
End

;>>>>>>>>>>>>>>FUNCTIONS<<<<<<<<<<<<<<<

Function jump()

	If KeyHit ( 57 )
		canjump  = 1
		jump       = 1
	End If

	If canjump = 1 And jump = 1 Then 
		dudeY# = dudeY# - jumpheight#
		jumpheight# = jumpheight# - gravity#
	Else
		jumpheight = 10
		canjump = 0
		jump = 0
	End If

	
End Function

Function ground()
	If dudeY# > 390 Then
	canjump   = 0
	jump        = 0
	dudeY# = 390
	End If
End Function

Function movelr()

	If KeyDown (203)
		dudeX# = dudeX# - 3
	ElseIf  KeyDown ( 205 )
		dudeX# = dudeX + 3
	End If

End Function

Function collision()

	If ImagesCollide (dl,dudeX# + 4,dudeY# + 84,0,pl,px,py,0) Then
		collide = 1
		canjump = 1
		jump = 0
		dudeY# = dudeY# 
		
	
	End If

End Function




Nebula(Posted 2003) [#2]
I looked at the source and found it constantly froze b+. One thing you should look at is not to use the Keyhit command for exiting the program. You can only use the keyhit command once inside a loop. Use keydown.

When your character jumps on top of a platform the collision takes effect. By using this collision and looking at when it does not happen we can tell the program to let the character fall.

Function collision()

	If ImagesCollide (dl,dudeX# + 4,dudeY# + 84,0,pl,px,py,0) Then
		collide = 1
		canjump = 1
		jump = 0
		dudeY# = dudeY# 
		ElseIf dudey<390 And jumpheight =10 Then
		canjump = 1
		jump = 1
		jumpheight = -10
	
	End If

End Function


Look at the ElseIf part. ElseIf we are not on the ground nor on a platform(collision) or not jumping now then set the variables for falling down.

The code looks very neat and organized btw.

Good luck.


WolRon(Posted 2003) [#3]
One thing you should look at is not to use the Keyhit command for exiting the program. You can only use the keyhit command once inside a loop. Use keydown.



This is not true. It's perfectly OK to use Keyhit() but just remember that you can only call the function once per keypress. If you need to check more than once the condition of whether or not a key was pressed then use a variable to retain the value of Keyhit() until the end of your loop (i.e. escapekeypressed = Keyhit(1)). And don't forget to reset the variable to false at the end of the loop.


GameCoder(Posted 2003) [#4]
Thankyou very much for that Nebula. It works perfactly.
I was wondering if you can give me some feedback on how I have determined how the code you have supplied works.

This is how I have explained it to myself.

When there is not a collision you have put that if dudeY is less than 390 (The Ground ) and jumpheight = 10. Then to activate a jump " canjump = 1 and jump = 1" but instead of making him jump upwards hes actually jumping towards the ground. Hes jumping towards the ground because jumpheight has been changed to negative number.

Have I interpreted this in the correct way?

THX again for helping me with this Nebula. Much Appreciated :)


GameCoder(Posted 2003) [#5]
Thx for that WolRon. Could I use 1 or 0 or would it be better to track it with true or false?


Nebula(Posted 2003) [#6]

When there is not a collision you have put that if dudeY is less than 390 (The Ground ) and jumpheight = 10. Then to activate a jump " canjump = 1 and jump = 1" but instead of making him jump upwards hes actually jumping towards the ground. Hes jumping towards the ground because jumpheight has been changed to negative number.



Yes, he jumps down. It is a quick hack. You might want to create some custom code for falling down.


GameCoder(Posted 2003) [#7]
I have tried various things to cause the character to fall when not on a platform but my noobiness wont allow me to figure it out.

This is the only way it works and the only way I understand how it works.

I can forsee a problem which might arrise due to using your method.

Because I'm using lines to detect collision Im hoping that if i put a platform right near the top of the screen, that when the character falls of it his downward velocity doesnt increase to the point where he misses a collision with another platform below. I had this problem when i first tried my jumping code. The downward velocity increased to a point where the actual "Y co-ordinate" of the line on the characters feet skipped hitting the line on the platform.


Nebula(Posted 2003) [#8]
I know. This is why I use a loop to move only one pixel at a time. If you skip 10 pixels you miss out the pixels in between.

example :
for i=0 to jumpheight
  dudey = dudey - 1
  if collision()=true then exit
next


Note that you would need to tell the function to 'return true' when the function changes something in the game.


GameCoder(Posted 2003) [#9]
sigh!! Im confused as to where to put this and when to use it. I understand the code, but is it to replace my actual jumping code or to be put where you put the last modification. Also in the code you have

for i=0 to jumpheight
  dudey = dudey - 1  <<< wouldnt this cause an upward motion instead of down
  if collision()=true then exit
next


Im sorry to keep asking all these questions. Im just really really new to all this and I really really want to create a platform game.


Nebula(Posted 2003) [#10]
Well what you could do is like below.

Modified jump function

Function jump()

	If KeyDown ( 57 ) And (jumpheight = 10 Or (Int(jumpheight = -1)))
		canjump  = 1
		jump       = 1
		jumpheight = 10
	End If

	If canjump = 1 And jump = 1 Then 		
		If jumpheight<0 Then ; down
			For i=0 To -10+Abs(jumpheight)*3
				If collision() = True Then Exit
				dudey# = dudey# + 0.5
			Next
			Else ; going up
			For i=0 To jumpheight
				If collision = True Then Exit
				dudeY# = dudeY# - 1
			Next
		End If
		jumpheight# = jumpheight# - gravity#
	Else
		jumpheight = 10
		canjump = 0
		jump = 0		
	End If

End Function



Notice I have two different loops, one for going up, and one for going down. This is for adding more realistic falling. I do not understand myself how it functions but it seems to work ok.
Also when jumping the jumpheight is now preset. Also the jump init here checks two states. Either the on a platform or on the ground state.

Also you need this slightly modified collision function.

Function collision()

If ImagesCollide (dl,dudeX# + 4,dudeY# + 84,0,pl,px,py,0) And jumpheight<0 Then
		collide = 1
		canjump = 1
		jump = 0	   
		Return True
		ElseIf dudey<390 And jumpheight =10 Then
		canjump = 1
		jump = 1
		jumpheight = -1		
		Return True
	End If	
Return False
End Function


As you can see, the function will return true when a collision occurs and also when no collision occurs when we are not on the ground. The jumpheight has been changed to -1 instead of -10. This for the new falling method which starts slowly and increases.

You might want to tweak the settings until you find they are comfortable.

Edit : minor adjustement.


WolRon(Posted 2003) [#11]
Could I use 1 or 0 or would it be better to track it with true or false?


TRUE and FALSE are constants that equate to 1 and 0 respectively. So there is technically no difference between them, just your preference.
I, myself, prefer to use true and false if there will only ever be two states because it usually makes more sense than 1 and 0.
However, if I have to add a third or more states than it would make more sense to use 0,1,2,3,4,etc. instead of false,true,2,3,4,etc.


GameCoder(Posted 2003) [#12]
nebula thank you very much. I understand the loop parts in the jumping code and im currently looking at the command reference to try to figure out what the rest means. :) Wish me luck.