Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  How do i do vectors  (Read 474 times)
0 Members and 1 Guest are viewing this topic.
Online VIrtueeL
« Posted 2014-06-23 18:26:03 »

how to calculate vectors and how to use them?
do i need to create a class called vectors or?

i know you do something with x and y of an object
and calculate it to get the diraction its moving and stuff
but how

object 1
x
y
dx
dy

now what? how to make the phycics

sy bad english

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline CopyableCougar4
« Reply #1 - Posted 2014-06-23 18:31:17 »

Hi

You use
1  
Math.atan2(dy, dx)
to get an angle of direction, and to get the size of a vector from dy and dx, you do
1  
2  
double angleInRadians = Math.atan2(dy, dx);
double size = Math.sqrt((dx * dx) + (dy * dy));


Hope this helps!

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Online VIrtueeL
« Reply #2 - Posted 2014-06-23 19:00:45 »

so i paint a rectangel at

g.drawrect(x + dx,y + dy,25,25);

and move it with
dy? and dx

what do i do with angelinradians
and size?

EDIT: how to move the rectangle in the diraction as well? with angel

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #3 - Posted 2014-06-23 19:29:36 »

No offense but from what I've seen from you in the forums, it clearly stands out that you don't really have the basics down yet.
Read up on linear algebra and hone your math skills, because honestly we can't answer all the maths questions that would ever come to your mind (also it would be faster if you would just read a good book). With that being said here's an answer to your question:
A vector is just pretty much a point in space (in 2D space it's likely to have 2 components: X and Y, in 3D space it has X, Y and Z).
If you think about vectors as arrows or as a line than you are most likely wrong.
A vector points from A to B, but if we're talking about a single vector than we "imagine" the other one to be at (0, 0[, 0...]).
You can make a vector that points from A to B just by subtracting A from B.

You can subtract two vectors by subtracting all of their matching components one-by-one (sorry if my explanation is not clear but English is not my native language either).
So basically:
1  
2  
3  
Vector3f a = new Vector3f(3f, 5f, 2f);
Vector3f b = new Vector3f(12f, 6f, 5f);
Vector3f aToB = new Vector3f(b.x-a.x, b.y-a.y, b.z-a.z);

LWJGL's vector class also has a subtract method if I'm not mistaken but I was just trying to demonstrate the principle here.

We call difference X = DX, and difference Y = DY, so basically you just have to subtract the A vector's X and Y components from the B vectors X and Y components, resulting in DX and DY accordingly.

You can get the angle between to points using the method that CopyableCougar4 wrote:
1  
double angleInRadians = Math.atan2(dy, dx);


Please note that the method's first parameter is DY and not DX as one would expect.
Also note that this method's return value is not degrees but radians.
If you want to convert radians to degrees you can do the following:
1  
2  
double radians = 0.6;
double angles = radians*180.0/Math.PI;


Size in Cougar's code should be called distance or length, that's the distance between the two vectors or as often referred to, the length of the vectors (btw I think this is wrongly called so, because calculating the distance between two vectors and a single vector is different).
The distance formula is this:
Click to Play


Keep in mind that I'm not a mathematician and there might be something wrong with my explanation or in my code, although I was trying to keep my post error-free.
Hopefully this cleared things up, but as I said in the beginning of my post, you should pick up a book on the subject. Wink

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline CopyableCougar4
« Reply #4 - Posted 2014-06-23 20:13:19 »

Hi

I didn't really thoroughly read this, but I found this page online. It seems to explain some basics of vectors, from dot products to add vectors. Again, I don't know how helpful this page is for a beginner, but here: http://www.mathsisfun.com/algebra/vectors.html Hope this helped Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Online VIrtueeL
« Reply #5 - Posted 2014-06-23 20:41:46 »

do i need to create my own vector class coos i cant do vec.x or vec.y on this
   Vector vec = new Vector(0, 0);

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline CopyableCougar4
« Reply #6 - Posted 2014-06-23 20:42:21 »

LWJGL Util has vector classes for anywhere from 2 to 4 points with some utility methods built in.

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline Gibbo3771
« Reply #7 - Posted 2014-06-24 06:15:45 »

https://www.khanacademy.org/math

The best math resource you will ever need.

do i need to create my own vector class coos i cant do vec.x or vec.y on this
   Vector vec = new Vector(0, 0);

What? X and Y are just fields in the vector class.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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