Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  What's more expensive, a lot of if statements or pi?  (Read 1980 times)
0 Members and 2 Guests are viewing this topic.
Offline ImTroyMiller
« Posted 2014-05-25 08:45:57 »

What is more expensive, 500 nested if statements being constantly check in the game loop, or pi being constantly calculated in the game loop?

At least the JVM will only check the first condition of the if-statement and ignore the rest if it is false.

So what's more expensive?
Offline Roquen
« Reply #1 - Posted 2014-05-25 08:47:18 »

Random branches are always expensive.  Pi is a constant.  I think you need to provide more details about what you think you want to do.
Offline ImTroyMiller
« Reply #2 - Posted 2014-05-25 09:20:14 »

Well, I want to make an on screen controller, kind of like a joystick, but one that doesn't return to the center and that doesn't stay pressed.  Basically a half circle, the top radius would be for full speed forward, and the position would be for turning left or right(not turn would be in the center).  One that can be touched anywhere and not moved from the center to a point.

I know I could make a lot(like 500) if statements for each little square area within the half circle controller area, and then set the movements based on that.  I could also just use pi to calculate exactly where and how far from the radius the player is touching.  Which ever is chosen, it'll be constantly looped if the area of the controller is touched.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2014-05-25 09:21:56 »

The answer is quite obvious.. Some little math calculation is far cheaper than 500 if statement.
Offline LiquidNitrogen
« Reply #4 - Posted 2014-05-25 09:24:54 »

you will probably find it easiest to get your controller working well if you calculate a distance from the center and an angle. then you can tweak the values until it feels right.
Offline ImTroyMiller
« Reply #5 - Posted 2014-05-25 09:25:37 »

I was thinking that I could split the if statements up.

if x < 50 then...

if x > 50 then...

So every if statement wouldn't actually be ran through.  I could nest it nicely.

Still more expensive?
Offline LiquidNitrogen
« Reply #6 - Posted 2014-05-25 09:27:47 »

its probably best to do it in a way which used 5 lines of code rather than 500
Offline trollwarrior1
« Reply #7 - Posted 2014-05-25 09:40:48 »

You could just calculate the rotation between the center point and the point that is pressed on the controller and also the distance. The distance would be the power the stick is bent, and the rotation would be by how much the stick is rotated.



In this drawing, black circle is the rotation stick pad. Red dot inside of black dot is the center of the pad. Green line going up is the line from which rotation is calculated. Red dot is a point where user is tapping. Purple thingy is the rotation from the green line to the tap point. Yellow line is the distance from center to tap point.
Online BurntPizza
« Reply #8 - Posted 2014-05-25 09:41:29 »

Wait, you just want to know distance and angle of the joystick from the center point?

1  
2  
3  
4  
5  
double dx = x - center.x;
double dy = y - center.y;

double angle = Math.atan2(dy, dx);
double dist = Math.sqrt(dx * dx + dy * dy);


I use that exact code in this color picker UI to calculate the color being selected by the wheel widget:


Seems applicable to your problem description.

I don't see why you would ever use anything but some simple math for this kind of stuff.
Offline ImTroyMiller
« Reply #9 - Posted 2014-05-25 09:47:55 »

The reason I'm asking is because this is for mobile devices.

I've heard the pi is expensive and should be avoided.  So, if I split up some if statements, after maybe 9 or 10, I would have a result.  Over and over again.

So, in other words, when it comes to using a mobile phones CPU, is checking through 9 to 10 basic <> if-statements, more expensive than using pi once?

Although, I'm pretty sure you all are saying that it is.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #10 - Posted 2014-05-25 09:50:19 »

What do you mean PI is expensive? Its like any other constant o-o In fact, constant are probably one of the cheaper things..
Online BurntPizza
« Reply #11 - Posted 2014-05-25 09:51:12 »

Both options are computationally inconsequential, when performed at the (relatively low) frequency you are talking about.
Don't worry about it, and go with the sane route. It's also the one that isn't a horrible approximation.

EDIT: also, to attempt to clear up any misconception, "pi is expensive" argument is non-existent as PI is stored as a constant value as Math.PI. It's not calculated every time you use it, in fact it's never calculated, it's hardcoded in the source of Math.java
Offline NegativeZero

JGO Knight


Medals: 30
Projects: 2
Exp: 3 years


Zero but not.


« Reply #12 - Posted 2014-05-25 09:56:45 »

π is expensive if you where to calculate it to the 10256th decimal place every single frame, but you'd be fine if you just had

1  
final float PI = 3.141593;


Doing calculations with a float like that is going to be fine on any device, and you'll find that if you want to optimize you will want to be looking in other places for now.

However doing some 500 nested if statements will certainly cause noticeable lag on some devices.

EDIT:

The stuff I've heard against using π is in relation to Math.PI which uses an extremely precise number (16 decimal places), but I think even then it'd just pedantic.
Offline ImTroyMiller
« Reply #13 - Posted 2014-05-25 10:24:25 »

Oops, I've been meaning to say SquareRoot instead of pi.  I don't know why I've been saying pi, lol.

Is Math.sqrt more expensive than a bunch of if statements?
Online Drenius
« Reply #14 - Posted 2014-05-25 10:54:03 »

No direct answer, but if you only do it so few times per frame, I would not really think about it, because even saying it is more expensive, it is still less expensive than a millisecond.

Use what is easier to use, so probably sqrt.
Offline StumpyStrust
« Reply #15 - Posted 2014-05-25 11:00:21 »

Worrying about performance now when nothing works is like seeing how quick you can get nowhere.

500 nested ifs...most things will be able to handle much much more than this.

A constant...about as fast as you can get.

Sqr()...actually very fast in java. You could have 50k calculations a frame and still be fine even on mobile.

The better answer is what is easiest to write and understand.

Offline Roquen
« Reply #16 - Posted 2014-05-25 11:19:42 »

To put numbers on it a square root is roughly 34 cycles (arch dependent).  Move 32-bits from L1 cache into a register is 5 cycles (again arch dependent).  Sqrt is fast (relatively).
Offline ImTroyMiller
« Reply #17 - Posted 2014-05-25 12:18:13 »

Alright, got it up and running on my phone and I can't see a performance slow down at all.  I don't know if I really like the joystick idea.  I'm using a half circle rather than a full one and it seems weird on a cell phone.  It's faster than the push buttons I had, but it's a bit awkward.
Online Drenius
« Reply #18 - Posted 2014-05-25 13:02:13 »

This is about the little mobile 3D game you are working on, correct?
Why don't you try to use a "go to" based movement system instead of a "go into key direction" one?
It would be a lot more user friendly and only a little bit work for you.
Offline princec

JGO Kernel


Medals: 340
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #19 - Posted 2014-05-25 13:18:13 »

Be super careful with touch controls... they make or break an entire game concept. Anything relying on on-screen controls is doomed to fail for starters.

Cas Smiley

Offline ImTroyMiller
« Reply #20 - Posted 2014-05-26 00:21:33 »

Here's a video showing the controller I put into it.  I think it may work a bit better if I set the input area up to go a little bit beyond the picture of the controller.  The biggest problem I have with it is the sudden stopping when I touch just out of the controller area.

Everything has been really easy so far, as far as the programming goes.  It seems like the general layout of the screen has been the most difficult.

<a href="http://www.youtube.com/v/A9vP2u66RG0?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/A9vP2u66RG0?version=3&amp;hl=en_US&amp;start=</a>
Online BurntPizza
« Reply #21 - Posted 2014-05-26 00:24:23 »

Then don't limit the area of the joystick, or only limit it to that half of the screen, and clamp the dist to whatever you want the max to be.
Offline princec

JGO Kernel


Medals: 340
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #22 - Posted 2014-05-26 00:38:30 »

It's doomed to failure  persecutioncomplex

Cas Smiley

Offline ImTroyMiller
« Reply #23 - Posted 2014-05-26 00:39:56 »

Then don't limit the area of the joystick, or only limit it to that half of the screen, and clamp the dist to whatever you want the max to be.

I'm just going to expand it out a little and make that area be full speed running.  The actual screen area without buttons is going to be used for ray picking which I already have set up.
Offline ImTroyMiller
« Reply #24 - Posted 2014-05-26 00:40:53 »

It's doomed to failure  persecutioncomplex

lol.  What should I do?
Offline Cero
« Reply #25 - Posted 2014-05-26 00:47:15 »

You're making a game that is designed for a pc or console on a phone with touch controls

3D third person, walking around emulating a stick and a button

why do you emulate a stick and a button ? because they dont exist on a phone screen - why do you need them ? because the game concept is not something that is designed FOR a phone.

Look at "Superbrothers Sword & Sworcery" on the Android for just an example of making something for a phone and not a game that ic just coincidentally on a phone

Offline ImTroyMiller
« Reply #26 - Posted 2014-05-26 00:57:30 »

I just fixed the controller and it actually controls alright.  I'll play around with it a bit more and see where it goes.
Offline theagentd
« Reply #27 - Posted 2014-05-26 01:04:53 »

Cas and Cero are right though. Although I can tolerate games like JRPGs that emulate SNES controllers (e.g. games that aren't twitch based), it's just impossible to play action games with an emulated analog stick and buttons. The responsiveness is horrible, you accidentally miss the buttons all the time, etc. I end up fighting the controls more than the actual enemies in the game, and it's just so frustrating I've simply given up on action games on phones.

Myomyomyo.
Offline pploco1996

Junior Member


Medals: 1
Exp: 1 year



« Reply #28 - Posted 2014-05-26 01:21:38 »

Dude, you REALLY need to learn some basic vector math...
Offline princec

JGO Kernel


Medals: 340
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #29 - Posted 2014-05-26 01:38:30 »

Play to the controller's strengths: you've got fairly accurate instant positional ability (tap-to-walk), intuitive drag to scroll or rotate (two fingers) or just keep the player avatar central. That sort of thing.

Cas Smiley

Pages: [1] 2
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

pw (15 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!