If I understood correctly, you want your object to halt immediately once no key is pressed, instead of slowly stopping due to friction.
If this is so, for the movement to stop you just set the linear velocity to zero using
phy set rigid body linear velocity, likewise for stopping rotation set zero the angular velocity with
phy set rigid body angular velocity,
Example (continuing on the previous code from
here):
sync on
sync rate 60
phy start
make object cube 1,20
scale object 1,100,150,100
phy make rigid body dynamic box 1
make object box 2,200,1,200
position object 2,0,-30,0
phy make rigid body static box 2
color object 1,rgb(128,128,0)
color object 2,rgb(0,128,200)
do
`control the box
gosub movements
`if no key pressed, stop movement
if scancode() = 0
phy set rigid body angular velocity 1, 0, 0, 0
phy set rigid body linear velocity 1, 0, phy get rigid body linear velocity y (1), 0
`Additionally you may want to set its rotation to 0
` phy set rigid body rotation 1,0,0,0
endif
phy update
sync
loop
movements:
`controls
if upkey() = 1 then phy add rigid body local force 1,0,0,40,5
if downkey() = 1 then phy add rigid body local force 1,0,0,-40,5
if leftkey() = 1 then phy add rigid body local force 1,-40,0,0,5
if rightkey() = 1 then phy add rigid body local force 1,40,0,0,5
if spacekey() = 1 and spkey = 0
phy add rigid body local force 1,0,20,0,2
spkey = 1
endif
if spacekey() = 0 and spkey = 1 then spkey = 0
`block the angular velocity
phy set rigid body angular velocity 1, 0, 0, 0
return
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why.
Programmers combine theory and practice: Nothing works and they don't know why.