Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) 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
 Random Direction  (Read 4938 times) 0 Members and 1 Guest are viewing this topic.
StumpyStrust
 « Posted 2012-03-07 08:47:13 »

How would I find a random direction to send an object flying towards? Say I want to send an object flying towards 56 degrees how can I actually move an object in the x and y plane to that direction? Normally I would just have a x speed and y speed but when i make a random explosion effect I get a square.

1 1 1 1 1
1         1
1   0    1
1         1
1 1 1 1 1

0 is the origin of the explosion.

I want

1
1   1
1   0   1
1   1
1

Can I just find a random number between 360 and send the object in that direction?

theagentd

« JGO Bitwise Duke »

Medals: 443
Projects: 2
Exp: 8 years

 « Reply #1 - Posted 2012-03-07 08:48:53 »

 1  2  3  4  5 `double angle = 10; //The shot angledouble moveSpeed = 10; //The speed of the shotdouble xSpeed = Math.cos(angle) * moveSpeed;double ySpeed = Math.sin(angle) * moveSpeed;`

Myomyomyo.
Danny02
 « Reply #2 - Posted 2012-03-07 10:00:26 »

and also remember in which unit youj have your angles, radient or gradient. The MAth.* functions take radient i gues(?), take a look at the docs.
 Games published by our own members! Check 'em out!
ReBirth
 « Reply #3 - Posted 2012-03-07 12:58:01 »

repeat theagentd's code with diff angle value. for explosion fixed length can work too.

ra4king

JGO Kernel

Medals: 386
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #4 - Posted 2012-03-07 13:00:28 »

Yeah careful all the trig functions in the Math class use radians.

 1  2  3  4  5 `double angle = Math.random() * 2 * Math.PI;double speed = 20;double dX = speed * Math.cos(angle);double dY = speed * Math.sin(angle);`

Roquen
 « Reply #5 - Posted 2012-03-07 13:53:28 »

Repeat to yourself: radians are your friend, degrees are for artist....
theagentd

« JGO Bitwise Duke »

Medals: 443
Projects: 2
Exp: 8 years

 « Reply #6 - Posted 2012-03-07 14:13:11 »

Ugh, too much OpenGL for me... xD

Myomyomyo.
StumpyStrust
 « Reply #7 - Posted 2012-03-07 14:17:09 »

Thank you so much exactly what I was looking for...well I knew I wanted to find the radians of the angle with sin/cos but could not remember what to do with them. How much slower is this compared to just giving and x and y speed? Is the sin/cos that costly? I know you are not doing it on every move update but still.

DiEvAl

Junior Newbie

 « Reply #8 - Posted 2012-03-07 15:30:03 »

Repeat to yourself: radians are your friend, degrees are for artist....
Degrees are for openGL ;-)

Thank you so much exactly what I was looking for...well I knew I wanted to find the radians of the angle with sin/cos but could not remember what to do with them. How much slower is this compared to just giving and x and y speed? Is the sin/cos that costly? I know you are not doing it on every move update but still.
If your computer is over 30 years old or if you have millions of objects, it may be costly. Otherwise you will not notice any slowdown.
ra4king

JGO Kernel

Medals: 386
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #9 - Posted 2012-03-07 16:56:39 »

If you are worried about performance, it is best to use this FastMath class, courtesy of Riven. This uses a lookup table, causing the trig functions to be over 50x faster.

 Games published by our own members! Check 'em out!
StumpyStrust
 « Reply #10 - Posted 2012-03-08 07:37:41 »

This was what I was looking for but I am still getting a square.
Am I doing this wrong?

double speedX = 10*Math.cos(angle);
double speedY = 10*Math.sin(angle);

ra4king

JGO Kernel

Medals: 386
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #11 - Posted 2012-03-08 22:27:15 »

Are you using 2 different angles for cos and sin? That's the only possible way to get a square

StumpyStrust
 « Reply #12 - Posted 2012-03-09 03:19:25 »

No same angle but i forgot the pi....no pun intended. I got it working now and it is looking really freaking good...at least to me it is.

theagentd

« JGO Bitwise Duke »

Medals: 443
Projects: 2
Exp: 8 years

 « Reply #13 - Posted 2012-03-09 08:59:24 »

Nice! =D

Myomyomyo.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 MrMapcom (11 views) 2015-05-23 20:26:16 MrMapcom (18 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (35 views) 2015-05-20 03:42:21 BurntPizza (70 views) 2015-05-10 15:53:18 FrozenShade (55 views) 2015-05-07 09:11:21 TheLopais (218 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (206 views) 2015-05-06 13:33:39 TheLopais (226 views) 2015-05-06 13:32:48
 Spasi 32x BurntPizza 16x Riven 13x ra4king 12x EgonOlsen 11x theagentd 11x DavidBVal 11x Husk 11x KevinWorkman 9x princec 8x scanevaro 8x opiop65 7x KaiHH 7x revers 6x SauronWatchesYou 6x MrPork 5x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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