Projecting velocity

Posts that don't fit into other categories.
deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

Projecting velocity

Postby deepinspace » Thu Jun 01, 2017 12:39 pm

I am building a game where the user can choose a direction and velocity to slingshot a Ball (org.dyn4j.dynamics.Body). While the user is choosing the direction/velocity, how would I calculate a line of X length to show the user where the ball will be launched with respect to speed, gravity and other objects that it may collide with?

William
Site Admin
Posts: 349
Joined: Sat Feb 06, 2010 10:23 pm

Re: Projecting velocity

Postby William » Sun Jun 04, 2017 2:26 pm

Just so I'm clear, it sounds like you want to show the projected path of the body.

Is projectile motion what you are looking for? You can use the equations there to build a smoothed path. Bodies may not follow this path exactly due to rounding and the iterative ODE solver.

This isn't all of the problem either. You need to also know the first object that it will collide with on that path. I'm not sure what the best approach there would be. You might be able to use the ConservativeAdvancement class. You'd need to iterate all bodies in the world or you could try to test only the bodies that intersect an expanded version of the path.

William

deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

Re: Projecting velocity

Postby deepinspace » Mon Jun 05, 2017 5:30 pm

Thanks for the information. I will start looking here.

deepinspace
Posts: 8
Joined: Thu Jun 01, 2017 12:35 pm

Re: Projecting velocity

Postby deepinspace » Sat Jun 10, 2017 8:35 am

I appreciate you pointing me in the right direction. I was able to calculate the graph with the following code and I posted it below in hopes that it can help someone else who may be looking to do the same thing. I am still trying to look into projecting collisions and adjust this graph accordingly. If I am able to figure that out as well, I will post an update.

where fx and fy are the initial flick velocity that gets applied to the body.

Code: Select all

body.setLinearVelocity(new Vector2(fx, fy));


Code: Select all

public double getDX(final double fx, final double time)
{
   return fx * time;
}

public double getDY(final double fy, final double time, final double gravity)
{
   return fy * time + (0.5 * gravity * Math.pow(time, 2));
}


I then draw the next 0.5 seconds worth of projectile velocity onto the android canvas:

Code: Select all

for (int i = 0; i < 10; i++)
{
   double t = i * 0.05;
               
   double x = slingshotFlickTracker.getDX(fx, t);
   double y = slingshotFlickTracker.getDY(fy, t, g);
               
   float cx = (float) (x * scale);
   float cy = (float) (y * scale);
               
   canvas.drawCircle(cx, cy, 10f, paint);
}


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests