Accelerometer in Renpy

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.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

Accelerometer in Renpy

#1 Post by Andredron » Sun Aug 28, 2022 5:31 am



Good afternoon, it turned out to receive data from the accelometer on the renpy screen

Code: Select all

init python in sensors:
    import store
    renpy = store.renpy

    if renpy.android:
        import jnius

    if renpy.android:
        _Context = jnius.autoclass('android.content.Context')
        _Sensor = jnius.autoclass('android.hardware.Sensor')
        _SensorManager = jnius.autoclass('android.hardware.SensorManager')

        _PythonSDLActivity = jnius.autoclass('org.renpy.android.PythonSDLActivity')


        class AccelerometerSensorListener(jnius.PythonJavaClass):
            __javainterfaces__ = ['android/hardware/SensorEventListener']

            def __init__(self, onSensorChanged):
                super(AccelerometerSensorListener, self).__init__()
                self.onSensorChanged = onSensorChanged

            @jnius.java_method('(Landroid/hardware/SensorEvent;)V')
            def onSensorChanged(self, event):
                self.onSensorChanged(event.values[:3])

            @jnius.java_method('(Landroid/hardware/Sensor;I)V')
            def onAccuracyChanged(self, sensor, accuracy):
                pass


        _mActivity = _PythonSDLActivity.mActivity

        _sensor_manager = jnius.cast(
            'android.hardware.SensorManager',
            _mActivity.getSystemService(_Context.SENSOR_SERVICE)
        )
        _sensor = _sensor_manager.getDefaultSensor(
            _Sensor.TYPE_ACCELEROMETER
        )

        def on_sensor_changed(values):
            store.kek = values

        listener = AccelerometerSensorListener(on_sensor_changed)
        _sensor_manager.registerListener(listener, _sensor, _SensorManager.SENSOR_DELAY_NORMAL)
Data from the sensor will be written to the variable kek

Here is more about the values ​​from the sensor (specifically, from the accelerometer)

https://developer.android.com/reference ... elerometer

But I don’t have enough brains to figure out how to work with this data so that parallax can be modernized

Code: Select all

# example of using parallax:

# label start:
    # $ mouse_parallax.set((-20, -5, "l0"), (-40, -10, "l1"), (-60, -15, "l2"))
    # $ showp("city0", "city1", "city2", "bg room")
    # with dissolve
    # "Move the mouse.\nNormal parallax. Layers closest to the camera move more."
    #$ scenep()
    # with dissolve
    # $ mouse_parallax.set((-60, -15, "l0"), (-40, -10, "l1"), (-20, -5, "l2"))
    # $ showp("city0", "city1", "city2", "bg room")
    # with dissolve
    # "Move your mouse.\nMirror parallax. Values ​​for offset layers reset. Far layers move more now. It's like we're looking in a mirror."
    #$ scenep()
    # with dissolve
    # return

init 1111 python:
    # class for parallax
    class MouseParallax(renpy.Displayable):
        def set(self, *args):
            self.xoffset, self.yoffset = 0.0, 0.0
            self.layer_info = args
            for i in self.layers():
                if i in config.layers + ["master2"]:
                    config.layers.remove(i)
            index = config.layers.index("master") + 1
            for xdist, ydist, layer in args:
                if not layer in config.layers:
                    config.layers.insert(index, layer)
                    index += 1
            config.layers.insert(index, "master2")

        def __init__(self, *args):
            super(renpy.Displayable, self).__init__()
            self.set(*args)
            config.overlay_functions.append(self.overlay)
            return

        def layers(self):
            layers = []
            for dx, dy, layer in self.layer_info:
                layers.insert(0, layer)
            return layers

        def render(self, width, height, st, at):
            return renpy.Render(width, height)

        def parallax(self, xdist, ydist):
            func = renpy.curry(trans)(xdist=xdist, ydist=ydist, disp=self)
            return Transform(function=func)

        def overlay(self):
            ui.add(self)
            for xdist, ydist, layer in self.layer_info:
                renpy.layer_at_list([self.parallax(xdist, ydist)], layer)
            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

    def trans(d, st, at, xdist=None, ydist=None, disp=None):
        d.xoffset, d.yoffset = int(round(xdist * disp.xoffset)), int(round(ydist * disp.yoffset))
        if xdist != 0 or ydist != 0:
            xzoom = (config.screen_width + abs(xdist + xdist)) / float(config.screen_width)
            yzoom = (config.screen_height + abs(ydist + ydist)) / float(config.screen_height)
            if yzoom > xzoom:
                d.zoom = yzoom
            else:
                d.zoom = xzoom
            d.anchor = (.5, 1.0)
            d.align = (.5, 1.0)
        return 0

    # list for storing images with layers
    parallax_images = []

    # show multiple images, each on its own parallax layer
    # possible with a transform effect or even multiple
    # the number of images must not exceed the number of layers
    # otherwise the extra ones will be displayed on the master2 layer on top of the rest
    # $ showp("city1", ("city2", truecenter), ("city3", [truecenter, woo]))
    def showp(*args):
        global parallax_images
        layers = mouse_parallax.layers()
        for i in args:
            at = []
            image = i
            if isinstance(image, tuple):
                image,at=image
                if not isinstance(at, list):
                    at = [at]
            l = "master2"
            if len(layers) > 0:
                l = layers.pop()
            renpy.show(image, at_list=at, layer=l)
            i = (image, l)
            if not i in parallax_images:
                parallax_images.append(i)

    # remove one image from the specified (or any) layer
    def hidep(image, layer=None):
        global parallax_images
        if not layer:
            layer="master2"
            for ii, ll in parallax_images:
                if ii == image:
                    layer=ll
        i = (image, layer)
        renpy.hide(image, layer=layer)
        if i in parallax_images:
            parallax_images.remove(i)

    # clear all parallax layers
    # and add new images if needed
    def scenep(*args):
        global parallax_images
        for i in parallax_images:
            image, layer = i
            renpy.hide(image, layer=layer)
        parallax_images=[]
         if args:
             showp(*args)

     mouse_parallax = MousePara
https://renpyfordummies.blogspot.com/20 ... t.html?m=1

Link to github question:
https://github.com/renpy/renpy/issues/3615
Last edited by Andredron on Wed Sep 14, 2022 9:52 am, edited 2 times in total.
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

Re: Accelerometer in Renpy

#2 Post by Andredron » Mon Aug 29, 2022 4:46 am

Please help, here is the data

When there is a phone on the table - [0.39, 0.16, 9.89]


Standing - [9.5, -0.23, 3]

Where does the first number go, the x value,
Second number y value
Third number z value


Here is an article about z positions in renpy
https://www.renpy.org/doc/html/3dstage.html
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]