Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Physics of a projectile  (Read 853 times)
0 Members and 1 Guest are viewing this topic.
Offline herah

Senior Newbie





« Posted 2013-02-18 20:09:31 »

Hey everyone,
This is my first game I have ever attempted and am running into road blocks left and right. At this point in the program I have a rock that is thrown from the origin towards x,y coordinates of a left mouse click. I want to have this rock thrown in a fairly realistic parabola using the magnitude of the throw (given by distance of click from origin), and angle of throw from the origin.

At this point I have written and rewritten the move method many times and simply can't get it to work. I understand the physics and the math of it, but not how to translate them into java. I've looked at a lot of examples but can't figure out how to apply it. Most of them talk about using time, but I am not sure how to apply it. Really any help will make a huge difference for me.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   

   private double velocity = (Math.sqrt(Math.pow((launchX-newX), 2) + (Math.pow((launchY-newY),2))))/150; // c^2 = sqrt(a^2 + b^2)
  private double gravity = 1;  
   private int launchX; // mouse input
  private int launchY; // mouse input
  private double initialX; // origin
  private double initialY; // origin

   synchronized public void move(){
      //currentTime = System.nanoTime() - startTime;
           if(newY < upperMax){
               active = false;
               }
               theta = (float) Math.toDegrees(Math.atan2(launchX - initialX, launchX - initialY));
                newX = (int)(newX + ((moveSpeed * velocity)));
                newY = (int)(newY + ((-velocity + (gravity * velocity)) * -Math.cos(theta)));
               
         } // end move
Offline actual

JGO Coder


Medals: 23



« Reply #1 - Posted 2013-02-18 20:55:57 »

I think part of the issue is that you need two bits of code.  The first sets up the initial x and y velocities and is only called once when the player "fires". The second piece of code needs to be run at every time step and updates the positions (and y velocity) as time passes.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
// When launching...
// NOTE I am not sure if it's cos, sin, or tan....the important thing is the initial value ONLY depends on the velocity, not gravity.
// I added const.  You probably want some constant factor to multiply by because raw pixel difference may not give you good results.
xVelocity = const * velocity * Math.cos(theta);
yVelocity = const * velocity * Math.sin(theta);


// Each update step.
// delta is the amount of time since the last frame.
public void update(float delta) {

   yVelocity -= gravity*delta;  // Update yvelocity due to gravity

   newY += yVelocity * delta;  // update y position.

   newX += xVelocity * delta;  // assuming no wind resistance, xVelocity is constant throughout.

   if (newY <=0) {
      // We have hit the ground.
  }
}


Also, a small thing, but if you are just squaring a number, it is usually better not to use Math.power(). It's slower (doesn't really matter in many cases) and (at least to my eyes) harder to read.

1  
velocity = Math.sqrt( (launchX - newX) * (launchX - newX) + (launchY - newY) * (launchY - newY));



Offline herah

Senior Newbie





« Reply #2 - Posted 2013-02-18 21:46:27 »

Thank you. Unfortunately now the projectile goes straight out of the guy then shoots upwards off the screen. Only difference is speed of the projectile when clicking elsewhere. I flipped the y for -+ and this lets it fall downwards instead of upwards, but does not start the projectile in an upwards motion. I suppose I don't understand how to change my values to more appropriate numbers.

1  
2  
3  
      xVelocity = (int)(2 * velocity * Math.cos(theta));
      yVelocity = (int)(2 * velocity * Math.sin(theta));
     

xVelocity is giving me readouts in the teens, while yVelocity is giving me numbers in the hundreds.

This is what I changed my move to. I have no idea how to get the delta you referred to; currTimeMillis - start time gave me awful results.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   synchronized public void move(){
      System.out.println("yvel " + yVelocity);
      System.out.println("xvel " + xVelocity);
            if(newY < upperMax){
               active = false;
               }
               theta = (float) Math.toDegrees(Math.atan2(launchX - initialX, launchX - initialY));

                  yVelocity += gravity*2;  // Update yvelocity due to gravity

                  newY -= yVelocity;  // update y position.

                  newX += xVelocity;  // assuming no wind resistance, xVelocity is constant throughout.

         } // end move

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline actual

JGO Coder


Medals: 23



« Reply #3 - Posted 2013-02-19 00:05:05 »

Some thoughts:

If the newX and newY are direct positions on the screen, then the problem is probably that the X,Y origin is at the top left corner of the screen. If that it is the case, then you'll want to negate the yVelocity when you set it.

In terms of time, the best thing to do is to search for "Game loop" here. There are a number of approaches.

You'll want to keep your xVelocity, yVelocity, newX, and newY as floats and then only convert to ints when you draw them. The reason for this is that depending on how short the frames are, you may only move a fraction of a pixel, which will round down to 0 and it will appear not to move.

Offline Slyth2727
« Reply #4 - Posted 2013-02-20 01:53:18 »

Well to make things a bit easier, you could use a Vector class such as this for the velocity and such. More of a mathematical approach you could say:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
public class Vector2d {

   public double x;
   public double y;

   public Vector2d(double num1, double num2){
      this.x = num1;
      this.y = num2;
   }

   public void add(double num){
      x += num;
      y += num;
   }

   public void sub(double num){
      x -= num;
      y -= num;
   }

   public void div(double num){
      x /= num;
      y /= num;
   }

   public void mult(double num){
      x *= num;
      y *= num;
   }

   public void add(Vector2d other){
      x += other.x;
      y += other.y;
   }

   public void sub(Vector2d other){
      x -= other.x;
      y -= other.y;
   }

   public void div(Vector2d other){
      x /= other.x;
      y /= other.y;
   }

   public void mult(Vector2d other){
      x *= other.x;
      y *= other.y;
   }
}

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!