Move character

Blitz3D Forums/Blitz3D Beginners Area/Move character

http://files.filefront.com/MoveCharacter20080930zip/;11934600;/fileinfo.html

Could someone please help me fix this? I want to it be so when you hold down the mouse button and drag, the character moves, and then if you do it again, the character moves some more. The way it is now, the character just keeps going back to the mouse's relative position on the screen instead of having a variable that changes each time. I know it's probably a simple fix but I just can't seem to figure it out. Most of the code relating to all this is at the very end of the .bb, and I've put some extra notes there too. Thanks.

I really appreciate this forum. I don't know what I'd do without some kind of a backup plan. Most of the stuff I can figure out on my own, but whenever I run into a wall it's great to have this place to turn to.

I can't run Blitz3d on this machine here, but usually I use code similair to this for mouse dragging:
graphics 800, 600, 0, 2
setbuffer backbuffer()

x = 400
y = 300

repeat

mx = mousex()
my = mousey()

if mousehit(1) then

  startmx = mx ;where mouse is when dragging starts
  startmy = my

  startcharx = x ;where object is when dragging starts
  startchary = y

endif

if mousedown(1) then

  dragx = (mx - startmx) ;how much the mouse has moved since dragging started
  dragy = (my - startmy)

  x = startcharx + dragx ;move character accordingly
  y = startchary + dragy

end if

cls
oval x, y, 10, 10  ;draw character
flip

until keyhit(1)

end


Okay, thanks. Looking forward to trying that when I get home.

That worked perfectly, thank you very much.

http://files.filefront.com/MoveCharacter20081007zip/;12028184;/fileinfo.html

Well, it was perfect. For some reason the new code only works on the first part of the function, which moves the character along the ground plane. When you try to use the other move modes, they just have the same bug as before, resetting the character's position each time you start a new mouse drag. One thing I noticed though is that each mode actually works just fine as long as it's first in the function, but I don't know why they won't all work together. I was thinking maybe I should break them up into different functions, but I'd really rather not do that for the purpose of keeping my menu code from getting even more complicated than it already is. Is there some way I can get them to all work in the same function? Hope you can help again. I've included a text file of the code this time and an exe instead of just the bb.

Hey I know how to fix it, you have to flush the mouse input (I made the function FlushInput() at the verrrrry bottom) to get rid of the speed... I changed your mouse hitting and downing into a Select thing so its easier to read, but I did leave your other way just incase you like it better (I didnt use the FlushInput in your other MouseHittings and Downings...

Graphics3D 640,480,32,1
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Const fps=30
ArialBold=LoadFont("arial",14,1,0,0)
SetFont ArialBold
AmbientLight 255,255,255
Dither 0
AntiAlias 0
WireFrame 0
AutoMidHandle 0
ClearTextureFilters
TextureFilter "alpha",2
period=1000/fps
time=MilliSecs()-period
elapsed%=0
ticks%=0
tween#=0
k%=0







.VARIABLES




.cameravariables

Global cameraposition#=5
Global cameraheight#=5.5
Global cameraturn#=0
Global camerapush#=15
Global cameradistance#=-9
Global aspectratio#=1.6




.mousevariables

Global mouseposition#=0
Global startingmouseposition#=0
Global horizontaldragamount#=0
Global mouseheight#=0
Global startingmouseheight#=0
Global verticaldragamount#=0
Global mousewheelposition%=0
; make variables for mouse location




.charactervariables

Global characterposition#=0
Global startingcharacterposition#=0
Global characterheight#=0
Global startingcharacterheight#=0
Global characterdistance#=0
Global startingcharacterdistance#=0
Global characterpush#=0
Global startingcharacterpush#=0
Global characterturn#=0
Global startingcharacterturn#=0
Global charactertilt#=0
Global startingcharactertilt#=0
; make variables for character position and direction




.menuvariables

Global menubarstate%=0
Global filemenustate%=0
Global quitstate%=0
Global movemode%=0







.CREATION




.cameracreation

Global camera=CreateCamera()
CameraClsColor camera,0,0,0 




.roomcreation

walltexturedark=LoadTexture("walldark.png",8)
floortexturedark=LoadTexture("floordark.png",8)
floor1=CreateCube()
ScaleEntity floor1,10.0/2,.001/2,10.0/2
PositionEntity floor1,-5,0,5
EntityTexture floor1,floortexturedark
floor2=CreateCube()
ScaleEntity floor2,10.0/2,.001/2,10.0/2
PositionEntity floor2,5,0,5
EntityTexture floor2,floortexturedark
floor3=CreateCube()
ScaleEntity floor3,10.0/2,.001/2,10.0/2
PositionEntity floor3,-5,0,-5
EntityTexture floor3,floortexturedark
floor4=CreateCube()
ScaleEntity floor4,10.0/2,.001/2,10.0/2
PositionEntity floor4,5,0,-5
EntityTexture floor4,floortexturedark
back1=CreateCube()
ScaleEntity back1,10.0/2,7.0/2,.001/2
PositionEntity back1,-5,3.5,10
EntityTexture back1,walltexturedark
back2=CreateCube()
ScaleEntity back2,10.0/2,7.0/2,.001/2
PositionEntity back2,5,3.5,10
EntityTexture back2,walltexturedark
side1=CreateCube()
ScaleEntity side1,.001/2,7.0/2,10.0/2
PositionEntity side1,10,3.5,5
EntityTexture side1,walltexturedark
side2=CreateCube()
ScaleEntity side2,.001/2,7.0/2,10.0/2
PositionEntity side2,10,3.5,-5
EntityTexture side2,walltexturedark




.charactercreation

Global mtsgirl=LoadMesh("mtsgirl.x")
PositionEntity mtsgirl,cameraposition,cameraheight-2,cameradistance+12
ScaleEntity mtsgirl,3,3,3
EntityColor mtsgirl,63,63,63




.menucreation

Global transparenttexture=LoadTexture("transparenttexturealpha.png")
Global menubar=LoadImage("menubar.png")
Global menubararea=LoadImage("menubararea.png")
Global menubarparent=CreateCube(camera)
EntityTexture menubarparent,transparenttexture
ScaleEntity menubarparent,.01,.01,.01
PositionEntity menubarparent,-.0021,-.0232,1.5
Global translucentmenubar=LoadSprite("translucentmenubar.png",1,menubarparent)
SpriteViewMode translucentmenubar,1
ScaleSprite translucentmenubar,.937,.73
Global filebutton=LoadImage("filebutton.png")
Global filebuttonarea=LoadImage("filebuttonarea.png")
Global filemenu=LoadImage("filemenu.png")
Global filemenuarea=LoadImage("filemenuarea.png")
Global filemenucancelarea=LoadImage("filemenucancelarea.png")
Global save=LoadImage("save.png")
Global savearea=LoadImage("savearea.png")
Global Quit=LoadImage("quit.png")
Global quitarea=LoadImage("quitarea.png")




.editorcreation

Global okayarea=LoadImage("okayarea.png")
Global okay=LoadImage("okay.png")
Global movealonggroundplane=LoadImage("movealonggroundplane.png")
Global moveupanddown=LoadImage("moveupanddown.png")
Global turn=LoadImage("turn.png")
Global push=LoadImage("push.png")
Global tilt=LoadImage("tilt.png")




.mousecreation

Global mouseimage=LoadImage("mouseimage.png")
Global mouse=LoadImage("mouse.png")
MoveMouse 320,240




While Not KeyHit(1)
Cls
Repeat
elapsed=MilliSecs()-time
Until elapsed
ticks=elapsed/period
tween#=Float(elapsed Mod period)/Float(period)
For k=1 To ticks
time=time+period
If k=ticks Then	CaptureWorld







.FUNCTIONS




MoveCamera()




UpdateWorld
Next
RenderWorld tween
MenubarControl()
MoveCharacter()
DebugText()
MouseControl()
WheelLimiter(position,lowestposition,highestposition)




Flip
Wend
End








.MenubarControl

Function MenubarControl()
If ImagesCollide(mouse,mouseposition,mouseheight,0,menubararea,0,0,0)
DrawImage menubar,0,0
EndIf
If ImagesCollide(mouse,mouseposition,mouseheight,0,filebuttonarea,0,0,0)
DrawImage filebutton,0,0
EndIf
If MouseDown(1) And ImagesCollide(mouse,mouseposition,mouseheight,0,filebuttonarea,0,0,0)
filemenustate=1
EndIf
If filemenustate=1
DrawImage filemenu,0,0
EndIf
If MouseDown(1) And ImagesCollide(mouse,mouseposition,mouseheight,0,filemenucancelarea,0,0,0)
filemenustate=0
EndIf
If filemenustate=1 And ImagesCollide(mouse,mouseposition,mouseheight,0,savearea,0,0,0)
DrawImage save,0,0
EndIf
If filemenustate=1 And ImagesCollide(mouse,mouseposition,mouseheight,0,quitarea,0,0,0)
DrawImage Quit,0,0
EndIf
If MouseDown(1) And ImagesCollide(mouse,mouseposition,mouseheight,0,quitarea,0,0,0)
Quit()
EndIf
End Function




.Quit

Function Quit()
End
End Function




.MoveCamera

Function MoveCamera()
PositionEntity camera,cameraposition,cameraheight,cameradistance
If KeyDown(77)
cameraposition=cameraposition+.3
ElseIf KeyDown(75)
cameraposition=cameraposition-.3
EndIf
If KeyDown(78)
cameraheight=cameraheight+.1
ElseIf KeyDown(156)
cameraheight=cameraheight-.1
EndIf
If KeyDown(72)
cameradistance=cameradistance+.3
ElseIf KeyDown(80)
cameradistance=cameradistance-.3
EndIf
RotateEntity camera,camerapush,cameraturn,0
If KeyDown(83)
cameraturn=cameraturn-1.5
ElseIf KeyDown(82)
cameraturn=cameraturn+1.5
EndIf
If KeyDown(73)
camerapush=camerapush-1.5
ElseIf KeyDown(81)
camerapush=camerapush+1.5
EndIf
If camerapush<-80
camerapush=-80
EndIf
If camerapush>80
camerapush=80
EndIf
CameraZoom camera,aspectratio
If KeyDown(55)
aspectratio=aspectratio+.01
ElseIf KeyDown(181)
aspectratio=aspectratio-.01
EndIf
If aspectratio<0.1
aspectratio=0.1
EndIf
End Function




.MoveCharacter

Function MoveCharacter()
movemode=-mousewheelposition
DrawImage okay,0,0
If movemode=1
DrawImage movealonggroundplane,0,0
EndIf
If movemode=2
DrawImage moveupanddown,0,0
EndIf
If movemode=3
DrawImage turn,0,0
EndIf
If movemode=4
DrawImage push,0,0
EndIf
If movemode=5
DrawImage tilt,0,0
EndIf
; scroll between the different move modes with the mouse wheel

EntityColor mtsgirl,63,63,63
PositionEntity mtsgirl,characterposition,characterheight,characterdistance
;If MouseHit(1) And movemode=1
;startingmouseposition=mouseposition
;startingcharacterposition=characterposition
;startingmouseheight=mouseheight
;startingcharacterdistance=characterdistance
;EndIf
;If MouseDown(1) And movemode=1
;horizontaldragamount=(mouseposition-startingmouseposition)
;characterposition=startingcharacterposition+horizontaldragamount/80
;verticaldragamount=(mouseheight-startingmouseheight)
;characterdistance=startingcharacterdistance-verticaldragamount/50
;End If
;; allow ground plane movement if the ground plane icon is lit 
;
;If MouseHit(1) And movemode=2
;startingmouseheight=mouseheight
;startingcharacterheight=characterheight
;EndIf
;If MouseDown(1) And movemode=2
;verticaldragamount=(mouseheight-startingmouseheight)
;characterheight=startingcharacterheight-verticaldragamount/70
;End If
;; allow up and down movement if the up and down icon is lit 
;
;RotateEntity mtsgirl,characterpush,characterturn,charactertilt
;If MouseHit(1) And movemode=3
;startingmouseposition=mouseposition
;startingcharacterturn=characterturn
;EndIf
;If MouseDown(1) And movemode=3
;horizontaldragamount=(mouseposition-startingmouseposition)
;characterturn=startingcharacterturn+horizontaldragamount/3
;End If
;; allow turning if the turn icon is lit
;
;If MouseHit(1) And movemode=4
;startingmouseheight=mouseheight
;startingcharacterpush=characterpush
;EndIf
;If MouseDown(1) And movemode=4
;verticaldragamount=(mouseheight-startingmouseheight)
;characterpush=startingcharacterpush-verticaldragamount/3
;End If
;; allow pushing if the push icon is lit
;
;If MouseHit(1) And movemode=5
;startingmouseposition=mouseposition
;startingcharactertilt=charactertilt
;EndIf
;If MouseDown(1) And movemode=5
;horizontaldragamount=(mouseposition-startingmouseposition)
;charactertilt=startingcharactertilt-horizontaldragamount/3
;End If

If MouseHit(1)
	Select movemode
		Case 1 ; allow ground plane movement if the ground plane icon is lit 
			startingmouseposition=mouseposition
			startingcharacterposition=characterposition
			startingmouseheight=mouseheight
			startingcharacterdistance=characterdistance
			
		Case 2 ; allow up and down movement if the up and down icon is lit 
			startingmouseheight=mouseheight
			startingcharacterheight=characterheight
			
		Case 3 ; allow turning if the turn icon is lit
			startingmouseposition=mouseposition
			startingcharacterturn=characterturn
			
		Case 4 ; allow pushing if the push icon is lit
			startingmouseheight=mouseheight
			startingcharacterpush=characterpush
			
		Case 5
			startingmouseposition=mouseposition
			startingcharactertilt=charactertilt

	End Select
	FlushInput()
EndIf

RotateEntity mtsgirl,characterpush,characterturn,charactertilt

If MouseDown(1)
	Select movemode
		Case 1 ; allow ground plane movement if the ground plane icon is lit 
			horizontaldragamount=(mouseposition-startingmouseposition)
			characterposition=startingcharacterposition+horizontaldragamount/80
			verticaldragamount=(mouseheight-startingmouseheight)
			characterdistance=startingcharacterdistance-verticaldragamount/50
			
		Case 2 ; allow up and down movement if the up and down icon is lit 
			verticaldragamount=(mouseheight-startingmouseheight)
			characterheight=startingcharacterheight-verticaldragamount/70
			
		Case 3 ; allow turning if the turn icon is lit
			horizontaldragamount=(mouseposition-startingmouseposition)
			characterturn=startingcharacterturn+horizontaldragamount/3
			
		Case 4 ; allow pushing if the push icon is lit
			verticaldragamount=(mouseheight-startingmouseheight)
			characterpush=startingcharacterpush-verticaldragamount/3
			
		Case 5
			horizontaldragamount=(mouseposition-startingmouseposition)
			charactertilt=startingcharactertilt-horizontaldragamount/3

	End Select
	FlushInput()
EndIf

End Function
; allow tilting if the tilt icon is lit




.MouseControl

Function MouseControl()
mouseposition=MouseX()
mouseheight=MouseY()
mousewheelposition=WheelLimiter(mousewheelposition+MouseZSpeed(),-5,-1)
HandleImage mouseimage,15,15
DrawImage mouseimage,mouseposition,mouseheight
HandleImage mouse,15,15
DrawImage mouse,mouseposition,mouseheight
End Function




.WheelLimiter

Function WheelLimiter%(position%,lowestposition%,highestposition%)
If position<lowestposition
position=lowestposition
EndIf
If position>highestposition
position=highestposition
EndIf
Return position
End Function




.DebugText

Function DebugText()
Color 63,63,63
Text 7,373,""
Text 7,388,""
Text 7,403,""
Text 7,418,""
Text 7,433,""
Text 7,448,""
Text 7,463,"Use the mouse wheel to select different move modes."
End Function







;

Function FlushInput()
	FlushKeys()
	FlushMouse()
End Function


hmmmmmmm.......

Something wrong?

Oh wow, you actually worked on my code for me? Thanks! Can't wait to try it when I get home.

No nothing's wrong.....just pretty complicated.

http://files.filefront.com/MoveCharacter20081009zip/;12028185;/fileinfo.html

I'm so glad you got it to work, I can't believe it. I'm gonna run with it, even though Select/Case isn't really my style. But just out of curiosity, how could I make your function work with my previous If/EndIf code? I tried putting it in what I thought would be comparatively similar places, but it didn't work. Anyway, don't worry about it. Thanks so much for fixing my code. The above link is the now working version for anyone who needs it.