Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (602)
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  
  Typecasting in render method?  (Read 1717 times)
0 Members and 1 Guest are viewing this topic.
Offline Gamerulf

Junior Devvie


Medals: 2



« Posted 2013-04-17 19:29:53 »

Is this "allowed" ?

1  
g.drawImage(player, (int)playerX, (int)playerY, this);

My thought was that in my class, the playerX and playerY would keep it's "true" float value. And then chop off the deciamls when rendering.
This will, I think, result in a more accuarate speed because it doesnt lose the decimals every cycle.

But it looks wrong for some reason.

- Gamerulf
Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-04-17 19:53:35 »

There are no other (efficient) alternatives, so that's fine.

There's nothig wrong with casting floats to ints (except that it rounds towards 0)

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-04-17 21:33:58 »

One thing to watch out for while typecasting - always do it at the last step when possible. Since rendering is usually the last step in your game cycle, it's fine.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2013-04-18 01:50:23 »

Keep in mind though that java2D can only render ints which means that a slow movement stutters due to the loss of precision. You can easily get away with a game update rate of 30 and render rate capped at 30 and keep things smooth with only integer movement rates. Another reason why java2D sucks for rendering.

Offline Regenuluz
« Reply #4 - Posted 2013-04-18 07:22:57 »

Keep in mind though that java2D can only render ints which means that a slow movement stutters due to the loss of precision. You can easily get away with a game update rate of 30 and render rate capped at 30 and keep things smooth with only integer movement rates. Another reason why java2D sucks for rendering.

Are you claiming that other things can render coordinates as floats? E.g. render at (10.5, 10.5)? So rendering only half of the eleventh pixel on both axises?
Offline StumpyStrust
« Reply #5 - Posted 2013-04-18 07:43:01 »

No you cannot render sub pixels as there are none.

Thing is, java2D looks like pooder at low movement speeds where opengl does not. I think this as to do with the way in which opengl renders.

Offline Regenuluz
« Reply #6 - Posted 2013-04-18 08:19:54 »

No you cannot render sub pixels as there are none.

That was kind of my point. ;P

However, I haven't really looked closely at the difference of how slow move speed looks with OpenGL vs Java2D, so I wouldn't know of the difference. Smiley
Offline Grunnt

JGO Kernel


Medals: 95
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #7 - Posted 2013-04-18 08:38:53 »

1  
g.drawImage(player, (int)playerX, (int)playerY, this);

My thought was that in my class, the playerX and playerY would keep it's "true" float value. And then chop off the deciamls when rendering.
This will, I think, result in a more accuarate speed because it doesnt lose the decimals every cycle.

There is no higher accuracy than a pixel accuracy, this is a hardware limitation. The way you do it is pretty much optimal: using floats and only casting them to ints when rendering.

Only if you are working with tile maps you may need to pay close attention to the way things are rounded: if one row of tiles gets its position rounded up, and the next round rounded down the result may be a tiny but very noticeable gap. So perhaps in that case you could try rounding up or down to avoid this. But that doesn't seem necessary in you case.

Offline nsigma
« Reply #8 - Posted 2013-04-18 10:01:26 »

There is no higher accuracy than a pixel accuracy, this is a hardware limitation.

Yes and no.  Both Java2D and OpenGL can use interpolation to give the appearance of sub-pixel accuracy through interpolation.  As the Java2D drawImage calls only take ints, you have to use the form that takes an AffineTransform, and also make sure that interpolation is set to bilinear or bicubic.  Of course, you didn't want this to work quickly, right?  Grin

My thought was that in my class, the playerX and playerY would keep it's "true" float value ... But it looks wrong for some reason.

Depending on your algorithm, using
(int)(playerX + 0.5f)
might be better - will have the effect of rounding.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline theagentd

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #9 - Posted 2013-04-18 12:45:28 »

You can use translate(double tx, double ty) together with bilinear interpolation to preserve subpixel movement inside an image but the edges won't be affected. This'll be really slow unless it's hardware accelerated though. Plus it's the best you can do without any hardware antialiasing like multisampling or supersampling.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gamerulf

Junior Devvie


Medals: 2



« Reply #10 - Posted 2013-04-18 16:54:06 »

My thought was that in my class, the playerX and playerY would keep it's "true" float value ... But it looks wrong for some reason.

Depending on your algorithm, using
(int)(playerX + 0.5f)
might be better - will have the effect of rounding.

This was what I was thinking about when deciding whether to round the playerX itself or to typecast it.
If you hold down "left" for lets say 1minute. You are gona lose a small amount(0.1-0.9) every time you updade. And in the long run it will be a pretty big difference. Wouldn't it?

Both Java2D and OpenGL can use interpolation to give the appearance of sub-pixel accuracy through interpolation.  As the Java2D drawImage calls only take ints, you have to use the form that takes an AffineTransform, and also make sure that interpolation is set to bilinear or bicubic.  Of course, you didn't want this to work quickly, right?  Grin

I am using interpolation. But I think im using it wrong because I didnt understand anything of that Tongue
I use it when updating, for example, movement.
1  
playerX += 3.0f*interpolation;

- Gamerulf
Offline nsigma
« Reply #11 - Posted 2013-04-18 19:15:27 »

You can use translate(double tx, double ty) together with bilinear interpolation to preserve subpixel movement inside an image but the edges won't be affected.

I missed out that you'll probably need the antialiasing hint too.  That still might not work with drawImage() though.  See this blog post from 2007 - http://weblogs.java.net/blog/campbell/archive/2007/03/java_2d_tricker.html.  Not sure if that's still accurate.  I've seen code in the Java2D implementation that seems to treat blitting sub-pixel positioned images differently.  As you said, unless this is on an accelerated pipeline, it's going to be slow.

I am using interpolation. But I think im using it wrong because I didnt understand anything of that Tongue
1  
playerX += 3.0f*interpolation;


Different meaning (well usage) of interpolation.

1  
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);


Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Gamerulf

Junior Devvie


Medals: 2



« Reply #12 - Posted 2013-04-18 19:43:05 »

Sigh, and now I got even more reading to do..

EDIT:
WOW, It looks so much better now Cheesy
I can't stop looking at it -.-

- Gamerulf
Pages: [1]
  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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!