Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (168) games submitted by our members Games in WIP (646) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1] 2
 ignore  |  Print
 Simplifying fractions  (Read 6098 times) 0 Members and 1 Guest are viewing this topic.
h3ckboy

JGO Coder

Medals: 5

 « Posted 2009-02-27 18:22:04 »

It is not a traditional fraction. It is two seperate numbers.

I use a formula to get the slope. I want to simplify the slope, so that it does not jump by like 100 pixels.

yeah I have googled it.

thx in advance.
pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #1 - Posted 2009-02-27 18:59:12 »

Do you mean actually simplifying fractions (divide numerator and denominator by their GCD) or rounding?
Wildern

Junior Devvie

 « Reply #2 - Posted 2009-02-27 19:17:39 »

I believe what you want to do is normalize to the unit vector which is a bit different than simplifying a fraction.
http://en.wikipedia.org/wiki/Unit_vector
 Games published by our own members! Check 'em out!
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #3 - Posted 2009-02-27 20:00:05 »

I think he want's sourcecode

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42 `float speed = veryFast * (float)Math.PI;Vec2 source = new Vec2(13, 14);Vec2 target = new Vec2(15, 15);Vec2 direction = target.sub(source);Vec2 normal = direction.normalize();Vec2 velocity = normal.mul(speed);source = source.add(velocity);class Vec2{   public float x, y;   public Vec2(float x, float y)   {      this.x = x;      this.y = y;   }   public Vec2 mul(float v)   {      return new Vec2(x*v, y*v);   }   public Vec2 add(Vec2 v)   {      return new Vec2(x+v.x, y+v.y);   }   public Vec2 sub(Vec2 v)   {      return new Vec2(x-v.x, y-v.y);   }   public Vec2 normalize()   {       float len2 = x*x + y*y;       float inv = 1.0f / Math.sqrt(len2);       return new Vec2(x * inv, y * inv);   }}`

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
h3ckboy

JGO Coder

Medals: 5

 « Reply #4 - Posted 2009-02-27 21:29:03 »

what all does that source code do? cause it has vecotrs n it. and I think that it does more than jsut simplifying.

I dont need ot know how to get a vector for simplyfying. I am pretty sure of that. I already hvae the slope. that is done. I jsut want to simplify it.
pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #5 - Posted 2009-02-28 01:04:30 »

I jsut want to simplify it.
I'm still waiting for you to define "simplify". So far there have been three guesses as to what you want: are any of them correct?
Wildern

Junior Devvie

 « Reply #6 - Posted 2009-02-28 01:23:28 »

what all does that source code do? cause it has vecotrs n it. and I think that it does more than jsut simplifying.

I dont need ot know how to get a vector for simplyfying. I am pretty sure of that. I already hvae the slope. that is done. I jsut want to simplify it.

That source code provides a class for handling your vector operations... it supports adding, subtracting, multiplying and normalizing.
A useful class when working with vectors.
h3ckboy

JGO Coder

Medals: 5

 « Reply #7 - Posted 2009-02-28 07:34:14 »

pjt33 is right.

I mean reagular like 3rd grade math simplifying of fractions.

for example: 2/6 = 1/3.
h3ckboy

JGO Coder

Medals: 5

 « Reply #8 - Posted 2009-02-28 08:20:25 »

I think I have found a way to do it. but hte numbers sometimes still come out big. like sometimes it tis 5, another 80.
Hansdampf

Senior Devvie

Projects: 3

too offending?

 « Reply #9 - Posted 2009-02-28 08:37:09 »

You don't want to simplify it. What you want is to NORMALIZE it. You are wasting a lot of time. Riven posted all code you need.

edit: btw, you have your slope. Put it as 'y' in the code, 'x' is 1.

lots of sillystupid games: http://www.emaggame.com
 Games published by our own members! Check 'em out!
h3ckboy

JGO Coder

Medals: 5

 « Reply #10 - Posted 2009-02-28 10:37:03 »

I get "possible loss of procission"

is it ok that x and y for me are integers?

I turned normalize into a void and [laced it inside my enemy class is that ok?
Hansdampf

Senior Devvie

Projects: 3

too offending?

 « Reply #11 - Posted 2009-02-28 10:42:04 »

is it ok that x and y for me are integers?
No. Cast them to double or float.

lots of sillystupid games: http://www.emaggame.com
h3ckboy

JGO Coder

Medals: 5

 « Reply #12 - Posted 2009-02-28 10:50:06 »

I dunno what oyu mean by "cast" but I will changet ehm to floats
Hansdampf

Senior Devvie

Projects: 3

too offending?

 « Reply #13 - Posted 2009-02-28 10:55:10 »

ok, that was unclear.
do all your calculations with floats or doubles, only when you draw them to the screen you might need to cast them (temporarily) to integer if your display is discrete (pixels).

lots of sillystupid games: http://www.emaggame.com
h3ckboy

JGO Coder

Medals: 5

 « Reply #14 - Posted 2009-02-28 10:57:22 »

yeah taht is wha tI jsut changed it to .

I still get possible loss of precission.
cylab

JGO Wizard

Medals: 85

 « Reply #15 - Posted 2009-02-28 12:35:27 »

 1 `int myIntX = (int)myFloatX; `

No offence, but you still need to learn a lot of the basics. Please try to find some decent basic java primer or book in your library and _understand_ it's contents. It would be easier for you and us.

And you should really learn and understand (2D) vector math - so many things get so much easier with vector math instead of dealing with slope functions etc.

Rivens code does (despite an irritating PI value  ) all you are trying to do and you didn't even noticed that, because you didn't try to understand the code (or weren't capable because of missing basics)...

Mathias - I Know What [you] Did Last Summer!
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #16 - Posted 2009-02-28 12:36:31 »

Seriously, you need to spend some time with a good Java book. Not a tutorial, a book.

You might think you know what you're doing in Java, as you have programmed a few tiny projects, but well, the more questions you post here, the more it becomes clear that you lack the basic knowlegde of how to program. You can't expect to get anywhere without.

Don't get me wrong, I'm just saying that a little investment in a book, and spending a lot of time on it, will in the end make you productive and able to get things done without needing help all the time.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
h3ckboy

JGO Coder

Medals: 5

 « Reply #17 - Posted 2009-02-28 13:08:32 »

I guess I have a lot to learn, I was jsut trying to figure it out on my own. casue I dont get stuff when I learn it from some1 else. cause if someone shows me a formula Idont get it. If I play with it for a while I will get teh idea.

yes I have read a BOOK(wow) before. I was learning it rivens way.

P.S: it is called the Newlass Clubies for a reason  .
cylab

JGO Wizard

Medals: 85

 « Reply #18 - Posted 2009-02-28 13:12:55 »

P.S: it is called the Newless Clubies for a reason  .

It's a place where more experienced programmers give advice to the new ones. So we give you the advice to read a good java book. If you already read one, I suspect it was a bad one

Mathias - I Know What [you] Did Last Summer!
h3ckboy

JGO Coder

Medals: 5

 « Reply #19 - Posted 2009-02-28 13:16:51 »

I skimmed .

reading takes too long. my problem when I was reading it was, taht whenever I learned a new thing I thouhgt of a coo thing to do tiwh it. so ther went a day or two into that then I moved ot the nect thing. so it took a while. I believe my book was java for dummies. I apparently and stil a dummy.
cylab

JGO Wizard

Medals: 85

 « Reply #20 - Posted 2009-02-28 13:22:37 »

I guess I have a lot to learn, I was jsut trying to figure it out on my own. casue I dont get stuff when I learn it from some1 else. cause if someone shows me a formula Idont get it. If I play with it for a while I will get teh idea.

reading takes too long. my problem when I was reading it was, taht whenever I learned a new thing I thouhgt of a coo thing to do tiwh it. so ther went a day or two into that then I moved ot the nect thing. so it took a while. I believe my book was java for dummies. I apparently and stil a dummy.

It seems that you have a lot of energy, which is Good(tm), but you also seem to be sloppy, impatient and hurried - which is standing in your way. That might be the reason you don't get things from abstract explanations. Take a step back and relax. Try to analyse formulars/given code step by step and try to understand the single steps. Don't get impacient if you don't get it right away (nobody does). Just take your time (or get your hands on some Ritalin )

Mathias - I Know What [you] Did Last Summer!
h3ckboy

JGO Coder

Medals: 5

 « Reply #21 - Posted 2009-02-28 14:12:00 »

thx for the advice.

I will take hte advice and try and figure out the source code from riven .

will post back wiht my analysis.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #22 - Posted 2009-02-28 14:19:41 »

I'd actually advise against analyzing my sourcecode.

Read that darn book first! If something is not clear, do no skip the page, ever! Because the learning curve is only UP, and every following page assumes you grasped the previous.

It's all about discipline.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
h3ckboy

JGO Coder

Medals: 5

 « Reply #23 - Posted 2009-02-28 14:22:56 »

I didnt say I skipped stuff. I said I took like 2 days on each little topic.
h3ckboy

JGO Coder

Medals: 5

 « Reply #24 - Posted 2009-02-28 15:00:06 »

Ok I skimmed through it to see wha tI had read before. I dont think that I had finished it.

I have covered all that the book has to offer though, through my scowering of th internet.

haha I just learned something. It will acutally fix a prolem Ihad in a game. I didnt konw how to make something gloabal to all instances. static variables. taht saved me tons of hassle.

thx for the badgering on my crappy skills, you saved me a lot of work.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #25 - Posted 2009-02-28 15:32:54 »

Don't skim too. And if you think you absorbed everything in the book, you might want to reread it, or buy a better book.

Besides that, the static keyword is easily abused.

If you design things properly, you might only use static in the main method, and the slingleton pattern. Steer clear of static as much as possible, it'll bite you in the long run.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
h3ckboy

JGO Coder

Medals: 5

 « Reply #26 - Posted 2009-02-28 16:00:29 »

is it ok if I make my score, level, and shield static?
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #27 - Posted 2009-02-28 16:08:40 »

What if you introduce more 'units' with shields...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
h3ckboy

JGO Coder

Medals: 5

 « Reply #28 - Posted 2009-02-28 16:45:09 »

my game is at the end of its development. and If I add more units it will still work. cause the shield I am referring to is the player shield.

check out ym game: http://www.java-gaming.org/topics/space-invaders-again-wait-it-is-different/19826/view.html

This may enlighten you on what I mean.

Oh eyah taht version does not have the static variables. cause I am trying to make levels. An older version of the game iwch was made with java 2d jsut re-initiated the game. but it had the gameloop so I could control it better. so I am going ot init the state (or game I am not sure wich it is doing I know it gets to the right place). The init(container) works when I am going to the menu. but it doesnt work when I am going to the next level.
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #29 - Posted 2009-03-01 01:42:20 »

This has already been explained to you, but typically when you want to handle movement you want to normalize your movement vector, which in the end produces a vector of length 1. That means, in the end, that it has direction only, but no magnitude.

You can think of it like a 1 dimensional vector. The only directions it can have are left or right, which would be represented by 1 and -1. Then no matter what you multiply by it, you end up getting the same direction, but it has the length of the multiple. Like multiplying it by 100 creates a vector going either left or right at that distance of 100. Dividing by 100 makes something going left or right that goes only 1/100 distance.

Typically you want whatever you're moving to have a speed which is a one dimensional decimal value. Then you get the direction it should move by getting a normalized vector, and you multiply that direction by the speed.

See my work:
OTC Software
Pages: [1] 2
 ignore  |  Print

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

 deepthought (48 views) 2015-06-30 15:39:44 deepthought (55 views) 2015-06-30 15:39:09 deepthought (65 views) 2015-06-30 15:36:52 Za\'Anzabar (42 views) 2015-06-29 05:44:54 TritonDreyja (49 views) 2015-06-24 17:10:40 CopyableCougar4 (48 views) 2015-06-23 00:34:45 BurntPizza (55 views) 2015-06-21 20:36:46 cookiecompiler (94 views) 2015-06-11 15:42:53 cookiecompiler (56 views) 2015-06-11 15:41:14 NegativeZero (81 views) 2015-06-11 09:49:18
 princec 29x wessles 23x BurntPizza 19x CopyableCougar4 19x opiop65 17x ags1 15x nsigma 15x orangepascal 14x EgonOlsen 14x Riven 13x KaiHH 12x Jesse 11x SauronWatchesYou 11x theagentd 11x KevinWorkman 11x Archive 11x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List 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:14
 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