Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / 2D Camera?
- By adamsanderson Date 2009-10-20 06:03
I was hoping to make a simple 2D camera so that most of my code could be drawn relatively instead of always having to get an offset x,y, but I think I'm missing something :)

I tried this (really basic, I know, but before I hack around anymore I thought I'd ask):

require 'gl'
class Camera
  include Gl
  attr_accessor :x,:y
 
  def initialize(window, x=0,y=0)
    @window = window
    @x,@y = x,y
  end
 
  def draw
    @window.gl do
      glPushMatrix
      glTranslatef(x,y,0)
      yield
      glPopMatrix
    end
  end
end

Oh and the assumption is that you would then use it as:

@camera.draw do
  #... normal drawing code
end

This ends up not having any effect, any suggestions?

I think it would be pretty nice for a lot of games, a combination camera / clipping would let you do some decent windowing, etc.
- By devgomes Date 2009-10-20 22:01
Like you, I tried to use the gl translate method so that I could use "local" coordinates while rendering the widgets inside other widgets in my gui library.
It didn't work and I deduced (never bothered to really check) that gosu caches the drawings so they all happen at the end of the draw function making any gl methods wrapped around your drawing code mostly useless.
You could try using the gl block (never tried, though) or just monkey patch the image drawn function.
- By jlnr (dev) Date 2009-10-21 08:41
Hmm, you can always monkey-patch all versions of Image#draw I think... I thought about the following once:


translate(camera.x, camera.y) do
  @my_map.draw
  @my_objects.draw
end


But hm, I didn't think it was perfect enough to just take the extra work to add it. Mostly because the code won't be that simple once you want to have parallax scrolling, and the game I mainly work on has parallax scrolling even within the object list.
- By Trebor777 Date 2009-11-21 18:36
Hi sorry, if this post is considered as Necro-posting
But here's some code I've produced ( for castlevania ), in order to have a nice smooth parallax scrolling(with several backgrounds) with a camera following the player in a map of a given size.

Basically the camera class is in charge to convert the absolute coordinates "or real coordinates" into screen coordinates and to keep the player in the center of the screen.

Here is the code:

http://pastebin.com/f7e26b916
Up Topic Gosu / Gosu Exchange / 2D Camera?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill