Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (624) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Spritesheets by dividing texture coordinates?  (Read 767 times) 0 Members and 1 Guest are viewing this topic.
JonnyGingerKid

Junior Devvie

Exp: 3 years

 « Posted 2014-05-04 05:02:38 »

So I'm making a 2D engine with LWJGL and wanted a spritesheet class, and thought that the best way to do it in OpenGL, would be to load the entire spritesheet, then divide the tex coords according to which sprite I want to access. I came up with this:

for(int x = 0; x < sheet.getWidth(); x++){
for(int y = 0; y < sheet.getHeight(); y++){
textures
• [y] = new Texture(path, false, new Vector2f[]{
//init tex coords
new Vector2f(x/spriteWidth, y/spriteHeight), //bottom left
new Vector2f((x+1)/spriteWidth, y/spriteHeight), //top left
new Vector2f((x+1)/spriteWidth, (y+1)/spriteHeight), //top right
new Vector2f(x/spriteWidth, (y+1)/spriteHeight) //bottom right
});
}
}

It turns out that way of dividing the tex coords doesn't work too well xD. How should I go about doing it?
Hermasetas

Senior Devvie

Medals: 6
Projects: 2
Exp: 3 years

I do gamez, yes!

 « Reply #1 - Posted 2014-05-04 09:46:04 »

My first guess is that you are using integers instead of floats.
When you divide an int with another int it will floor the result..

E.g.
5 / 2 = 2

5f / 2f = 2,5f

Hope that helps
trollwarrior1
 « Reply #2 - Posted 2014-05-04 10:02:32 »

 1 `(x+1)/spriteWidth`

should be this:
 1 `(x+spriteWidth)/spriteWidth`
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #3 - Posted 2014-05-04 10:58:27 »

lets say you have a texture with 16 sprites (4 x 4)
this means you have to do something like this (xindex and yindex ranging from 0 to 3):

 1  2  3  4  5 `float size = 1f / 4f;new Vector2f(size * xindex, size * yindex), //bottom leftnew Vector2f(size * (xindex+1), size * yindex), //top leftnew Vector2f(size * (xindex+1), size * (yindex+1)), //top rightnew Vector2f(size * xindex, size * (yindex+1)) //bottom right`
JonnyGingerKid

Junior Devvie

Exp: 3 years

 « Reply #4 - Posted 2014-05-04 19:18:52 »

I feel like I'm getting closer guys. Changing some of the variables to floats definitely helped. When I tried this:

float size = 1/width;
new Vector2f(size * x, size * y), //bottom left
new Vector2f(size * (x+1), size * y), //top left
new Vector2f(size * (x+1), size * (y+1)), //top right
new Vector2f(size * x, size * (y+1)) //bottom right

And then rendered the sprite from index (3,3), the texture was distorted, and when I tried this:

new Vector2f(x/spriteWidth, y/spriteHeight),
new Vector2f((x+spriteWidth)/spriteWidth, y/spriteHeight),
new Vector2f((x+spriteWidth)/spriteWidth, (y+spriteHeight)/spriteHeight),
new Vector2f(x/spriteWidth, (y+spriteHeight)/spriteHeight)

The texture was still distorted... I would've thought that at least 1 of these methods would work...

Any more ideas?
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #5 - Posted 2014-05-04 21:09:37 »

what value has width?
JonnyGingerKid

Junior Devvie

Exp: 3 years

 « Reply #6 - Posted 2014-05-04 21:33:58 »

Sorry guys, I fixed it. The big noob inside me thought that the texture coordinate call goes before the vertex call. Also I screwed up the order of the texture coordinates, and one of Slick-Utils parameters. In the end it was RobinB's solution that ended up working. Thanks to all of you!
Pages: [1]
 ignore  |  Print

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

 ClaasJG (30 views) 2015-04-27 13:36:51 BurntPizza (34 views) 2015-04-23 03:42:11 theagentd (37 views) 2015-04-22 16:23:07 Riven (51 views) 2015-04-16 10:48:47 Duke0200 (60 views) 2015-04-16 01:59:01 Fairy Tailz (44 views) 2015-04-14 20:13:12 Riven (47 views) 2015-04-12 21:36:37 bus hotdog (66 views) 2015-04-10 02:39:32 CopyableCougar4 (67 views) 2015-04-10 00:51:04 BurntPizza (72 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x Spasi 17x wessles 15x alwex 11x kingroka123 11x kevglass 7x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Hanksha 7x chrislo27 7x Riven 7x KevinWorkman 6x trollwarrior1 5x
 How 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:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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