Public Group

# Smooth Scrolling Algorithm

This topic is 5041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all lol, well ive had a shoking week, been brain dead all week, but woke up today and finnaly got this smooth scrolling algorithm around my head lol. Im posting it here for 2 reasons; 1. Anybody having troubles implementing smooth scrolling can use this as a reference. 2. If anybody can see an ineffiency in my algorithm i would be greatful if you could point it out :)

int screenWidth = 800;
int screenHeight = 800;

int tileHeight = 50;
int tileWidth = 50;

int offsetX = Math.round(x);
int offsetY = Math.round(y);

for(int c = Math.round(y / tileHeight); c <= (screenHeight / tileHeight) + ( y / tileHeight) + 1; c++){

for(int r = Math.round(x / tileWidth); r <= (screenWidth / tileWidth) + ( x / tileWidth) + 1; r++){

int xDraw = r * tileWidth - offsetX;
int yDraw = c * tileHeight - offsetY;

if(map[c][r] == 0){

g2D.drawImage(grass, xDraw, yDraw, null);
}

if(map[c][r] == 1){

g2D.drawImage(rock, xDraw, yDraw, null);

}

if(map[c][r] == 2){

g2D.drawImage(rock, xDraw, yDraw, null);

}

if(map[c][r] == 4){

g2D.drawImage(water, xDraw, yDraw,  null);

}

}

}


Thanks to everyone who helped me understand the concept of smooth srolling :) Cheers, Nick

##### Share on other sites
what resolution are you using?

int screenWidth = 800;
int screenHeight = 800;

800 x 600 is a standard resolution, are you running at 1600x1200

can you post the code for the routine below

g2D.drawImage(grass, xDraw, yDraw, null);

and what does Math.round do?

##### Share on other sites
800x800 atm, im not really worried about it atm, because im planning on making it full screen soon. Do you know if that is hard to implement?

Math.round rounds the number eg, Math.round(3.1) = 3, Math.round(3.7) = 4) haha

g2D.drawImage(grass, xDraw, yDraw, null);

g2D is a Graphics2D object, drawImage is a method inside the class, grass is the tile image, xDraw is the x position, y Draw is the y position, null is the ImageObserver ( it is set to null because im drawing the image to the buffer, not the screen )

Graphics2D is part of the APi which can be loacted at

http://java.sun.com/j2se/1.5.0/docs/api/

:) Have a good one!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015444
×