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

Senior Newbie

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

Senior Newbie

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

Senior Newbie

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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 trollwarrior1 (27 views) 2014-11-22 12:13:56 xFryIx (69 views) 2014-11-13 12:34:49 digdugdiggy (48 views) 2014-11-12 21:11:50 digdugdiggy (42 views) 2014-11-12 21:10:15 digdugdiggy (36 views) 2014-11-12 21:09:33 kovacsa (60 views) 2014-11-07 19:57:14 TehJavaDev (64 views) 2014-11-03 22:04:50 BurntPizza (62 views) 2014-11-03 18:54:52 moogie (77 views) 2014-11-03 06:22:04 CopyableCougar4 (77 views) 2014-11-01 23:36:41
 theagentd 29x basil_ 27x BurntPizza 23x HeroesGraveDev 21x Riven 19x Spasi 18x princec 17x KevinWorkman 13x kpars 11x SHC 11x gouessej 10x Grunnt 10x Nate 9x Longarmx 9x LiquidNitrogen 8x CopyableCougar4 8x
 Understanding 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:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 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