"Look around" parallax possible?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
marigoldsofthenight
Regular
Posts: 44
Joined: Sat Aug 14, 2010 5:31 pm
Contact:

"Look around" parallax possible?

#1 Post by marigoldsofthenight »

I've played a few visual novels that have a parallax effect where the player can 'look around' a scene or background image in a limited range by moving the mouse around. Has anyone coded anything like this for Ren'Py?

marigoldsofthenight
Regular
Posts: 44
Joined: Sat Aug 14, 2010 5:31 pm
Contact:

Re: "Look around" parallax possible?

#2 Post by marigoldsofthenight »

Still searching. :D

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: "Look around" parallax possible?

#3 Post by Elmiwisa »

There is Viewport which can be used to move view part of the image, and can scroll. Simply superimpose multiple viewport on top of each other, each corresponding to a layer of object in the scene, and set them to basically, have different speed of scrolling, so that object in the background scroll slower than the foreground. When you draw the graphics for the scene, leave transparent background in so you can see through to the next layer.

marigoldsofthenight
Regular
Posts: 44
Joined: Sat Aug 14, 2010 5:31 pm
Contact:

Re: "Look around" parallax possible?

#4 Post by marigoldsofthenight »

Sorry... I don't think I'm explaining what it is I'm after properly.

Basically I'm wanting to emulate how the screen moves in this sample visual novel. (You'll have to start a game to see what I mean.)

https://googledrive.com/host/0ByTPdb3Fa ... mo_vn.html

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: "Look around" parallax possible?

#5 Post by Elmiwisa »

Uh, yeah, that was what I talked about...
If you want that kind of mouse behaviour, you can simply make an empty layer on top to control the mouse event too.

marigoldsofthenight
Regular
Posts: 44
Joined: Sat Aug 14, 2010 5:31 pm
Contact:

Re: "Look around" parallax possible?

#6 Post by marigoldsofthenight »

I was under the impression that viewports could only be used with vertical or horizontal scroll bars like in the tutorial. I'll do my best to read up on them.

User avatar
Alera
Miko-Class Veteran
Posts: 651
Joined: Sun Mar 21, 2010 3:20 am
Completed: Tortichki // Zayay // Hero's Spirit
Deviantart: psyalera
itch: psyalera
Location: UK
Contact:

Re: "Look around" parallax possible?

#7 Post by Alera »

I'm quite interested in this, could someone provide a code or at least some more explanations? Thank you!
Image
Games:
❤️ Zayay [Otome?][BxPlayer][NaNo 2013]
❤️ Tortichki [Drag&Drop mini game]

Other games I've worked on:
My Heart's Flame Emissary of Starlight Freedom From Silence Sickness
And many more unannounced/secret projects. (. .)

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: "Look around" parallax possible?

#8 Post by Elmiwisa »

Alera wrote:I'm quite interested in this, could someone provide a code or at least some more explanations? Thank you!
I just typed on this code. I did not put much effort into it, so I didn't use Viewport, but merely layer_at_list. Though it's just a demonstration of the concept to see how it can be done. using layer_at_list make thing very inflexible for you: you can't have varying level of distance from the point of view to the object that depend on scene for example, unless you are willing to declare a new layer for every single possible distance you might use in the game.
So here it is:

Code: Select all

init 800 python:
    class MouseParallax(renpy.Displayable):
        def __init__(self,layer_info):
            super(renpy.Displayable,self).__init__()
            self.xoffset,self.yoffset=0.0,0.0
            self.sort_layer=sorted(layer_info,reverse=True)
            cflayer=[]
            masteryet=False
            for m,n in self.sort_layer:
                if(not masteryet)and(m<0):
                    cflayer.append("master")
                    masteryet=True
                cflayer.append(n)
            if not masteryet:
                cflayer.append("master")
            cflayer.extend(["transient","screens","overlay"])
            config.layers=cflayer
            config.overlay_functions.append(self.overlay)
            return
        def render(self,width,height,st,at):
            return renpy.Render(width,height)
        def parallax(self,m):
            def trans(d,st,at):
                d.xoffset,d.yoffset=(int)(m*self.xoffset),(int)(m*self.yoffset)
                return 0
            return Transform(function=trans)
        def overlay(self):
            ui.add(self)
            for m,n in self.sort_layer:
                renpy.layer_at_list([self.parallax(m)],n)
            return
        def event(self,ev,x,y,st):
            import pygame
            if ev.type==pygame.MOUSEMOTION:
                self.xoffset,self.yoffset=((float)(x)/(config.screen_width))-0.5,((float)(y)/(config.screen_height))-0.5
            return
    MouseParallax([(400,"farback"),(200,"back"),(-200,"front"),(-400,"inyourface")])
The default master layer will be the layer with the intersection point for the parallax effect i.e. it will not move at all when you pan around. To obtain the parallax effect in the demo game above, use the layer in front of the master layer only. The layer behind the master layer will be stuff like the sun and the moon.
For example, you can use it like this:

Code: Select all

label start:
    show mountain range
    show candy house onlayer front
    show witch onlayer inyourface
    show hubble telescope onlayer back
    show crescent moon onlayer farback
    "The witch invite you to the house. The mountain range looming in the distance. Behind it appears to be the hubble telescope. The heartless moon cracks a malevolent smile."

User avatar
Alera
Miko-Class Veteran
Posts: 651
Joined: Sun Mar 21, 2010 3:20 am
Completed: Tortichki // Zayay // Hero's Spirit
Deviantart: psyalera
itch: psyalera
Location: UK
Contact:

Re: "Look around" parallax possible?

#9 Post by Alera »

@Elmiwisa
Thank you so much! This works perfectly~
Image
Games:
❤️ Zayay [Otome?][BxPlayer][NaNo 2013]
❤️ Tortichki [Drag&Drop mini game]

Other games I've worked on:
My Heart's Flame Emissary of Starlight Freedom From Silence Sickness
And many more unannounced/secret projects. (. .)

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: "Look around" parallax possible?

#10 Post by asatiir »

I was gonna post the same question before posting in this thread. I was wondering if there's a way to use a phone's gyroscope to give the same effect since phones don't have mice?
Image
Image

User avatar
hassohappa
Regular
Posts: 33
Joined: Thu Oct 22, 2015 8:51 pm
Tumblr: me-patra
Contact:

Re: "Look around" parallax possible?

#11 Post by hassohappa »

Sorry to revive an old thread, but does anyone know how to do this in the current version of Renpy?
Elmiwisa's old code posted above often results in a pickling error upon reloading :^(
I found some other threads where people had the same problem, but they never managed to find a way around it...
http://lemmasoft.renai.us/forums/viewto ... 28#p345125
http://lemmasoft.renai.us/forums/viewto ... 43#p314414

After putting renpy.use_cpickle = False, the error message started pointing fingers at this function:

Code: Select all

            def trans(d,st,at):
                d.xoffset,d.yoffset=(int)(m*self.xoffset),(int)(m*self.yoffset)
                return 0
Apparently, since the function is being declared within another one, it's unpickle-able. (Weirdly, the code works for me in a blank project. I've tried slowly pasting in the rest of my project into the blank project bit by bit, but at some arbitrary point it'll always throw the error.)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: "Look around" parallax possible?

#12 Post by xela »

I remember suggesting moving the func out of the methods scope... This does just that and can serve as a permanent solution that should allow saving at any time:

Code: Select all

init 800 python:
    class MouseParallax(renpy.Displayable):
        def __init__(self,layer_info):
            super(renpy.Displayable,self).__init__()
            self.xoffset,self.yoffset=0.0,0.0
            self.sort_layer=sorted(layer_info,reverse=True)
            cflayer=[]
            masteryet=False
            for m,n in self.sort_layer:
                if(not masteryet)and(m<0):
                    cflayer.append("master")
                    masteryet=True
                cflayer.append(n)
            if not masteryet:
                cflayer.append("master")
            cflayer.extend(["transient","screens","overlay"])
            config.layers=cflayer
            config.overlay_functions.append(self.overlay)
            return
        def render(self,width,height,st,at):
            return renpy.Render(width,height)
        def parallax(self,m):
            func = renpy.curry(trans)(disp=self, m=m)
            return Transform(function=func)
        def overlay(self):
            ui.add(self)
            for m,n in self.sort_layer:
                renpy.layer_at_list([self.parallax(m)],n)
            return
        def event(self,ev,x,y,st):
            import pygame
            if ev.type==pygame.MOUSEMOTION:
                self.xoffset,self.yoffset=((float)(x)/(config.screen_width))-0.5,((float)(y)/(config.screen_height))-0.5
            return
    MouseParallax([(400,"farback"),(200,"back"),(-200,"front"),(-400,"inyourface")])
            
    def trans(d, st, at, disp=None, m=None):
        d.xoffset, d.yoffset = int(round(m*disp.xoffset)), int(round(m*disp.yoffset))
        return 0
Like what we're doing? Support us at:
Image

User avatar
hassohappa
Regular
Posts: 33
Joined: Thu Oct 22, 2015 8:51 pm
Tumblr: me-patra
Contact:

Re: "Look around" parallax possible?

#13 Post by hassohappa »

YES saving/reloading is finally fine with that, thank you so much!! :D :D :D With my amateur skills I couldn't figure out how to get it to work on the outside! You've just saved an unknown number of newbs who will Google "how do I do parallax in renpy" in the future.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: "Look around" parallax possible?

#14 Post by trooper6 »

what does this look like in practice?

The link above doesn't work anymore...so are we talking about something that looks like the example on this webpage?
http://www.rleonardi.com/tutorial-animation/
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
hassohappa
Regular
Posts: 33
Joined: Thu Oct 22, 2015 8:51 pm
Tumblr: me-patra
Contact:

Re: "Look around" parallax possible?

#15 Post by hassohappa »

It looks like the example you posted, except with additional movement in the y direction. You can get rid of that by changing this line in the event function, if you want.

Code: Select all

#               self.xoffset,self.yoffset=((float)(x)/(config.screen_width))-0.5,((float)(y)/(config.screen_height))-0.5
                self.xoffset=((float)(x)/(config.screen_width))-0.5
You can control how much each layer gets offset, by, too! Pretty nifty.

Post Reply

Who is online

Users browsing this forum: Google [Bot]