Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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 4267 times)
0 Members and 1 Guest are viewing this topic.
Offline h3ckboy
« Posted 2009-02-27 19: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.
Offline pjt33
« Reply #1 - Posted 2009-02-27 19:59:12 »

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

Junior Member





« Reply #2 - Posted 2009-02-27 20: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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


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

I think he want's sourcecode persecutioncomplex


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
Offline h3ckboy
« Reply #4 - Posted 2009-02-27 22: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.
Offline pjt33
« Reply #5 - Posted 2009-02-28 02: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?
Offline Wildern

Junior Member





« Reply #6 - Posted 2009-02-28 02: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.
Offline h3ckboy
« Reply #7 - Posted 2009-02-28 08:34:14 »

pjt33 is right.

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


for example: 2/6 = 1/3.
Offline h3ckboy
« Reply #8 - Posted 2009-02-28 09: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.
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #9 - Posted 2009-02-28 09: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!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy
« Reply #10 - Posted 2009-02-28 11: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?
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #11 - Posted 2009-02-28 11: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
Offline h3ckboy
« Reply #12 - Posted 2009-02-28 11:50:06 »

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

Senior Member


Projects: 3


too offending?


« Reply #13 - Posted 2009-02-28 11: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
Offline h3ckboy
« Reply #14 - Posted 2009-02-28 11:57:22 »

yeah taht is wha tI jsut changed it to Smiley.

I still get possible loss of precission.
Offline cylab

JGO Knight


Medals: 34



« Reply #15 - Posted 2009-02-28 13: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  Wink) 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!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2009-02-28 13: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
Offline h3ckboy
« Reply #17 - Posted 2009-02-28 14: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  Grin.
Offline cylab

JGO Knight


Medals: 34



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

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

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 Wink

Mathias - I Know What [you] Did Last Summer!
Offline h3ckboy
« Reply #19 - Posted 2009-02-28 14:16:51 »

I skimmed Smiley.

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.
Offline cylab

JGO Knight


Medals: 34



« Reply #20 - Posted 2009-02-28 14: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 Lips Sealed)

Mathias - I Know What [you] Did Last Summer!
Offline h3ckboy
« Reply #21 - Posted 2009-02-28 15:12:00 »

thx for the advice.

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

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

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #22 - Posted 2009-02-28 15: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
Offline h3ckboy
« Reply #23 - Posted 2009-02-28 15:22:56 »

I didnt say I skipped stuff. I said I took like 2 days on each little topic.
Offline h3ckboy
« Reply #24 - Posted 2009-02-28 16: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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #25 - Posted 2009-02-28 16: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
Offline h3ckboy
« Reply #26 - Posted 2009-02-28 17:00:29 »

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

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #27 - Posted 2009-02-28 17: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
Offline h3ckboy
« Reply #28 - Posted 2009-02-28 17: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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #29 - Posted 2009-03-01 02: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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (23 views)
2014-04-15 03:46:01

UprightPath (38 views)
2014-04-14 17:39:50

UprightPath (20 views)
2014-04-14 17:35:47

Porlus (36 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!