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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    Truck.adjustVelocity(new Vector2f(0f, -35f));
    World.add(Truck);

    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. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    Box1.adjustVelocity(new Vector2f(0f, -35f));
    world.add(Box1);

    }

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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!!!

    Quote Originally Posted by KevinWorkman View Post
    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. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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);
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Hello every body
    By skipper90 in forum Member Introductions
    Replies: 1
    Last Post: April 8th, 2013, 01:30 AM
  2. Replies: 22
    Last Post: April 6th, 2013, 03:11 PM
  3. How to control gain between mouse movement and cursor movement ?
    By DrPete in forum Java Theory & Questions
    Replies: 3
    Last Post: March 12th, 2012, 07:27 AM
  4. hi every body!!!!!!!!!........
    By kranthi in forum Member Introductions
    Replies: 2
    Last Post: January 10th, 2012, 06:03 AM
  5. Hi Every Body!
    By Xcube in forum Member Introductions
    Replies: 1
    Last Post: March 12th, 2011, 05:27 PM

Tags for this Thread