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 14 of 14

Thread: Moving object in the y-axis

  1. #1
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Moving object in the y-axis

    Hi guys,

    I'm new in this forum and I'm using it to help me in my coding classes.

    I just have one problem with a "car" program in java.
    The objective is to move the car in the direction it is facing.
    Moving forward in the normal position (when the angle is zero) means moving in the x-axis. Choosing to turn the car function (for example: 90º) and then using the forward function should move up or down the car in the y-axis. However the y-axis is not working.
    I will leave here the code in bluej and what teacher wants me to do...


    I hope that someones can teach me how to do this... I'm stuck.



    PS: I don't know if someone in forum already posted this.
    Car2 Demo.rarThe Problem to be solved.JPGmyanswer.JPGMoveforwardfunction.jpg


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Moving object in the y-axis

    Please copy the code you are asking about and paste it here.
    Be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Moving object in the y-axis

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  4. #4
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving object in the y-axis

    Sorry :\
    Here it goes...

     public void moveForward(int dist)
        { 
            // Obtain the current rotation in degrees
            int rotationInDegrees  = carImage.getRotation();
            // Convert the current rotation from degrees to radians
            rotationInDegrees=0;
            double rotationInRadians = 0.0;
            rotationInRadians=Math.toRadians(rotationInDegrees); 
            IO.outputln("The result of radians is: "+rotationInRadians);
     
            // Calculate the distance to move in both x and y directions
            double distX=dist;
            double distY=0.0;
     
            distX=dist*Math.cos(Math.toRadians(rotationInRadians));
            distY=dist*Math.sin(Math.toRadians(rotationInRadians));
              IO.outputln("x= "+distX);
              IO.outputln("y= "+distY);
     
     
     
     
            // Move the car in both x and y directions with the correct distances
            // Notice that setX() and setY() take int as argument
             carImage.setX(carImage.getX() + (int)distX);
     
     
             carImage.setY(carImage.getY() + (int)distY);       
        }
     public void makeTurn(int angle) 
        { 
            // Change the orientation of car from current orientation plus angle
            carImage.setRotation(carImage.getRotation() + angle);
        }

    How I can I move the car in the y-axis after using the "maketurn" function ?


    Thanks

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Moving object in the y-axis

    I'm assuming you have a paintComponent() method somewhere that draws the car starting at some x, y coordinate. To move the car across the drawing area to the right, you increase the x-coordinate and redraw the car. Similarly, to move the car up the drawing area, you'd decrease the y-coordinate and redraw the car. Decreasing x moves the car to the left, increasing y moves the car down. If you increase both x and y in equal proportions, then the car will move in a 45-degree direction towards the bottom right corner.

    You'll want to determine the correct proportions in which to vary x and y according to the direction in which the car is pointed. Do you know basic trig, like sine and cosine?

  6. #6
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving object in the y-axis

    Thanks for your fast reply.

    The program doesn't use the paint component.
    And yes, I know some basic trig, although it was very long time that I studied that.

    And sorry because I forgot to post all the rest of the program, since the teacher only wants me to modify in the moveforward function
    import comp102x.IO;
    import comp102x.Canvas;
    import comp102x.ColorImage;
    public class Car2
    {
     
        String owner = "NoName";
        ColorImage carImage = new ColorImage("Car1.png");
        double gasMileage = 10.0;
        double gasInTank = 10.0;
      public void Car2Demo()
        {
            Canvas canvas = new Canvas();
            canvas.add(carImage,200,200);
        }

    The gasMileage and gasintank is for other functions. (just forget about them )
    Hope it's more clear now.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Moving object in the y-axis

    Where is the value of the object's y location changed?
    Where is the object moved to a new location using that y value?

    Is the changed y value being seen in the code that shows the object in a new location?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Moving object in the y-axis

    And sorry that I didn't read Post #4 very carefully.
    How I can I move the car in the y-axis after using the "maketurn" function ?
    "Functions" in Java are called methods.

    The makeTurn() method sets the car's rotation, then in the moveForward() method, the car's rotation is retrieved and applied to distX and distY which are then used to modify the car's x and y coordinates. What I don't see is where the car image is redrawn using the new coordinates. Either the carImage object should draw itself using the updated coordinates, or there should be a statement that updates the canvas object with the car image's new coordinates. I don't see where that's happening.

  9. #9
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving object in the y-axis

    No matter what I do, the car only moves in the x-axis. It's like it's not recognizing the y-axis coordinates.

    I suppose will be this
    ...
        carImage.setX(carImage.getX() + (int)distX);
             carImage.setY(carImage.getY() + (int)distY); 
    ..
    But it's not working.

    Can I show you by stream the running of car2demo ?

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Moving object in the y-axis

    Can't you post the source code necessary to compile and run the app to witness what is/isn't happening?

  11. #11
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving object in the y-axis

    Ok.

    I will repost again all the code of car2 class (the class that I'm working in)
    import comp102x.IO;
    import comp102x.Canvas;
    import comp102x.ColorImage;
     
    /**
     * Write a description of class Cars here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Car2
    {
     
        String owner = "NoName";
        ColorImage carImage = new ColorImage("Car1.png");
        double gasMileage = 10.0;
        double gasInTank = 10.0;
     
        public Car2 () {}
     
        public Car2(String nameOfOwner) 
        {
            owner = nameOfOwner;
            carImage = new ColorImage();
        }
     
        public Car2 (String nameOfOwner, double newGasMileage) 
        {
            owner = nameOfOwner;
            carImage = new ColorImage( );
            gasMileage = newGasMileage;
        }
     
        public void moveForward(int dist)
        { 
            // Obtain the current rotation in degrees
            int rotationInDegrees  = carImage.getRotation();
            // Convert the current rotation from degrees to radians
            rotationInDegrees=0;
            double rotationInRadians = 0.0;
            rotationInRadians=Math.toRadians(rotationInDegrees); 
            IO.outputln("The result of radians is: "+rotationInRadians);
     
            // Calculate the distance to move in both x and y directions
            double distX=dist;
            double distY=0.0;
     
            distX=dist*Math.cos(Math.toRadians(rotationInRadians));
            distY=dist*Math.sin(Math.toRadians(rotationInRadians));
              IO.outputln("x= "+distX);
              IO.outputln("y= "+distY);
            // Move the car in both x and y directions with the correct distances
            // Notice that setX() and setY() take int as argument
             carImage.setX(carImage.getX() + (int)distX);
             carImage.setY(carImage.getY() + (int)distY);
     
            // Update the amount of gas in tank assumming that each unit of dist is 10m
            double distKm = dist / 100.0;
            double gasUsed = distKm / 100.0 * gasMileage;
            gasInTank = gasInTank - gasUsed;
            IO.outputln("Amount of gas used: " + gasUsed + ", gas remained: " + gasInTank);
        }
     
        public void makeTurn(int angle) 
        { 
            // Change the orientation of car from current orientation plus angle
            carImage.setRotation(carImage.getRotation() + angle);
        }
     
        // addGas adds an amount of gas equal to gasToAdd to gasInTank
         public void addGas(double gasToAdd) {
             //gasInTank = gasInTank + gasUsed;
             gasInTank = gasInTank + gasToAdd;
        }
     
        public void Car2Demo()
        {
            Canvas canvas = new Canvas();
            canvas.add(carImage,200,200);
        }
     
    }

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Moving object in the y-axis

    The answer to my question is, "No, you can't because of the non-standard API on which the program relies and I can't find."

    It's interesting that in the moveForward() method, distX is initialized to dist, but distY is initialized to 0.

    Looking at the math closer:

    rotationInDegrees=0;
    double rotationInRadians = 0.0;
    rotationInRadians=Math.toRadians(rotationInDegrees );

    So rotationInRadians = 0.

    distY=dist*Math.sin(Math.toRadians(rotationInRadia ns));

    I don't understand why rotationInRadians is being converted to radians (again), so that might be wrong, but also, the sin( 0 ) is 0, so distY will always be 0.

    You might try plugging in some appropriate constants for distY to see if that causes the Y-coordinate to change - or just use distX to start with. In other words, change this statement:

    carImage.setY(carImage.getY() + (int)distY);

    to:

    carImage.setY(carImage.getY() + (int)distX);

    Once you've done that and see that successfully changing the y-coordinate causes the image to move in the y direction, then you need to review the math to see what you've done wrong, if anything. (Not sure what you were given and what you've written.)

    Good luck!

  13. #13
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Moving object in the y-axis

    Greg

    I tried this and now works
    I took off the rotationinRadians=0.0; and did other small modifications
    see
     public void moveForward(int dist)
        { 
     
     
            // Calculate the distance to move in both x and y directions
           double rotationInRadians=Math.toRadians(carImage.getRotation());
            double distX=dist*Math.cos(rotationInRadians);
            double distY=dist*Math.sin(rotationInRadians);
              IO.outputln("x= "+distX);
              IO.outputln("y= "+distY);
            // Move the car in both x and y directions with the correct distances
            // Notice that setX() and setY() take int as argument
             carImage.setX(carImage.getX() + (int)distX);
             carImage.setY(carImage.getY() + (int)distY); 
            // Update the amount of gas in tank assumming that each unit of dist is 10m
            double distKm = dist / 100.0;
            double gasUsed = distKm / 100.0 * gasMileage;
            gasInTank = gasInTank - gasUsed;
            IO.outputln("Amount of gas used: " + gasUsed + ", gas remained: " + gasInTank);
        }

    Anyone can use this code if needed.
    Thanks

  14. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Moving object in the y-axis

    Glad you figured it out.

Similar Threads

  1. Help with moving an object in Java?
    By vishvanaar in forum Java Theory & Questions
    Replies: 1
    Last Post: February 21st, 2014, 09:43 AM
  2. [SOLVED] help with java game source code..
    By hemla in forum Object Oriented Programming
    Replies: 7
    Last Post: March 7th, 2013, 06:48 PM
  3. How To Track A Moving Object
    By mahdi.nikoo in forum Algorithms & Recursion
    Replies: 7
    Last Post: November 2nd, 2012, 01:03 PM
  4. Replies: 0
    Last Post: February 26th, 2012, 12:57 PM
  5. Replies: 3
    Last Post: November 10th, 2011, 07:11 AM