verlet limiting...

Blitz3D Forums/Blitz3D Programming/verlet limiting...

RexRhino(Posted 2003) [#1]
OK... I have rewritten my verlet library to eliminate the slow stuff.

The trouble I am having now is when I try to limit the magnitude of my vectors based on the distance they have traveled... for some reason is zeros out the vectors. Here is the code:

Function verlet_inertia_entity(entity)
	hash = verlet_get_hash(entity) ;get an array reference based on the entity number
	
	elapsed_millisecs# = MilliSecs() - verlet_last_clock(hash) ;elapsed milliseconds since last time manipulating this entity
	elapsed_seconds# = elapsed_millisecs# / 1000.00 ;converted elapsed milliseconds to elapsed seconds
	dampening# = 1.0 - (verlet_dampening#(hash) * elapsed_seconds#) ;multiply this by vectors to dampen them according to setting
	
	init_x# = EntityX#(entity, True) ;record the position of an entity before movement
	init_y# = EntityY#(entity, True)
	init_z# = EntityZ#(entity, True)
	
	gravity# = (-9.8 * elapsed_seconds# * verlet_gravity#(hash)) ;this is how much gravity will move the entity
	
	verlet_vector_x#(hash) = (verlet_vector_x#(hash) * dampening#) ;dampen the vectors and add gravity
	verlet_vector_y#(hash) = (verlet_vector_y#(hash) * dampening#) + gravity#
	verlet_vector_z#(hash) = (verlet_vector_z#(hash) * dampening#)
	
	movement_x# = (verlet_vector_x#(hash) * elapsed_seconds#) ;calculate the movement by multiplying vector times elapsed seconds
	movement_y# = (verlet_vector_y#(hash) * elapsed_seconds#)
	movement_z# = (verlet_vector_z#(hash) * elapsed_seconds#)
	
	TranslateEntity entity, movement_x#, movement_y#, movement_z#, True ; move the entity
	
	verlet_constrain_entity(entity) ;constrain an entity to it's linked entities
	
	final_x# = EntityX#(entity, True) ;find out the positions now that we moved the entity
	final_y# = EntityY#(entity, True)
	final_z# = EntityZ#(entity, True)
	
	distance_x# = final_x# - init_x# ;figure out the distance for each vector we moved.
	distance_y# = final_y# - init_y# 
	distance_z# = final_z# - init_z#
	
	seconds_adjustment# = 1.0 / elapsed_seconds# ;calculate how many times our elapsed seconds fits into a second
	
	;THIS PART IS CAUSING TROUBLE!!!******************************************************
	verlet_vector_x#(hash) = (distance_x# * seconds_adjustment#) ;limit vectors by setting them to the distance moved scaled over a second
	verlet_vector_y#(hash) = (distance_y# * seconds_adjustment#)
	verlet_vector_z#(hash) = (distance_y# * seconds_adjustment#)
	;*************************************************************************************
	
	verlet_last_clock(hash) = MilliSecs() ;update the milliseconds 
End Function


I have highlighted the part that is causing the problem. Strangly enough, when I put a stop in the function and trace the variables, everything appears to work fine.


Shambler(Posted 2003) [#2]
If it works OK in debug I would assume its the

seconds_adjustment# = 1.0 / elapsed_seconds# ;calculate how many times our elapsed seconds fits into a second


which is giving a value less than 1 in debug because you are running slow and greater than 1 in non debug.


Michael Reitzenstein(Posted 2003) [#3]
verlet_vector_z#(hash) = (distance_y# * seconds_adjustment#)



Shambler(Posted 2003) [#4]
hehe its a classic, I missed that one too =*)


RexRhino(Posted 2003) [#5]
Thanks for pointing out the error Micheal... it didn't fix the problem, but I am sure it would have caused more problems in the future.

which is giving a value less than 1 in debug because you are running slow and greater than 1 in non debug.

This might be it... In fact, that is what I suspect might be the problem. but why? It is supposed to be greater than 1 in non-debug.

Could it be that the variables cannot handle the small fractions involved? I thought real number variables could handle it.

It is weird though, because if I comment out the part where I mentioned the problem, it works fine (except it doesn't properly limit verlet vectors, so therefore it is "jerky").