formula to set jump heights?

BlitzMax Forums/BlitzMax Beginners Area/formula to set jump heights?

coffeedotbean(Posted 2007) [#1]
I'm making a little game for fun but I've run into a little problem.

I need to make boxes jump to set heights in increments of 32px even after changing gravity on the fly. Currently i have manualy set the jump heights but this is not accurate and I cannot change the gravity becuase these fix values are for a set gravity value.

phew... any way heres some code. I have set the jump heights for the boxes in the Makeboxes() function.

so for example I would like to tell a box to jump 5 squares which is 5*32px = 160px and it would do that too exactly 160px no matter what the gravity was set too, and the same forumla can be used to tell a box to jump 1 sqaure or even a 100 squares.

So a function like GetJumpheight(squares,gravity) would return the correct value to attached b.JumpHeight or somthing along those lines.


Graphics 640,480,0,60

' Grabity applied to boxes
Global Gravity# = 0.60

' declare box type
Global L_Box:TList=New TList
Type T_Box
	Field xPos , yPos#=480-64
	Field JumpYVel# , JumpHeight# = 6.50
	Field id
End Type

' make the boxes
MakeBoxes()

'===============================

Repeat ; Cls

	UpdateBoxes()
	
	' Just the pink lines to show how high the box should jump.
	For x=640-64 To 64 Step -64
		SetColor 255 , 0 , 255 ; DrawLine x , i*32 , x+32 , i*32 ; i=i+1
	Next ; i = 5
	
	DrawText "Mouse over a box to make it jump",0,0

Flip ; Until KeyDown(KEY_ESCAPE) ; End

'===============================

Function UpdateBoxes()
	
	For b:T_Box = EachIn L_Box
		
		' draw box and its id.
		SetColor 255,0,0 ; DrawRect b.xPos , b.yPos , 32 , 32
		SetColor 255 , 255 , 255 ; DrawText b.id , b.xPos + 8 , b.yPos + 8
		
		' mouse over a box to make it jump
		If MouseX() => b.xPos And MouseX() =< b.xPos + 32 And MouseY() => b.yPos And MouseY() =< b.yPos + 32
			b.JumpYVel=b.JumpHeight
		EndIf
		
		' apply gravity to jumping boxes
		b.JumpYVel# = b.JumpYVel# - Gravity
		
		
		' update bos Y position
		b.yPos:- b.JumpYVel
		
		' Stop box falling off bottom of screen
		If b.yPos => 480 - 64 Then b.JumpYVel = 0.0 ; b.yPos = 480 - 64
		
	Next
	
EndFunction

Function MakeBoxes()
	
	' make 8 boxes and give a jump height based on its given ID
	For i=1 To 8
		b:T_Box = New T_Box
		ListAddFirst L_Box , b
		b.xPos = i * 64
		b.id = i
		'------------------- Not an ideal method, its not accurate and if I change gravity its all wronge
		'------------------- must be a fancy formula that could be used? to apply accurate jump height to
		'------------------- boxes even after chnaging Gravity on the fly. Boxes ment to jump in 32px increments.
		If b.id = 1 Then b.JumpHeight# = 6.50
		If b.id = 2 Then b.JumpHeight# = 9.00
		If b.id = 3 Then b.JumpHeight# = 11.00
		If b.id = 4 Then b.JumpHeight# = 12.70
		If b.id = 5 Then b.JumpHeight# = 14.10
		If b.id = 6 Then b.JumpHeight# = 15.50
		If b.id = 7 Then b.JumpHeight# = 16.70
		If b.id = 8 Then b.JumpHeight# = 17.80
		'-------------------
		'-------------------
		'-------------------
	Next

End Function



Czar Flavius(Posted 2007) [#2]
Hm. I've put together this, which offers a different way around the problem. Your code attempts to pre-workout the jump height.

My one acts more like a physics simulation that has no knowledge or care of predicting the future, and works out the velocity on the fly.

I'm not sure how useful you will find it, but maybe looking at the problem from a different angle will offer ideas.

The reason why there is so much difference between the jump height of the boxes, is because I made the mass of each box relative to its id number. Eg box 8 is 8 times more massive than box 1. Change this to get less extreme ranges in height.

Also I'm no physics expert so if anyone sees a mistake in my code, please comment.


Strict

Graphics 640, 480, 0, 60

Const thefloor = 480-64 'boxes cannot fall below this point

Global gravity# = 0.6
Global momentum# = 8

Global L_Box:TList = New TList
Type T_Box
	Field xpos, ypos# = thefloor
	Field accel# = 0 'acceleration
	Field velocity#
	Field mass#
	Field id
End Type

makeboxes(8, 0.3) 'make 8 boxes, with mass cooefficent of 0.3 or whatever

Repeat
	Cls
	
	updateboxes()
	
	Flip
	
Until KeyDown(KEY_ESCAPE)
End

Function makeboxes(number, relmass#)
	Local box:T_Box
	For Local i = 1 To number
		box = New T_Box
		ListAddFirst L_Box, box
		box.xpos = i * 64
		box.id = i
		box.mass = i * relmass
	Next
End Function

Function updateboxes()

	For Local box:T_Box = EachIn L_Box
	
		SetColor 255,0,0 ; DrawRect box.xpos , box.ypos , 32 , 32
		SetColor 255 , 255 , 255 ; DrawText box.id , box.xpos + 8 , box.ypos + 8
		
		If box.velocity = 0 And MouseX() => box.xPos And MouseX() =< box.xPos + 32 And MouseY() => box.yPos And MouseY() =< box.yPos + 32
			box.velocity = -momentum / box.mass 'each box is given a momentum. velocity = momentum / mass
			box.accel = gravity 'gravity affects all objects equally, regardless of mass
		EndIf
		
		box.ypos :+ box.velocity
		box.velocity :+ box.accel
		
		If box.ypos > thefloor
			box.ypos = thefloor
			box.velocity = 0
			box.accel = 0
		EndIf
	
	Next
	
End Function




coffeedotbean(Posted 2007) [#3]
Hi, thanks for the reply ill look over ya code later and see if it helps, maybe I should go more towards the physic side could make some nice effects that way. =D


Who was John Galt?(Posted 2007) [#4]
Try
v=(2gh)^0.5
v is your start speed, g is the change in velocity per loop.