Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

1. ## Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

I am trying to write a code in Phys2d in which an object(BOX) is supposed to be moved from location A to location B with the Velocity of 2 m/s in certain direction let suppose North.

I'm new to this game programming specially physics & I don't know how should i have to set the velocity...I just read in few demos (they wern't even clear )

following is the code but box is not moving, do I need to apply force as well??

... new Box("Truck",10.0f, 10.0f, 100.0f);
Truck.setPosition(262, 184);
Truck.setPaint(Color.blue);

any help will be highly appreciated...

--- Update ---

Sorry after spending some time, I finally figured out that I made a mistake by defining object as static after declaring it as dynamic Code is working fine... the box is moving with certain velocity in upward direction because of (negative) velocity and falls down due to gravity... Still i m wondering how should I move Box from Location (x,y) to Location2(x,y).... Is there any way to have a look of sky view because here things fall from top to bottom?Huh any help??

2. ## Re: Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

You haven't really provided enough information for anybody to help you. I suggest providing an SSCCE that demonstrates exactly the problem- not just a snippet, but not your whole program either.

I see a setPosition() method. Can't you just use that?

3. ## Re: Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

Thanks kevin for your suggestion: Following is not exactly SSCCE b/c the rendering class is not included (b/c of big size, but I can upload that file if required...) but I hope it will give clear idea.
Regarding setPosition() method -I've tried this but this method just teleport the body to specified location. I want that Body start moving (in drag manner) from location one and stops at another location with Specified velocity(this velocity is no problem as i can adjust now). Any Idea???

================================================== ==============
public class Demo14 extends AbstractDemo { //Abstract Demo class deals with the rendering of these bodies.

public Demo14() {
super("Phys2D Demo 14");
}

/**
* @see net.phys2d.raw.test.AbstractDemo#init(net.phys2d.r aw.World)
*/

protected void init(World world) {
world.setGravity(0,0); //

Body Box1 = new Body("Cart", new Box(50.0f, 50.0f), 50);
Box1.setPosition(242.0f, 205);

}

/**
* Entry point for tetsing
*
* @param argv The arguments to the test
*/
public static void main(String[] argv) {
Demo14 demo = new Demo14();
demo.start();
}

4. ## Re: Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

Ah, I see. What you're looking for is called interpolation, and there are a ton of resources on it out there that explain it much better than I could!

5. ## Re: Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

Could you please describe more about interpolation, I googled it and got literally tons of resources but in various other fields...what other keywords i should attach to know more about it in my problem... Confused!!!

Originally Posted by KevinWorkman
Ah, I see. What you're looking for is called interpolation, and there are a ton of resources on it out there that explain it much better than I could!

6. ## Re: Phys2d Velocity Problem... Body Movement from Point A to B with Velocity.

If you google something like "position interpolation" you might get better results. Interpolation is just the transitioning from one state to another state through some middle states. For you, this means transitioning from one position to another position through the positions between them.

Think about it this way: if you want your movement from Point A to Point B to take 10 seconds, after 5 seconds you want your object to be halfway between Point A and Point B (assuming linear speed). The algorithm looks something like this:

```//called before movement starts
Position startPosition = currentPosition;
Position endPosition = whatever you want;
long startTime = System.currentTimeMillis();
long duration = 10*1000; //10 seconds

//called several times a second
update(){
long elapsed = System.currentTimeMillis - startTime;
newPosition = startPosition + (endPosition-startPosition) * (elapsed/duration);
}```