Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (764) Games in Android Showcase (229) games submitted by our members Games in WIP (852) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Implementing Vector Maths  (Read 1911 times) 0 Members and 1 Guest are viewing this topic.
samcp12

Junior Devvie

Medals: 1

 « Posted 2013-07-09 19:37:02 »

Hello, I recently started learning Vector maths and I'm find it really interesting. But I'm struggling trying to implement them into game programming (2D), specifically Vectors that require a direction and a speed, and how I would translate it into a game. For example, say I wanted to add something like wind or velocity, I would need the speed and the direction it is going, of course speed isn't an issue but what about the direction of the vector, how would I specify something like that within a game?

Sorry If what I'm asking is a bit vague, I'm looking for more for guidance than a page of code (though an example is always nice).
Thanks!
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #1 - Posted 2013-07-09 19:51:21 »

 1 `directionVector.normalize().multiply(speed)`

Gives you a Cartesian vector.
Where directionVector is the direction you want it to go (in x, y).

If you want a polar vector converting to Cartesian, there's some different maths for that.

jonjava
 « Reply #2 - Posted 2013-07-09 21:35:24 »

All a "Vector" essentially is is collection of two values: direction and speed.

Direction and Speed, viewed through another lens is just an X and Y value (coordinates). In other words, a vector is practically the same as a Point.

Now, depending on what you're doing (physics, programming, maths, whatever) you may want to interpret the values in different ways (direction, speed, length, position etc).

Direction can be viewed as just another way of saying how the position (x and y values) should change in relation to each other (not how much they should change, speed says that).

All you need to do is know how to convert a Directional value which is usually defined by an "angle" in degrees or radians. How you do that is a simple dip into the basics of trigonometry. Java like any programming I've ever familiarized myself with have already code made to deal with most basic Maths thingies such as basic trigonometry. (In Java the 'Math' class)

For example, taking a vector with Direction 45 (north-east, 0 degrees pointing conventionally directly to the right) and Speed 100 we can with simple cosine and sine get that the resulting x and y coordinated would be just about x=71, y=71. If we change the Direction to 90 degrees we'd get x = 0 and y = 100.

You might wonder how a simple point can be viewed as a Vector? Quite simple really, the start of the vector is at position (0,0) and the end at the point (x,y). With that information you can get the length (speed), direction, whatever you need.

Redocdam

Senior Devvie

Medals: 18

 « Reply #3 - Posted 2013-07-09 21:46:31 »

As a non-mathy person, I found this to be extremely helpful.

http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

It also has nice pictures, so easily distracted persons like myself don't get lost.
jonjava
 « Reply #4 - Posted 2013-07-09 21:51:29 »

Nice blog post, explains it a lot better than me.

samcp12

Junior Devvie

Medals: 1

 « Reply #5 - Posted 2013-07-10 12:06:45 »

Thanks a lot guys, particularly jonjava, that explanation really helped, and that blog post looks awesome as well.
Pages: [1]
 ignore  |  Print

 EgonOlsen (579 views) 2018-06-10 19:43:48 EgonOlsen (682 views) 2018-06-10 19:43:44 EgonOlsen (471 views) 2018-06-10 19:43:20 DesertCoockie (822 views) 2018-05-13 18:23:11 nelsongames (1025 views) 2018-04-24 18:15:36 nelsongames (1074 views) 2018-04-24 18:14:32 ivj94 (1654 views) 2018-03-24 14:47:39 ivj94 (582 views) 2018-03-24 14:46:31 ivj94 (1483 views) 2018-03-24 14:43:53 Solater (567 views) 2018-03-17 05:04:08
 SkyAphid 17x orange451 16x philfrei 9x Spasi 8x dime26 7x ral0r2 7x beeaware 6x LeoOno 5x Sickan 4x br0ken_m1nd 4x KaiHH 4x nsigma 4x FabulousFellini 4x SHC 3x princec 3x jezek2 3x
 Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46Deployment and Packagingby philfrei2018-08-19 23:53:08Deployment and Packagingby philfrei2018-08-19 23:50:04Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39
 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