Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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 2762 times)
0 Members and 1 Guest are viewing this topic.
Offline StumpyStrust
« Posted 2012-03-07 09: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?

Offline theagentd
« Reply #1 - Posted 2012-03-07 09:48:53 »

1  
2  
3  
4  
5  
double angle = 10; //The shot angle
double moveSpeed = 10; //The speed of the shot

double xSpeed = Math.cos(angle) * moveSpeed;
double ySpeed = Math.sin(angle) * moveSpeed;

Myomyomyo.
Offline Danny02

JGO Knight


Medals: 36



« Reply #2 - Posted 2012-03-07 11: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!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-03-07 13:58:01 »

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

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #4 - Posted 2012-03-07 14: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);

Offline Roquen

JGO Ninja


Medals: 66



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

Repeat to yourself: radians are your friend, degrees are for artist....
Offline theagentd
« Reply #6 - Posted 2012-03-07 15:13:11 »

Ugh, too much OpenGL for me... xD

Myomyomyo.
Offline StumpyStrust
« Reply #7 - Posted 2012-03-07 15: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.
Offline DiEvAl

Junior Newbie





« Reply #8 - Posted 2012-03-07 16: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.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #9 - Posted 2012-03-07 17: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!
Try the Free Demo of Droid Assault
Offline StumpyStrust
« Reply #10 - Posted 2012-03-08 08:37:41 »

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


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

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


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

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

Offline StumpyStrust
« Reply #12 - Posted 2012-03-09 04: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.


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

Nice! =D

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (32 views)
2013-05-17 21:29:12

alaslipknot (41 views)
2013-05-16 21:24:48

gouessej (71 views)
2013-05-16 00:53:38

gouessej (70 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (73 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (137 views)
2013-05-12 17:10:36

kutucuk (137 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.134 seconds with 20 queries.