[SOLVED] How to Implement Vector2-like Physics Without Access to Vector2

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
GemstoneSystem
Newbie
Posts: 11
Joined: Sun Jun 19, 2022 5:04 pm
Projects: Shards
itch: gemmysystem
Contact:

[SOLVED] How to Implement Vector2-like Physics Without Access to Vector2

#1 Post by GemstoneSystem » Fri Aug 12, 2022 3:33 am

Does anyone know how I could implement the functionality of pygame.math.Vector2 without, you know, actually having the ability to use pygame.math.Vector2? The PyGame build Ren'Py is built on doesn't have access to it, far as I can tell, and I need it for my platformer minigame's physics (gravity, friction, etc.). Is there maybe another way I can go about implementing these things manually?

EDIT: I found a Vector class that worked nicely enough for my purposes, paired with the kinematics values I'd already figured out, so I'm good now! Here's the class, for those interested:

Code: Select all

    class Vector:
        def __init__(self, x: float, y: float):
            self.x = x
            self.y = y

        # Vector operator overloads
        def __add__(self, other):
            if isinstance(other, self.__class__):
                return Vector(self.x + other.x, self.y + other.y)
            return Vector(self.x + other, self.y + other)

        def __sub__(self, other):
            if isinstance(other, self.__class__):
                return Vector(self.x - other.x, self.y - other.y)
            return Vector(self.x - other, self.y - other)

        def __mul__(self, other):
            if isinstance(other, self.__class__):
                return Vector(self.x * other.x, self.y * other.y)
            return Vector(self.x * other, self.y * other)

        def __rmul__(self, other):
            return self.__mul__(other)

        def __truediv__(self, other):
            if isinstance(other, self.__class__):
                return Vector(self.x / other.x, self.y / other.y)
            return Vector(self.x / other, self.y / other)

        def __eq__(self, other):
            if isinstance(other, self.__class__):
                return self.x == other.x and self.y == other.y
            return self.x == other and self.y == other

        def __neg__(self):
            return Vector(-self.x, -self.y)

        def make_int_tuple(self):
            return int(self.x), int(self.y)

        def set(self, vec):
            self.x = vec.x
            self.y = vec.y
Last edited by GemstoneSystem on Sun Aug 14, 2022 8:44 am, edited 1 time in total.

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1882
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to Implement Vector2-like Physics Without Access to Vector2

#2 Post by Ocelot » Fri Aug 12, 2022 4:11 am

You can easily reimplement it yourself. It is school-grade math, nothing too complex.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]