Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (580)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
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  
  Spritesheets by dividing texture coordinates?  (Read 786 times)
0 Members and 1 Guest are viewing this topic.
Offline 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++){
  • [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?
Offline 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..

5 / 2 = 2

5f / 2f = 2,5f

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

Instead of this

should be this:
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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):

float size = 1f / 4f;
new Vector2f(size * xindex, size * yindex), //bottom left
new Vector2f(size * (xindex+1), size * yindex), //top left
new Vector2f(size * (xindex+1), size * (yindex+1)), //top right
new Vector2f(size * xindex, size * (yindex+1)) //bottom right
Offline 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?
Offline 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?
Offline 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.

Waterwolf (20 views)
2015-05-20 15:01:45

chrislo27 (24 views)
2015-05-20 03:42:21

BurntPizza (58 views)
2015-05-10 15:53:18

FrozenShade (44 views)
2015-05-07 09:11:21

TheLopais (207 views)
2015-05-06 13:36:48

TheLopais (191 views)
2015-05-06 13:35:14

TheLopais (198 views)
2015-05-06 13:33:39

TheLopais (214 views)
2015-05-06 13:32:48

TheLopais (214 views)
2015-05-06 13:31:28

ClaasJG (236 views)
2015-04-30 20:33:25
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

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 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‑
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!