Your problem is that when you move your object, a force is applied and thus the object will continue to move until that force is exhausted. To solve this problem you have 2 options.
Option 1 (better than option 2)
If the doors will move only up and down i.e. they will not interact apart from collision, the solution is to setup the object is kinematic. This will allow you to control the object without it being affected by other forces such as gravity.
` Project Name: Alone (Doors Module)
` Written By: Lukas W
` Date: 25.07/2007
Set Display Mode 1024,768,32
Sync On
Sync Rate 60
Hide Mouse
Phy Start
Global MouseLatch
` make floor
Make Object Box 1, 2000, 5, 2000
Position Object 1, 0, -2.5, 0
Phy Make Rigid Body Static Box 1
`--------------
doorsSetup()
doorsMake( "sliding door", "up", 100, 0, 100, 0, 0, textureIMG )
doorsMake( "sliding door", "up", 250, 0, 100, 90, 0, textureIMG )
Do
GoSub Player
` TEMP ---------------------------------------------------------------------
` cursor
Ink 0, 0
Line (Screen Width()/2)-5, (Screen Height()/2), (Screen Width()/2)+5, (Screen Height()/2)
Line (Screen Width()/2), (Screen Height()/2)-5, (Screen Width()/2), (Screen Height()/2)+5
` check for visibility to door
oid = Pick Object( (Screen Width()/2), (Screen Height()/2), 2, 99 )
If oid>0
distToObj# = Get Pick Distance()
temp1$ = "" : If distToObj# <= DoorOpenableDist# Then temp1$ = " You are close enough to open this door"
Text (Screen Width()/2), (Screen Height()/2)+15, Str$(int(distToObj#))+temp1$+temp2$
Text (Screen Width()/2), (Screen Height()/2)+30, "Type: "+doors(oid).type$
Text (Screen Width()/2), (Screen Height()/2)+45, "Direction: "+doors(oid).direction$
Text (Screen Width()/2), (Screen Height()/2)+60, "Is Locked: "+Str$( doors(oid).locked )
Text (Screen Width()/2), (Screen Height()/2)+80, Str$( doors(oid).yposMax# )
Text (Screen Width()/2), (Screen Height()/2)+95, Str$( doors(oid).ypos# )
EndIf
` --------------------------------------------------------------------------
doorsUpdate()
Phy Update
Sync : Loop
Function doorsUpdate()
For x = 1 To 99
If doors(x).type$ = "sliding door"
If MouseClick() =2 And MouseLatch =0 ` use door on Right Mouse Click
oid = Pick Object( (Screen Width()/2), (Screen Height()/2), 2, 99 )
If oid >0
If Get Pick Distance() <= DoorOpenAbleDist#
If Not doors(oid).locked
If doors(oid).MustOpen <> 1 Then doors(oid).MustOpen =1
Else
EndIf
EndIf
EndIf
MouseLatch =1
EndIf
` Door is closed, we must open it
If doors(x).MustOpen =1
If doors(x).direction$ = "up"
If doors(x).ypos# > doors(x).yposMax# -5.0
doors(x).MustOpen =2
Else
doors(x).ypos# = Object Position Y(x)
doors(x).ypos# = CurveValue( doors(x).yposMax#, doors(x).ypos#, 50.0 )
EndIf
EndIf
Else
` Door is opened, we must close it.
If doors(x).MustOpen =2
If doors(x).direction$ = "up"
` we'll let gravity handle the closing.
doors(x).ypos# = Object Position Y(x)
EndIf
EndIf
EndIf
Position Object x, doors(x).xpos#, doors(x).ypos#, doors(x).zpos#
Rotate Object x, doors(x).xang#, doors(x).yang#, doors(x).zang#
`*******************************************************************************
Phy Set Rigid Body kinematic Position x, doors(x).xpos#, doors(x).ypos#, doors(x).zpos#
Phy Set Rigid Body kinematic Rotation x, doors(x).xang#, doors(x).yang#, doors(x).zang#
`*******************************************************************************
EndIf
Next x
If MouseClick() =0 Then MouseLatch =0
EndFunction
Function doorsSetup()
GoSub doorsSetupTypes ` DBPro Cannot declare Types inside a function! :(
Global Dim doors( 99 ) As tDoor ` this is where we store the doors data
Global DoorOpenableDist# As Float ` with this variable you can decide how far away you can be to open a door
DoorOpenableDist# = 100.0
EndFunction
doorsSetupTypes:
Type tDoor
xpos# As Float
ypos# As Float
zpos# As Float
xang# As Float
yang# As Float
zang# As Float
ysiz# As Float
yposMax# As Float
MustOpen As Boolean
type$ As String
direction$ As String
textureIMG As Integer
locked As Boolean
EndType
RETURN
Function doorsIsLocked( id )
returnVal = doors(id).locked
EndFunction returnVal
Function doorsMake( doorType$, direction$, xpos#, ypos#, zpos#, angle#, locked, textureIMG )
xsiz = 100
ysiz = 150
zsiz = 5
` setup data for door
id = FreeObj()
doors(id).xpos# = xpos#
doors(id).ypos# = ypos#+(ysiz/2)
doors(id).zpos# = zpos#
doors(id).xang# = 0
doors(id).yang# = angle#
doors(id).zang# = 0
doors(id).ysiz# = ysiz
doors(id).type$ = doorType$
doors(id).direction$ = direction$
doors(id).textureIMG = textureIMG
doors(id).locked = locked
` this is a sliding door
If doorType$ = "sliding door"
doors(id).yposMax# = (ypos#+(ysiz/2))+ysiz
Make Object Box id, xsiz, ysiz, zsiz
Position Object id, xpos#, ypos#+(ysiz/2), zpos#
Rotate Object id, 0, angle#, 0
Phy Make Rigid Body Dynamic Box id
Phy Set Rigid Body Rotation id, 0, angle#, 0
`**********************************************************************************
` set as kinematic
`**********************************************************************************
phy set rigid body kinematic id,1
EndIf
If textureIMG >0 Then Texture Object id, textureIMG
EndFunction
Function FreeObj() : Repeat : Inc o : Until Not Object Exist(o)
EndFunction o
player:
` This is part of the PLAYER MODULE for ALONE.
` so it may look unfinished with unescessary variables. (i'm too lazy to remove them :/ )
` only the nescessary variables for this example are added here.
pspeed# = 3.0
csr# = 0.1
`********************
` Make Player if not made
if playerinitialized =0 then makeplayer()
`********************
` Rotate Camera using Mouse
cax# = WrapValue( (cax# + (MouseMoveY() * csr#) ) )
cay# = WrapValue( (cay# + (MouseMoveX() * csr#) ) )
` Running
If KeyState(42) : speedmultiplier = 70 : playerRun =1 : Else : speedmultiplier = 20 : playerRun =0 : EndIf
` Reset Variables
playermove =0
playerrun# = ( KeyState(42) / 2.0 ) +1.0
pspeed1# = pspeed#
playerinshadow =1
` Movement
If KeyState(17) : Phy Move Character Controller playerobj, pspeed1#*speedmultiplier : playermove =1 : EndIf
If KeyState(31) : Phy Move Character Controller playerobj, (pspeed1#*speedmultiplier)*-1 : playermove =1 : EndIf
If KeyState(30) : yRotate Object playerobj, cay#-90 : Phy Move Character Controller playerobj, pspeed1#*speedmultiplier : yRotate Object playerobj, cay# : playermove =1 : EndIf
If KeyState(32) : yRotate Object playerobj, cay#+90 : Phy Move Character Controller playerobj, pspeed1#*speedmultiplier : yRotate Object playerobj, cay# : playermove =1 : EndIf
` Set Movement Variables
If playermove =0 Then Phy Move Character Controller playerobj, 0.0
cpx# = Object Position X( playerobj ) : cpy# = Object Position Y( playerobj ) : cpz# = Object Position Z( playerobj )
` update camera/ player/ listener
Rotate Camera cax#+(humancalc/2), cay#+(humancalc2#), caz#
Position Camera cpx#, cpy#+28+humancalc+hide, cpz#
yRotate Object playerobj, cay#
RETURN
Function makeplayer()
Global playerobj = 100
cpx# = -100
cpy# = 100
cpz# = 100
Make Object Box playerobj, 30, 60, 30
Phy Make Box Character Controller playerobj, cpx#, cpy#, cpz#, 30, 60, 30, 1, 20.0, 60.0
Hide Object playerobj
Global playerinitialized = 1
EndFunction
Option 2:
If, on the other hand, you want the door to interact with the player e.g. player smashing the door instead of opening it, thus requiring gravity etc, you need to set the linear velocity of the object to zero every loop.
` Project Name: Alone (Doors Module)
` Written By: Lukas W
` Date: 25.07/2007
Set Display Mode 1024,768,32
Sync On
Sync Rate 60
Hide Mouse
Phy Start
Global MouseLatch
` make floor
Make Object Box 1, 2000, 5, 2000
Position Object 1, 0, -2.5, 0
Phy Make Rigid Body Static Box 1
`--------------
doorsSetup()
doorsMake( "sliding door", "up", 100, 0, 100, 0, 0, textureIMG )
doorsMake( "sliding door", "up", 250, 0, 100, 90, 0, textureIMG )
Do
GoSub Player
` TEMP ---------------------------------------------------------------------
` cursor
Ink 0, 0
Line (Screen Width()/2)-5, (Screen Height()/2), (Screen Width()/2)+5, (Screen Height()/2)
Line (Screen Width()/2), (Screen Height()/2)-5, (Screen Width()/2), (Screen Height()/2)+5
` check for visibility to door
oid = Pick Object( (Screen Width()/2), (Screen Height()/2), 2, 99 )
If oid>0
distToObj# = Get Pick Distance()
temp1$ = "" : If distToObj# <= DoorOpenableDist# Then temp1$ = " You are close enough to open this door"
Text (Screen Width()/2), (Screen Height()/2)+15, Str$(int(distToObj#))+temp1$+temp2$
Text (Screen Width()/2), (Screen Height()/2)+30, "Type: "+doors(oid).type$
Text (Screen Width()/2), (Screen Height()/2)+45, "Direction: "+doors(oid).direction$
Text (Screen Width()/2), (Screen Height()/2)+60, "Is Locked: "+Str$( doors(oid).locked )
Text (Screen Width()/2), (Screen Height()/2)+80, Str$( doors(oid).yposMax# )
Text (Screen Width()/2), (Screen Height()/2)+95, Str$( doors(oid).ypos# )
EndIf
` --------------------------------------------------------------------------
doorsUpdate()
Phy Update
Sync : Loop
Function doorsUpdate()
For x = 1 To 99
If doors(x).type$ = "sliding door"
If MouseClick() =2 And MouseLatch =0 ` use door on Right Mouse Click
oid = Pick Object( (Screen Width()/2), (Screen Height()/2), 2, 99 )
If oid >0
If Get Pick Distance() <= DoorOpenAbleDist#
If Not doors(oid).locked
If doors(oid).MustOpen <> 1 Then doors(oid).MustOpen =1
Else
EndIf
EndIf
EndIf
MouseLatch =1
EndIf
` Door is closed, we must open it
If doors(x).MustOpen =1
If doors(x).direction$ = "up"
If doors(x).ypos# > doors(x).yposMax# -5.0
doors(x).MustOpen =2
Else
doors(x).ypos# = Object Position Y(x)
doors(x).ypos# = CurveValue( doors(x).yposMax#, doors(x).ypos#, 50.0 )
EndIf
EndIf
Else
` Door is opened, we must close it.
If doors(x).MustOpen =2
If doors(x).direction$ = "up"
` we'll let gravity handle the closing.
doors(x).ypos# = Object Position Y(x)
EndIf
EndIf
EndIf
Position Object x, doors(x).xpos#, doors(x).ypos#, doors(x).zpos#
Rotate Object x, doors(x).xang#, doors(x).yang#, doors(x).zang#
Phy Set Rigid Body Position x, doors(x).xpos#, doors(x).ypos#, doors(x).zpos#
Phy Set Rigid Body Rotation x, doors(x).xang#, doors(x).yang#, doors(x).zang#
`***********************************************************************************************************
phy set rigid body linear velocity x,0,0,0
`***********************************************************************************************************
EndIf
Next x
If MouseClick() =0 Then MouseLatch =0
EndFunction
Function doorsSetup()
GoSub doorsSetupTypes ` DBPro Cannot declare Types inside a function! :(
Global Dim doors( 99 ) As tDoor ` this is where we store the doors data
Global DoorOpenableDist# As Float ` with this variable you can decide how far away you can be to open a door
DoorOpenableDist# = 100.0
EndFunction
doorsSetupTypes:
Type tDoor
xpos# As Float
ypos# As Float
zpos# As Float
xang# As Float
yang# As Float
zang# As Float
ysiz# As Float
yposMax# As Float
MustOpen As Boolean
type$ As String
direction$ As String
textureIMG As Integer
locked As Boolean
EndType
RETURN
Function doorsIsLocked( id )
returnVal = doors(id).locked
EndFunction returnVal
Function doorsMake( doorType$, direction$, xpos#, ypos#, zpos#, angle#, locked, textureIMG )
xsiz = 100
ysiz = 150
zsiz = 5
` setup data for door
id = FreeObj()
doors(id).xpos# = xpos#
doors(id).ypos# = ypos#+(ysiz/2)
doors(id).zpos# = zpos#
doors(id).xang# = 0
doors(id).yang# = angle#
doors(id).zang# = 0
doors(id).ysiz# = ysiz
doors(id).type$ = doorType$
doors(id).direction$ = direction$
doors(id).textureIMG = textureIMG
doors(id).locked = locked
` this is a sliding door
If doorType$ = "sliding door"
doors(id).yposMax# = (ypos#+(ysiz/2))+ysiz
Make Object Box id, xsiz, ysiz, zsiz
Position Object id, xpos#, ypos#+(ysiz/2), zpos#
Rotate Object id, 0, angle#, 0
Phy Make Rigid Body Dynamic Box id
Phy Set Rigid Body Rotation id, 0, angle#, 0
EndIf
If textureIMG >0 Then Texture Object id, textureIMG
EndFunction
Function FreeObj() : Repeat : Inc o : Until Not Object Exist(o)
EndFunction o
player:
` This is part of the PLAYER MODULE for ALONE.
` so it may look unfinished with unescessary variables. (i'm too lazy to remove them :/ )
` only the nescessary variables for this example are added here.
pspeed# = 3.0
csr# = 0.1
`********************
` Make Player if not made
if playerinitialized =0 then makeplayer()
`********************
` Rotate Camera using Mouse
cax# = WrapValue( (cax# + (MouseMoveY() * csr#) ) )
cay# = WrapValue( (cay# + (MouseMoveX() * csr#) ) )
` Running
If KeyState(42) : speedmultiplier = 70 : playerRun =1 : Else : speedmultiplier = 20 : playerRun =0 : EndIf
` Reset Variables
playermove =0
playerrun# = ( KeyState(42) / 2.0 ) +1.0
pspeed1# = pspeed#
playerinshadow =1
` Movement
If KeyState(17) : Phy Move Character Controller playerobj, pspeed1#*speedmultiplier : playermove =1 : EndIf
If KeyState(31) : Phy Move Character Controller playerobj, (pspeed1#*speedmultiplier)*-1 : playermove =1 : EndIf
If KeyState(30) : yRotate Object playerobj, cay#-90 : Phy Move Character Controller playerobj, pspeed1#*speedmultiplier : yRotate Object playerobj, cay# : playermove =1 : EndIf
If KeyState(32) : yRotate Object playerobj, cay#+90 : Phy Move Character Controller playerobj, pspeed1#*speedmultiplier : yRotate Object playerobj, cay# : playermove =1 : EndIf
` Set Movement Variables
If playermove =0 Then Phy Move Character Controller playerobj, 0.0
cpx# = Object Position X( playerobj ) : cpy# = Object Position Y( playerobj ) : cpz# = Object Position Z( playerobj )
` update camera/ player/ listener
Rotate Camera cax#+(humancalc/2), cay#+(humancalc2#), caz#
Position Camera cpx#, cpy#+28+humancalc+hide, cpz#
yRotate Object playerobj, cay#
RETURN
Function makeplayer()
Global playerobj = 100
cpx# = -100
cpy# = 100
cpz# = 100
Make Object Box playerobj, 30, 60, 30
Phy Make Box Character Controller playerobj, cpx#, cpy#, cpz#, 30, 60, 30, 1, 20.0, 60.0
Hide Object playerobj
Global playerinitialized = 1
EndFunction
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.