Results 1 to 5 of 5
- 12-27-2008, 10:01 PM #1
[SOLVED] Using Vectors to Control Movement
Hello all!
I am trying to create a program that accepts an angle and a speed from the user and then shoots a ball at that angle and at that speed, slowing down as it goes. I have a program that does close to that, but my current method is not accurate at all. The movement of the ball is determined by my "slope calculator". This is a separate class that creates a triangle from the ball's X position, Y position, and launch angle. It then finds the slope of the hypotenuse and moves the ball up and over accordingly. After a certain number of loops, the values are decremented, causing the ball to slow down and eventually stop. This is not realistic at all, however, and adding more balls to the table would be incredibly difficult.
My programming teacher told me to use vectors instead. By using vectors, I could create a vector representing friction to slow the ball down, and I could also add other balls to the screen to have them interact. This would be done through vector addition. He said vectors would make the program more accurate and more versatile.
MY PROBLEM: I have no idea how to implement vectors in my code. Is there a Java class that is specifically made for vectors? Is there perhaps another way to approach this problem?
Any help is greatly appreciated.
Cheers
- 12-28-2008, 12:01 AM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
If you plan to make your program more complicated, then yes using vector maths will be more versatile, although in principle you can also do it by calculating in terms of separate components as you're trying to do -- you just need to do it right!
The standard Java3D framework includes classes for manipulating vectors. Otherwise, I'm sure if you Google, you'll find a squillion other packages.Neil Coffey
Javamex - Java tutorials and performance info
- 12-28-2008, 12:24 AM #3
There is a Vector class in Java, but it is not what you want. Not at all. The Java Vector class is a dymanic array.
What your teacher wants you to use is polar processing, where you deal with angles and speed. The polar processing can be 2D or 3D, (even 4D if you want) depending on your domain.
In 2D, a vector is just an angle and a magnitude. Its easy to implement.
- 12-28-2008, 07:25 AM #4
Here's an idea.
Or you might try something like this:Java Code:public class YourViewComponent extends JPanel { Vector2 v; double friction = 0.025; public YourViewComponent() { v = new Vector2(10.0, 300.0, -Math.PI/4, 10.0); } protected void paintComponent(Graphics g) { v.draw(g); } inside your animation cycle { v.step(friction); repaint(); pause } public static void main(String[] args) { show this component start animation cycle } } class Vector2 { double x; double y; double theta; double speed; public Vector2(double x, double y, double theta, double speed) { this.x = x; this.y = y; this.theta = theta; this.speed = speed; } public void step(double friction) { move ahead in theta direction adjust speed with friction } public void draw(Graphics g) { draw this into the graphics context } }
which might be more in line with what your programming teacher had in mind.Java Code:public class YourViewComponent extends JPanel { Ball ball; Vector2 friction; double coefOfFriction = -0.025; public YourViewComponent() { ball = new Ball(); v = new Vector2(10.0, 300.0, -Math.PI/4, 10.0); friction = new Vector2(0, 0, 0, coefOfFriction); } protected void paintComponent(Graphics g) { ball.draw(g); } inside your animation cycle { ball.step(friction); repaint(); pause } } class Vector2 { double x; double y; double theta; double speed; public Vector2(double x, double y, double theta, double speed) { this.x = x; this.y = y; this.theta = theta; this.speed = speed; } public void add(Vector2 v) { this.x += v.x; this.y += v.y; this.theta += v.theta; this.speed += v.speed; } } class Ball Vector2 v; public Ball(Vector2 v) { this.v = v; } public void step(Vector2 fv) { adjust v speed with friction: v.add(fv); move v (x,y) ahead in theta direction } public void draw(Graphics g) { draw this into the graphics context } }
- 12-29-2008, 02:14 AM #5
Similar Threads
-
Snake game movement
By BeerMonkey in forum New To JavaReplies: 9Last Post: 11-27-2008, 12:48 PM -
Detecting user movement of a JFrame
By dklett in forum AWT / SwingReplies: 4Last Post: 08-27-2008, 07:01 AM -
control app width based on certain control
By thebillybobjr in forum SWT / JFaceReplies: 0Last Post: 05-15-2008, 04:52 PM -
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks