Python set position (not working correctly)

When I set a command for change object position on collision with another object, there is a problem. The position changes ok but the object hurls away.

Here an example
ball.blend (1.08 MB)

Press arrow key for ball motion. If ball falls on the blue plane, set a change reset position. I do not restart the scene, only change the object’s position. I use Blender 2.76
script used:

import bge

scene = bge.logic.getCurrentScene()
objList = scene.objects

obj_name = 'CubeNOVE'
cube = objList[obj_name]

cube.worldPosition = [2.75482, 3.59394, 13.89875]

I try this script with spawn point, but not work again


import bge


def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    scene = bge.logic.getCurrentScene()
    
    spawnPoint = scene.objects ["spawnPoint"]
    
    if own.position.z < 5:
        own.worldPosition = spawnPoint.worldPosition

main()

I guess it is the same question as on Stackexchange.

As you already wrote that your object is an empty, the next question is: is it a static or dynamic physics type?

I asked because static empties can’t collide with anything. Therefore you would not be able to detect an collision.

btw. you do not need to surround your script code with def main(). It forces the reader jump up and down in text without any benefits.

hint: it is better to copy the position rather than refer to it:


storedPosition = object.worldPosition.copy()

Hi Monster, sorry my English.
my object (player-CubeNove) is a rigid body. Plane is collision for first script
[video]http://petercakovsky.com/docasne/drop.mp4[/video]
I want to do set position as the start game.
hmm… gravity it s strong

YourHint: hard for me Monster :smiley: I´m veeery noob in python

import bge

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    scene = bge.logic.getCurrentScene()
    
    spawnPoint = scene.objects ["spawnPoint"]
    own.worldPosition = spawnPoint.worldPosition.copy()
 main()

your code works, ive looked at ur blend, my opinion there is something wrong with ur physics settings.
anyway, your ball will spawn at the right position, but the problem you have is it keep moving in anydirection.

tried a few things, orientation/velocity/angularVelecity none of them got impact on ur ball (slows it down a notch but not totally)

i even removed everything from ur scene and build it again, strange physics, if you move the ball (keep holding down W) when he goes off the ledge it does not fall but float in the direction he was going and dropping slightly at Z direction until you release the key.

i would suggest to use addObject actuator. when ball hits the plane -> end object and spawn a new one. this should solve your problems.

When I set a command for change object position on collision with another object, there is a problem. The position changes ok but the object hurls away

This is due to using rigid body, you change the location but the ball keeps it momentum and spinning actions.
you need to stop all movement/rotation from ur ball when you move the position.

own.applyForce((0,0,9.8*own.mass),0)
Assuming gravity is not changed.

the script works fine…something with the physics is goobered up, but I cant put my finger on it. Turning collision bounds on, on the plane keeps it from falling through

I missed the file you posted in post#1.

The code works fine. But you forgot to cancel the velocity after teleporting to the given position. That means the ball will still fall down with the speed it had at the time of collision. Due to the steady increase it will fall faster and faster until the physics engine can’t detect the collision anymore.


cube.linearVelocity = [0,0,0]

Anyway, a physics object inside another is usually no good idea (CubeNOVE, EmptyNOVY). If you really want that setup, you better move both objects.


emptyNOVY = scene.objects["EmptyNOVY"]
emptyNOVY.worldPosition = [2.75482, 3.59394, 13.89875]
emptyNOVY.linearVelocity = [0,0,0]

thank you very much all!
I’m going to try and write here.

Monster, your last script work very well, thank you very much!