Gone and appear again, on the other side. How?

glancedUpglancedUp Posts: 46Member

I want to make an object that moves from left to right, appear again on the other side and continuing moving left to right indefinitely. How may I do that?

Answers

  • SomnivoreSomnivore Posts: 102Member
    edited March 2018

    The term you're looking for is screen wrapping. This is accomplished by a modulus operation on the object's position, taking into consideration the wrap area, and depending if you want to wrap horizontally, vertically or both. You need a shader which will render your object's sprite or model in two positions at once (or up to four, if wrapping both ways), taking into account whether an object is wrapping (which can be understood by using the sprite's global position and its boundaries.) Alternatively you can duplicate the sprite or model and its state on the other side of the screen. That will solve the visual problem, though be certain you're masking the wrap edges, otherwise it'll look like there's multiple objects at once instead of there being one object that's wrapping around the other side. If you're using collision detection with these objects, it may be easiest to have a duplicate collider in existence for those moments when you're on the edge of the screen, which mirrors the position of your player subtracting or adding the wrap area.

    Essentially there's one "real" object, and multiple reflections of it.

  • SomnivoreSomnivore Posts: 102Member

    Also fun fact, this is the basis for how you'd make a Portal clone.

    IT'S ALL SMOKE AND MIRRORS, PEOPLE

  • glancedUpglancedUp Posts: 46Member

    @Somnivore said:
    The term you're looking for is screen wrapping. This is accomplished by a modulus operation on the object's position, taking into consideration the wrap area, and depending if you want to wrap horizontally, vertically or both. You need a shader which will render your object's sprite or model in two positions at once (or up to four, if wrapping both ways), taking into account whether an object is wrapping (which can be understood by using the sprite's global position and its boundaries.) Alternatively you can duplicate the sprite or model and its state on the other side of the screen. That will solve the visual problem, though be certain you're masking the wrap edges, otherwise it'll look like there's multiple objects at once instead of there being one object that's wrapping around the other side. If you're using collision detection with these objects, it may be easiest to have a duplicate collider in existence for those moments when you're on the edge of the screen, which mirrors the position of your player subtracting or adding the wrap area.

    Essentially there's one "real" object, and multiple reflections of it.

    First of all, thank you very much @Somnivore for the detailed answer.

    I read several times in order to understand it properly. But I am a noob coder, translating all these to code would be a huge wall to overcome lol :#

    Can someone help me with the code?

    Thanks again and sorry for being such a noob :(

  • SomnivoreSomnivore Posts: 102Member

    @glancedUp said:
    First of all, thank you very much @Somnivore for the detailed answer.

    I read several times in order to understand it properly. But I am a noob coder, translating all these to code would be a huge wall to overcome lol :#

    Can someone help me with the code?

    Thanks again and sorry for being such a noob :(

    Well, one correction I'd like to make is that modulo only works on whole numbers in Godot. However you can just use a conditional instead, and add or subtract your wrap distance to your position if it goes out of bounds.

    I can't help you with code that doesn't exist. I also don't know what experience you have with coding. First step is for your level to monitor whatever objects you need wrapped, whether it's everything or just some things. Node2D and Spatial nodes have global positions in them, which you can read and assign every frame. Knowing you can assign a position to your objects, you should be able to figure out the rest.

  • dnattackdnattack Posts: 1Member
    edited January 26

    This is a very simple solution for 2d arcade games you can implement with just one line of code in your sprites (if you add the funcion in your global.gd file)

    1) add this function in your sprite (scene) code or in global.gd file
    obj can be any scene that includes the "position" property like Area2d or KinematicBody2D
    diff_x and diff_y the no. of pixels the object position can be outside the border before been warped

    func test_and_warp(obj, diff_x, diff_y):
        if  (obj.position.x < (0 - diff_x)): 
            obj.position.x = global._SIZE.x
        elif (obj.position.x > (global._SIZE.x + diff_x)): 
            obj.position.x = 0
        if (obj.position.y < (0 - diff_y)): 
            obj.position.y = global._SIZE.y
        elif(obj.position.y > (global._SIZE.y + diff_y)): 
            obj.position.y = 0
    

    2) execute it in the process function of the object (scene) to check and warp, for example:

    func _physics_process(delta):
        --> here move your sprite, then test the new position:
        global.test_and_warp(self, 4, 4)
    
Sign In or Register to comment.