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$
  • 1
    $\begingroup$ Why not just detect when the object reaches one edge, and set it's location to the other edge? $\endgroup$ – X-27 the fluffy unicorn Dec 23 '16 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 Dec 24 '16 at 4:25
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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.