Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 642 times)
0 Members and 1 Guest are viewing this topic.
Offline JonnyGingerKid

Senior Newbie


Exp: 1 year



« 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?
Offline Hermasetas

Senior Member


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 Smiley
Offline trollwarrior1
« Reply #2 - Posted 2014-05-04 10:02:32 »

Instead of this
1  
(x+1)/spriteWidth


should be this:
1  
(x+spriteWidth)/spriteWidth
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):

1  
2  
3  
4  
5  
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

Senior Newbie


Exp: 1 year



« 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

Senior Newbie


Exp: 1 year



« 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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!