How to apply damping to objects in python script?

NEW DILEMMA!
How do I apply damping to an object using python scripting?

Drag objects script:

import bge
from bge import events
from bge import logic

cont = logic.getCurrentController()
empty = logic.getCurrentScene().objects['Empty']
obj = cont.owner

over = cont.sensors['Over']

right_click = logic.mouse.events[events.RIGHTMOUSE]
object = over.hitObject

if right_click == 2 and over.positive and 'drag' in object:
    obj["grab"] = over.hitObject

if right_click == 2 and obj["grab"] != None:
    grabobj = obj["grab"]
  
    ( x, y, z) = grabobj.worldPosition - empty.worldPosition
    distance = obj.getDistanceTo(grabobj)
    
    if distance < 5:
        appliedforce = (-x*distance*7, -y*distance*7, -z*distance*7)        
        grabobj.applyForce((appliedforce), False)
    
else:
    obj["grab"] = None

NOTE: Only problem now seems to be just to add damping to the object!

I’ve gotten pretty far with a script that drags objects… So far my drag script will only “drag” objects if they have a property named ‘drag’ in them… but my script seems to not work the way I want - it will set the position of the object I want to drag closer and closer to me quickly, instead of just following where my mouse goes - how do I fix this?

import bge
from bge import events
from bge import logic
import Rasterizer

cont = logic.getCurrentController()

over = cont.sensors['Over']

mouse = logic.mouse.events

right_click = mouse[events.RIGHTMOUSE]
object = over.hitObject

if right_click == 2 and over.positive and 'drag' in object:
    object.position = (over.hitPosition[0], over.hitPosition[1], over.hitPosition[2])

NOTE: the sensor over is a mouse over any sensor

create a cursor empty, wich follows the hitPosition. Parent the dragable object to it and remove the parent on release of the button.

I must create another object just to get this to work?

How do I get a drag script to work without having to create a cursor empty object?

to use an empty is easy and has the advantage, that you could just just slow parent the dragging object to that empty, what gives a smoother look.
but if you really don’t want to use emptys, do it this way: instead of changing the emptys position, change the dragging objects one.
to avoid, that the object isn’t totally above ground, you could add vector in normal direction to the new position.

Yes - but what do I change the dragging objects position to? What’s the technique? I’ve tried using ‘mouse over any’.hitPosition, but this just makes the objects quickly move towards use since the hitPosition is on the surface of the object, and the object is continually moving towards it’s hitPosition. So hitPosition is difficult.

i see… that means that you want your object stay on the ground but also follow the cursor?

what do you think, it is possible to make a Penumbra-like game with BGE?
in Penumbra there is very good physic interaction with objects, like drag, lift, throw etc

Ok - well I’ve gone a little bit further… Now he can drag an object - (the object parent’s to the camera), but now… It is able to MOVE THROUGH the floor if I drag it through the floor… which is whyyy… I don’t want to use the parent to anything method since it seems to lose all normal rigid body attributes, anyone know a way around this?

import bge
from bge import events
from bge import logic

cont = logic.getCurrentController()
camera = logic.getCurrentScene().objects['Camera']
obj = cont.owner

over = cont.sensors['Over']

mouse = logic.mouse.events

right_click = mouse[events.RIGHTMOUSE]
object = over.hitObject

if right_click == 2 and over.positive and 'drag' in object:
    object.setParent(camera)
else:
    object.removeParent()

As you can see in these images…

Attachments



i think we have to be more exactly concerning dragging…
if you are in first person perspective, for me, dragging is to control an object in your radius, but
if you are looking from top and want to drag something, then, you actually move objects above ground, maybe orientating to the facenormal,
but theres no radius. it’s like in world editors…
@abc123: you mean the “first” dragging. dragging shouldn’t be too difficult. but how do you throw…like bowling?

Basicly - dragging as in - I’m in a first person perspective, and I want to drag we’ll say boxes on the ground for example ( or like in Dusk by Mattline1) dragging doors or things - but I don’t want the objects to move through walls (in other words I can’t just use parent object)

hi again…ive tried your method but without parenting. but even if i just change the position by obj.worldPosition blabla objects can “go” through walls. but as in this method, the object is still a dynamic, i can “ask” it whether it has a collision etc…i’ll try a bit…

When using dynamic objects, it’s best to use a simpler collision box anyway. That said, could you drag the object, as above, and then use the simpler collision object to bang into walls? Just thinking out loud here…

ok…i’ve got a “solution”…it’s far away from perfect…but it works in rooms.

heres the code without parenting:

import bge
import mathutils as mat
log = bge.logic
evt = bge.events

con = log.getCurrentController()
own = con.owner
over= con.sensors[“over”]

dis = 14.0
tol = 2.0

mouse = log.mouse.events

obj = over.hitObject

if mouse[evt.RIGHTMOUSE] == 2 and over.positive and “drag” in obj:
ori = own.worldOrientation
lx = mat.Vector([1.0,0.0,0.0])
gx = lx * ori

hit,pos,nor = own.rayCast(obj,None,100.0,"wall",0,1)
if hit != None:
   dis = own.getDistanceTo(pos)
   print("dis = ",dis)
obj.worldPosition = own.worldPosition + gx * (dis - tol)

else:
pass

i’ve also did it with parenting:

import bge
import mathutils as mat
log = bge.logic
evt = bge.events

con = log.getCurrentController()
own = con.owner
over= con.sensors[“over”]

dis = 14.0
tol = 2.0

mouse = log.mouse.events

obj = over.hitObject

if mouse[evt.RIGHTMOUSE] == 2 and over.positive and “drag” in obj:
obj.setParent(own)
lx = mat.Vector([1.0,0.0,0.0])

hit,pos,nor = own.rayCast(obj,None,100.0,"wall",0,1)
if hit != None:
   dis = own.getDistanceTo(pos)
obj.localPosition.x = dis - tol

else:
obj.removeParent()

NOTE: the object MUST have an property “drag” and the walls need a property “wall”.

oh somehow…the tabs are gone…but i think its clear where some belong :wink:

and what i forgot to say:
the camera is parented to an empty (with x direction looking in the same direction like the camera and positioned at cameras position).
this empty holds everything like the python script and over any sensor…hope it’s clear so

With code - use the ’

' thingies.

Also - rayCast needs vector, not (obj), first argument is a vector…

no, rayCast can have both, vector or object…see api
at least it works at me.

Do you know how to apply damping to an object?

Is changing the objects damping even possible using python script?