3
$\begingroup$

Suppose I have character surrounded by a cubic room.
My goal is to move that character to the opposite position of the room every time the character reaches one of the walls, roof and floor both included.

Like in the classical game "Asteroids" but with the Z axis included.

It's already possible to print the position by using a game property (string) attached to this module:

def update(cont):
    own = cont.owner

    own["pos"] = str([round(v) for v in own.worldPosition])

But how do I create the interval able to move the character object to the desired position?

There is a .blend prototype hosted HERE. Asteroids3D prototype

Addendum 1): There is another prototype that can be found here:

LINK TO THE SECOND PROTOTYPE

For this second prototype if you press only the W key continuously, the character will move to the opposite side as intended. But, how does one achieve this integration?

$\endgroup$
2
  • 1
    $\begingroup$ Why not just detect when the object reaches one edge, and set it's location to the other edge? $\endgroup$ Commented Dec 23, 2016 at 21:23
  • $\begingroup$ You want to do something like if obj.location.x > room.right.x: obj.localtion.x = room.left.x $\endgroup$
    – sambler
    Commented Dec 24, 2016 at 4:25

1 Answer 1

2
$\begingroup$

Sounds like a job for the modulo operator. The boundary size is hard coded here, so you can delete that boundary cube. Set the worldSize variable to the size you want the boundary to be:

def update(cont):
    own = cont.owner

    own["pos"] = str([round(v) for v in own.worldPosition])

    worldSize = 10

    own.worldPosition[0] = own.worldPosition[0] % worldSize
    own.worldPosition[1] = own.worldPosition[1] % worldSize

And a screenshot for context:

enter image description here

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .