Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) 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 2274 times) 0 Members and 1 Guest are viewing this topic.
JonnyGingerKid

Junior Devvie

Medals: 1
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: 45
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

Medals: 1
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: 45
Projects: 1
Exp: 3 years

Spacegame in progress

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

what value has width?
JonnyGingerKid

Junior Devvie

Medals: 1
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

 EgonOlsen (76 views) 2018-06-10 19:43:48 EgonOlsen (56 views) 2018-06-10 19:43:44 EgonOlsen (76 views) 2018-06-10 19:43:20 DesertCoockie (258 views) 2018-05-13 18:23:11 nelsongames (156 views) 2018-04-24 18:15:36 nelsongames (155 views) 2018-04-24 18:14:32 ivj94 (896 views) 2018-03-24 14:47:39 ivj94 (160 views) 2018-03-24 14:46:31 ivj94 (809 views) 2018-03-24 14:43:53 Solater (173 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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