GPWiki.org
GPWiki.org
It is currently Wed Jun 19, 2013 4:21 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Making a star field
PostPosted: Fri Nov 12, 2010 11:34 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3854
Location: Ferriday, LA, US
I'm trying to make a star field for my game, but am having some problems.

What I want to do is use a function:

Code:
function updateStars(viewX, viewY)


That will update the stars' positions. viewX and viewY are given the player's position like this: (player.X-500, player.Y-500). The viewport is 1000*1000, so the player is centered that way.

So, given that I know the positions of the viewport and the stars, I need to figure out how to get those stars to constrain themselves to an area with X = between 0 and 1000 and Y = between 0 and 1000.

I was thinking that I could take a star's position and subtract the viewport space from that to get its "background-space" (between 0 and 1000) position. Then, with this position, I could do a check to see if the star was between 0 and 1000. If it were, I could then set the star to show up in the opposite end of the screen. If it's still in the "safe area" of the viewport, then draw it at its location minus the viewport space.

Of course, it works *nothing* like it should when I implement the function. No matter how I tweak it, it gives really, really bizarre results. I'm not even going to bother posting the function I have now as it's completely broken and useless (like me).

Please offer your advice.

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 12, 2010 12:38 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
Basically you want the stars constrained between

viewx <= starx < viewx+1000
viewy <= stary < viewy+1000

?


Code:
function updateStars(viewX, viewY)
{
  for n=1 to numberofstars
    while star(n).x < viewx : star(n).x=star(n).x+1000 : wend
    while star(n).x >= viewx+1000 : star(n).x=star(n).x-1000 : wend
    while star(n).y < viewy : star(n).y=star(n).y+1000 : wend
    while star(n).y >= viewy+1000 : star(n).y=star(n)y.-1000 : wend
  next
}

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 4:52 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3854
Location: Ferriday, LA, US
Thanks Jasmine! My problem was that I was thinking too much about it, and making it more complicated than it needed to be. Using the code you posted above, I was able to work things out. :)

"Only the human mind can take a humble mound of sod and raise it to the magnitude of a mountain range." :lol

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 12:45 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
When I did my asteroids game, I had a parallax starfield, so that the distant faint stars would drift slower, and only the nearest would drift at the speed of the player's ship.

The way to add this is to give stars a Z depth too, randomly between 0 to 1
  • 0 corresponds with the nearby stars which are drifting at the speed of the player's ship,
  • 1 corresponds with the farthest stars, where parallax makes them drift slower, which if you use the formula below will drift at half speed.
Stellar_Drift_Speed = Ship_Speed / (1+Z)

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 4:27 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3854
Location: Ferriday, LA, US
Nice. :) I'll keep that in mind as I continue working.

By the way, I noticed that I'm having a slight issue with my ship's movement. I have a maxSpeed variable that's a number. I check the ship's speed based on that, but the catch is that when my ship is moving at an angle that isn't divisible by 45 degrees, the movement "squares off". I can be pointed at, say, 60 degrees, but then as my ship picks up speed, it starts to go more toward a 45-degree angle.

Any suggestions for capping the ship's speed while keeping the movement angle correct?

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 4:45 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
Hmm... Are you capping the x and y velocities separately?

ie,
ship_speed.x is capped at maxspeed
ship_speed.y is capped at maxspeed

??

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 5:00 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3854
Location: Ferriday, LA, US
Currently, yes.

if this.x > maxSpeed : this.x = maxSpeed;

etc. But maxSpeed is one variable, applied to both x and y.

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 5:14 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
Then that's the problem.

If your ship is moving quickly, then both x and y speeds will be capped to maxspeed, and your ship will be moving with velocity (maxspeed, maxspeed), which is at 45 degrees.

What you need to do is cap the total speed, not the component speeds. Something like this...

Code:
totalspeed = SQRT( xspeed ^2 + yspeed ^2 )

if totalspeed > maxspeed then
  speedcapper = maxspeed/totalspeed
  xspeed = xspeed * speedcapper
  yspeed = yspeed * speedcapper
endif



Introducing a sudden cap on the speed can be felt like a bounce, as though the ship has hit something. You'll have to try it and see how it feels.

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2010 5:45 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3854
Location: Ferriday, LA, US
Thanks again, Jasmine! It's working well. I haven't noticed any bouncing movements, but that may be because I have the ship accelerate up to speed, and the max speed really isn't *that* high.

Woot! :D

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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

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