Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 19, 2013 6:08 pm

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Fri Jun 29, 2012 10:57 pm 
Offline
User avatar

Joined: Wed Jun 20, 2012 10:33 pm
Posts: 35
Location: Doncaster, South Yorkshire
I'm developing a game in which you fly around on a broomstick after a flying golden ball. I need to program the flying golden ball, so that it flies around at random (Maybe, I might script it so that it flies a certain path, it's as yet undecided), and I want the screen to scroll as the player flies around. I've found Slick tutorials on scrolling Tiled games, but I can't find any for non-tiled ...

... I know, it's a nooby question. Sorry.

So, yeah. Any help? On either of the 2 problems?

Thankyou in advance :)


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 7:11 pm 
Offline

Joined: Mon Jun 18, 2012 1:58 pm
Posts: 11
With the scrolling issue, how are you rendering your "map" as of right now?

If its an array or list of some sort, when you render each tile, include x and y offset variables and when the player reaches the edge of the screen, modify these variables accordingly so the map has the illusion of scrolling around. Does that make sense?


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 8:19 pm 
Offline
User avatar

Joined: Wed Jun 20, 2012 10:33 pm
Posts: 35
Location: Doncaster, South Yorkshire
It's not a tiled game, I'm just drawing an image behind everything else. Thanks though :)


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 9:04 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
What you wanna do is basically, when player's x and y increase's/decreases, do the opposite on the "maps" x and y.

For example
Code:
Vector2f direction = new Vector2f(playerx, playery);
mapx -= 0.1f * delta * Math.cos(Math.toRadians(direction.getTheta()));
mapy -= 0.1f * delta * Math.sin(Math.toRadians(direction.getTheta()));



or something like that. hope this helps!

EDIT: Actually, i just thought.. Do you want the map to scroll continually in one direction? (like a R-Type like game?)


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 9:25 pm 
Offline
User avatar

Joined: Wed Jun 20, 2012 10:33 pm
Posts: 35
Location: Doncaster, South Yorkshire
I'll be honest, I didn't understand the example you gave :oops: I'm relatively new to Java, and even newer to Slick.

I want it so that the player flies around, and the camera scrolls with it, keeping the player in the middle. I've never done anything that scrolls before, or anything with a moving camera, and whilst there are a lot of tutorials online for scrolling tile-maps, I can't find any for scrolling without tiles D:

Thanks :)


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 9:56 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Haha, no problem :) I'm still quite new too, and had to do this for a scrolling map with the player centered.

Here's some code you'll need:

Code:
public int x = 300, y = 300; // These will be used for your player. change accordingly.
public int mapX = 0, mapY = 0; //These will be for the maps location when you draw it.
public Image map;
public float speed = 0.1f * delta; // This is for the speed of the map. change in .1 increments to if too slow.
public Vector2f direction;

//In the init Method
map = new Image("MAP LOCATION HERE");

//In the render Method
map.draw(mapX, mapY);

//In the update Method
Input input = container.getInput();
direction = new Vector2f(x, y);

if(input.isKeyDown(Input.KEY_LEFT) {   
   mapX += speed * Math.cos(Math.toRadians(direction.getTheta()));
   }
if(input.isKeyDown(Input.KEY_RIGHT) {
   mapX -= speed * Math.cos(Math.toRadians(direction.getTheta()));
   }
if(input.isKeyDown(Input.KEY_UP) {
   mapY += speed * Math.sin(Math.toRadians(direction.getTheta()));
   }
if(input.isKeyDown(Input.KEY_DOWN) {
   mapY -= speed * Math.sin(Math.toRadians(direction.getTheta()));
   }



Hopefully this works (fingers crossed, this was typed in the reply box xD). Post back if does/doesn't work ^_^


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 10:17 pm 
Offline
User avatar

Joined: Wed Jun 20, 2012 10:33 pm
Posts: 35
Location: Doncaster, South Yorkshire
Ah, that works, but it's not quite what I was looking for D:

See, I was looking for a way of moving the character and having a camera follow it, as opposed to making the background move. I was considering adding networking of some sort at some point (this method would make that near impossible), and it would make the business with the 'snitch' much more difficult. I might alter and just use that; I could use static sprites for the snitches, have multiple snitches and use them like coins? Could have them spawn randomly ... Adding multiplayer was a bit ambitious for my 2nd project anyway :lol: I'll keep looking into it, but I'll use your suggestion if I can't find a way of doing it.

Thanks for the help man! :)


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 10:55 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Ah, thats a shame. I'm interested in what type of scrolling you're actually looking for?


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 11:08 pm 
Offline
User avatar

Joined: Wed Jun 20, 2012 10:33 pm
Posts: 35
Location: Doncaster, South Yorkshire
AtomizerZero wrote:
Ah, thats a shame. I'm interested in what type of scrolling you're actually looking for?


Like on Pokemon, for example, the character wanders about and the camera follows, as opposed to the ground moving beneath the character?

Anyways, I've implemented the scrolling that you suggested and it works very well! :) Quite happy with it.


Top
 Profile  
 
PostPosted: Sun Jul 01, 2012 12:32 am 
Offline

Joined: Mon Jun 18, 2012 1:58 pm
Posts: 11
Delinitor wrote:
AtomizerZero wrote:
Ah, thats a shame. I'm interested in what type of scrolling you're actually looking for?


Like on Pokemon, for example, the character wanders about and the camera follows, as opposed to the ground moving beneath the character?

Anyways, I've implemented the scrolling that you suggested and it works very well! :) Quite happy with it.


With slick, its very hard to do it the other way around without a ton of code :/ I tried this for a game that used the same kind of Pokemon scrolling and it got confusing and messy quickly so I scrapped it altogether for this kind of scrolling.

Good luck with coding!


Top
 Profile  
 
PostPosted: Thu Jul 05, 2012 1:31 pm 
Offline

Joined: Wed Feb 15, 2012 10:17 am
Posts: 11
Delinitor wrote:
Like on Pokemon, for example, the character wanders about and the camera follows, as opposed to the ground moving beneath the character?

I thought the world moves around the player on pokemon too?
Obviously I haven't played pokemon in a while.


Top
 Profile  
 
PostPosted: Thu Jul 19, 2012 5:41 am 
Offline

Joined: Mon Apr 09, 2012 6:04 pm
Posts: 20
If you're still looking for generic 2d camera suggestions, I had a lot of luck with this method:

Create a class called Camera. Make it look something like this:

Code:
   public float XOffset;
   public float YOffset;
   
   // How close you'll need to get to the edge of the screen to start moving the camera
   // (in pixels)
   private float sideBuffer;
   
   public Camera()
   {
      XOffset = 0;
      YOffset = 0;
      
      sideBuffer = 150;
   }
   
   public void moveX(float offset)
   {
      XOffset -= offset;
   }
   
   public void moveY(float offset)
   {
      YOffset -= offset;
   }
   
   public boolean isOutsideBoundsX(float x)
   {
      if (
            x + XOffset < sideBuffer
            || x + XOffset > CUtil.Dimensions.width - sideBuffer
            )
         return true;
      return false;      
   }
   
   public boolean isOutsideBoundsY(float y)
   {
      if (
            y + YOffset < sideBuffer
            || y + YOffset > CUtil.Dimensions.height - sideBuffer
            )
         return true;
      return false;      
   }


Next, we'll need to move it around. When you move your controlled unit, add this in:

(fXpos and fYpos are the controlled unit's position. the x and yDifs are the amount we are moving this loop.
Code:
fXpos += xDif;
      fYpos += yDif;
      
      if ( CUtil.GameCamera.isOutsideBoundsX(fXpos) )
      {
         CUtil.GameCamera.moveX(xDif);
      }
      if ( CUtil.GameCamera.isOutsideBoundsY(fYpos) )
      {
         CUtil.GameCamera.moveY(yDif);
      }   


So now when we go outside of the buffer, the camera moves with our unit (because it looks weird if your character is locked in the center of the screen)

Lastly, we need to apply the camera to our sprites. Give your sprites a boolean, something like isCamerad. This is true if they are affected by the camera. So in your sprite's draw method, have something like...

Code:
public void draw(Graphics g)
   {   
      mTexture.setRotation(fAngle);
      
      mTexture.draw(
            (iXpos - (iWidth * fScale) / 2) + (bIsCamered ? CUtil.GameCamera.XOffset : 0),
            (iYpos - (iHeight * fScale) / 2) + (bIsCamered ? CUtil.GameCamera.YOffset : 0),
            fScale);
   }


Basically the idea is to shift the object by whatever the camera's offset is.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group