Player Movement and Rotation following an entity...
Hiya!
Im trying to come up with a control system that has the players entity move towards and rotate depending on where an entity is moved to around the screen with the mouse. But im having a bit of bother with getting it to work right. And wonder if you great folks, could help me in getting it working. that would be brillo. :)
Here's the code:
Im after a control system along the lines of Plobb! I first tried this with sprites, and have opted for quad meshes as I was having problems with the entities alpha. Hopefully it'll work with both. And i havent figured out how to have the mouse pointer gfx doesnt effet the player.
What you'll notice is that from the code above, the rotating and movement isnt quite right. There maybe a better method. And if you could help me out, much appreciated.
Many many thanks,
Clyde :)
Hiya!
Im trying to come up with a control system that has the players entity move towards and rotate depending on where an entity is moved to around the screen with the mouse. But im having a bit of bother with getting it to work right. And wonder if you great folks, could help me in getting it working. that would be brillo. :)
Here's the code:
; ; Mouse And Entity Movement Test. ; Clyde Radcliffe August 2005. ; ; ; Setup screen. ; Const XRES=640 Const YRES=480 Graphics3D XRES,YRES SetBuffer BackBuffer() ; ; Meet the Globals. ; Global MainCam Global PlayerSprite Global MousePointer ; ; Type definitions. Look at that! ; Type Player Field GFX Field X#, Y#, Z# End Type Type Bullet Field GFX End Type ; ; Main program running order. ; InitializePlayer() RunPlayer() End Function InitializePlayer() ; ; Setup camera. ; MainCam = CreateCamera() PositionEntity MainCam,0,0,-5 ; ; Hit the lights. ; Light=CreateLight() ; ; Create GFX for mouse pointer. ; used for rotating and positioning the player. ; MousePointer = CreateQuad() ScaleEntity MousePointer,.2,.2,.1 EntityColor MousePointer,164,000,164 EntityOrder MousePointer,1 ; ; Setup player. ; PlayerSprite = CreateQuad() HideEntity PlayerSprite ScaleEntity PlayerSprite,.25,.25,.25 Add.Player = New Player Add\GFX = CopyEntity ( PlayerSprite ) EntityColor Add\GFX, 000, 000, 255 End Function Function RunPlayer() While Not KeyHit( 1 ) UpdatePlayer() UpdateFire () RenderWorld() Flip Wend End Function Function UpdatePlayer() ; ; Get mouse position / movement info. ; TranslateEntity MousePointer, Float (MouseXSpeed())/100, -Float (MouseYSpeed())/100, 0 ; ; Center the mouse. ; MoveMouse XRES/2, YRES/2 ; ; Update the player. ; For Update.Player = Each Player MoveEntity Update\GFX,.01,.01,0 Rotate( Update\GFX, MousePointer ) ;PosX# = MoveToX( Update\GFX, MousePointer ) ;PosY# = MoveToY( Update\GFX, MousePointer ) ;Update\X = PosX# ;Update\Y = PosY# ;PositionEntity Update\GFX, Update\X, Update\Y, Update\Z ; ; Check for fire. ; If MouseDown( 1 ) Then Fire(Update) End If Next End Function Function Fire(Update.Player) Add.Bullet = New Bullet Add\GFX = CreateSprite() ScaleSprite Add\GFX,.1,.1 EntityColor Add\GFX,164,000,00 ; ; Start the bullet at the player ; PositionEntity Add\GFX,EntityX(Update\GFX),EntityY(Update\GFX),EntityZ(Update\GFX) ; ; Set the bullet's direction for moving ; PointEntity Add\GFX,MousePointer End Function Function UpdateFire() For Update.Bullet = Each Bullet MoveEntity Update\GFX,0,0,.4 If EntityInView(Update\GFX,maincam)=False Then FreeEntity Update\GFX Delete Update End If Next End Function Function CreateQuad( Parent=0 ) QuadMesh = CreateMesh ( Parent ) surfer = CreateSurface ( QuadMesh ) ; ; Make Quad - 2 faced. ; AddVertex surfer, -1, 1, 0, 0, 0 AddVertex surfer, 1, 1, 0, 1, 0 AddVertex surfer, -1,-1, 0, 0, 1 AddVertex surfer, 1, -1, 0, 1, 1 ; ; front. ; AddTriangle surfer, 0, 1, 2 AddTriangle surfer, 3, 2, 1 ; ; back. ; AddTriangle surfer,2,1,0 AddTriangle surfer,1,2,3 UpdateNormals QuadMesh Return QuadMesh End Function Function Rotate( Target, Destination ) PosX1# = EntityX( Target ) PosY1# = EntityY( Target ) PosX2# = EntityX( Destination ) PosY2# = EntityY( Destination ) Degrees# = 90 + ATan2( PosY1-PosY2, PosX1-PosX2 ) If Degrees < 0 Then Degrees = Degrees + 360 Else If Degrees > 360 Then Degrees = Degrees - 360 End If RotateEntity Target, 0, 0, Degrees End Function Function MoveToX#( Target, Destination ) PosX1# = EntityX( Target ) PosX2# = EntityX( Destination ) If PosX1 < PosX2 Then PositionX#=PositionX# + .1 If PosX1 > PosX2 Then PositionX#=PositionX# - .1 Return PositionX# End Function Function MoveToY#( Target, Destination ) PosY1# = EntityY( Target ) PosY2# = EntityY( Destination ) If PosY1 < PosY2 Then PositionY#=PositionY# + .1 If PosY1 > PosY2 Then PositionY#=PositionY# - .1 Return PositionY# End Function
Im after a control system along the lines of Plobb! I first tried this with sprites, and have opted for quad meshes as I was having problems with the entities alpha. Hopefully it'll work with both. And i havent figured out how to have the mouse pointer gfx doesnt effet the player.
What you'll notice is that from the code above, the rotating and movement isnt quite right. There maybe a better method. And if you could help me out, much appreciated.
Many many thanks,
Clyde :)