java - How can I flick an object so it moves indefinitely with velocity depending on force of flick? -
i have circle on graph, , want able flick , have move indefinitely , wrap around screen goes. circle, or ball how coded it, view. right have circle can touch , drage, via code:
//creates view ball framelayout flview = (framelayout) v; flview.setpaddingrelative(0, padding, 0, 0); //creates new ball ball = new ball(findviewbyid(r.id.main_view).getcontext(), x, y, 10,padding); switch(event.getaction()){ case motionevent.action_move: flview.removeallviews(); flview.addview(ball); break; case motionevent.action_down: flview.removeallviews(); flview.addview(ball); break; }
here's i'd do. i'd measure startposition (x,y) of ball, , time user first taps screen, ball, whatever. user moves finger across screen i'd calculate velocity given ball's current (x,y) , time. i'd calculate pixels / ms moving. once pick figure keep adding x,y velocity ball's position, , mod screen size it'll wrap.
// don't keep adding , removing ball view. // add once on startup, can move ball's // position when user clicks on screen. flview.addview(ball); switch(event.getaction()){ case motionevent.action_move: ball.x = event.x; ball.y = event.y; int lastpositionx = ball.x int lastpositiony = ball.y long lasttime = system.currenttimemillis(); ball.velocity.x = (lastpositionx - startpositionx) / (lasttime - starttime); ball.velocity.y = (lastpositiony - startpositiony) / (lasttime - starttime); break; case motionevent.action_down: ball.x = event.x; ball.y = event.y; ball.velocity.x = 0.0; ball.velocity.y = 0.0; startpositionx = ball.x; startpositiony = ball.y; long starttime = system.currenttimemillis(); break; }
i'm not sure code provided does, move section listener looks bad removed code think looked wrong. moves ball person touched. after you'll have figure out how animate scene. have this:
ball.x = (ball.velocity.x + ball.x) % screensize; ball.y = (ball.velocity.y + ball.y) % screensize;
Comments
Post a Comment