Box2d Libgdx Velocity skips too many pixels so I can't stop where I want -


so screen 1280x720 following setup:

static final float box_step=1/60f;   static final int box_velocity_iterations=6;   static final int box_position_iterations=2;   static final float world_to_box=0.01f;   static final float box_to_world=100f; 

as can see using pretty normal parameter here. set velocity of object follows:

    if(screenx - slimebody.getposition().x * box_to_world > 0)         velocity = 8;     else         velocity = -8;      slimebody.setlinearvelocity(velocity, 0);             target = screenx; 

but want stop @ given point , use following function:

if(slimebody.getposition().x * box_to_world +10 > target && slimebody.getposition().x * box_to_world < target)         slimebody.setlinearvelocity(0,0); 

now don't using because produces jitters when moving object side side quickly. because moment set velocity object, goes fast skips ton of coordinates, in pixels talking anywhere 10-20 , in meters .10-.20

it crucial me detect movement down millimeter. if gonna tell me take down velocity, movement slow purpose. anyway force object register more pixels,coordinates when traveling can stop more precisely?

i make assumption interested in stopping @ target position instead of slowing down. assume moving in x direction , not interested in y component.

a simple way solve problem find distance between current position , target position. if @ (10, 10) , target (20, 10) distance 10. moving @ 180 units / second, means 3 units / frame @ 60 fps. should pretty clear not able stop @ target because 3 frames , 4 frames long. instead, need interpolate between frames determine if can reach our target 'during' frame.

this isn't hard, know how fast going, know our current location, , know want go. on third frame @ (19, 10) , our target (20, 10). math.abs(20 - 19) < 3, can set position (20, 10) , set our velocity 0.

this 1 solution problem facing, 1 of simplest. hope helps :)


Comments