Yet another test to do a gravity gun

Hi,

I’m a math teacher and I want to do a game for my students using blender’s GE. The idea is to do a “clone” of Portal but instead of using portals, they will have to solve mathematicals problems to advance in the game. Basicaly they will have to find a cube with the appropriate value to put on the button.

Anyway, I want to do a gravity gun to take the cubes. The various examples I’ve found do not really achieve what I want, so I’m doing it myself.

I have 2 problems.

First, to avoid the cubes to hit the player when they are attracted, I want to calculate the next position the attracted cube will have and if it will hit the player, I put it in the position where it is held. The problem is that the formula I’m using does not seems to work.

Basically, I calculate the force for each coordinate. Then I calculate the displacement from the previous frame (I use variables to keep the previous position of the cube). Finally, I use this formula for each coordinate:
futur position = actual position + previous displacement + force/(mass of the cube * 60)
The 60 comes from the fact that there is 60 frames per second.

But the result is not correct.

The other problem is that, once the cube is held, I use “setPosition” to put it in a particular point. But, I don’t understand why, it is slowly going down, even so I use setPosition every frame.

If you have any ideas…

Thanks.

p.s.: My file is kind of a mess and the comments are in french, but at least you can test it.

Attachments

FPS_attraction_gun_new_grav2.blend (709 KB)

Problem 1 can be solved with object.getLinearVelocity() , which returns an array of floats that indicate the velocity the object is moving at in Blender units per second. So, if [0.3,19.2,5.7] was returned, the object will move 19.2 units in the Y direction in one second.

Problem 2 I’ve had before, and it’s something I’ve never understood. However, there is a workaround: use a Motion(Servo) actuator that’s set to [0,0,0]. Servo uses forces to cause an object to reach a specified velocity, and no velocity is a valid target.

Thanks for your answer.
For the velocity, Will it give the velocity at the current frame (i.e. from the previous to the current frame), or can I have the velocity from the current to the next frame, which is the one I want? In a previous version I used setLinearVelocity instead of setForce but it was not as nice.

For the second problem, I solved it in a previous version but as I never understood how I did it, I couldn’t reproduced it for this version.

I’m going to try your solutions and will see how it works.

Thanks again.

I’m not sure whether getLinearVelocity() gives the velocity it just had or the velocity it’s currently having. Any difference between the two is probably minimal anyway.

The servo motion actuator solved the second problem. And for the first one, for the moment it seems to work, even if the values used are incorrect. So I think I will leave it for the moment.

There is some other small problems, but I will try to solve them myself.

Thanks for your help.