2D in 3D Physics

Blitz3D Forums/Blitz3D Beginners Area/2D in 3D Physics

Buggy(Posted 2007) [#1]
In an attempt to create a basic 2D in 3D top-down space shooter, I first (of course) had to make the ship move correctly.

Needless to say, it doesn't.

This is what I have for moving the ship forward:
TFormVector 0, 1, 0, ship, 0

ship\dx# = ship\dx# + TFormedX()
ship\dy# = ship\dy# + TFormedY()

totalSpeed# = Sqr(ship\dx#^2 + ship\dy#^2)

If totalSpeed# > maxSpeed#

     ship\dx# = ship\dx#*maxSpeed#/totalSpeed#
     ship\dy# = ship\dy#*maxSpeed#/totalSpeed#

Endif



b32(Posted 2007) [#2]
Couldn't you use TurnEntity and MoveEntity instead ?
Ie:
TurnEntity ship, 1, 0, 0
MoveEntity ship, 0, 0, 1



Buggy(Posted 2007) [#3]
Not if I want the ship to be able to slide around (zero-G).

Note: I use TurnEntity for turning.


GfK(Posted 2007) [#4]
By the sound of it, you need to read up on 'inertia'.

You want to be able to rotate the ship, but it continues to go in the same direction until you apply thrust, at which point the trajectory gradually changes until the ship is moving forwards, yes?

You need to be using TurnEntity and TranslateEntity, with lashings of Sin and Cos.


Buggy(Posted 2007) [#5]
Well, I can do it fine in 2D, so is it the same in 2D-in-3D? I only used this method because I made a 3D space engine, and the way I controlled it was exactly this way, but with a z axis too. It was word-for-word the same, but with z.


Stevie G(Posted 2007) [#6]
Here's something I posted a couple of years ago .....

Graphics3D 640,480,16,1

Const Upkey = 200
Const DownKey = 208
Const LeftKey = 203
Const Rightkey = 205

Global Camera = CreateCamera()
PositionEntity Camera,0,0,-150

Type TypePlayer
	Field Model
	Field SpeedX#
	Field SpeedY#
	Field TurnSpeed#
	Field TurnDrag#
	Field TurnAcceleration#
	Field MoveDrag#
	Field MoveAcceleration#
	Field MoveDeceleration#
End Type

Type Frame 
	Field SpeedFactor#
End Type
Global FL.Frame = New Frame
FL\Speedfactor = 1	;60fps
  
Dim Player.TypePlayer(10)


;main program

LoadPlayer()
While Not KeyDown(1)

	DoPlayer()
	RenderWorld()
	
	Flip

Wend

;===============================================
;===============================================
;===============================================

Function LoadPlayer()

	For i = 1 To 10
		Player(i) = New TypePlayer
		Player(i)\SpeedX# = 0
		Player(i)\SpeedY# = 0
		Player(i)\TurnSpeed# = 0
		Player(i)\TurnAcceleration# = 0.03
		Player(i)\TurnDrag# = .99
		Player(i)\MoveAcceleration# = 0.05
		Player(i)\MoveDeceleration# = 0.05
		Player(i)\MoveDrag = .99
		
		;MaxSpeed = .03 / ( 1.0 -.99 ) = 3
		;MaxTurnSpeed = .05 / ( 1.0 - .99 ) = 5
		
		Player(i)\Model = CreateCone()
		ScaleEntity(Player(i)\Model, 10, 10,10)
		HideEntity(Player(i)\Model)
	Next
 	ShowEntity(Player(1)\model)
	
End Function

;===============================================
;===============================================
;===============================================

Function DoPlayer()

	For i = 1 To 10
		DoPlayerMovement( Player( i ) )
	Next

End Function

;===============================================
;===============================================
;===============================================

Function DoPlayerMovement( p.TypePlayer )

	;turning
	Turn# = KeyDown (LeftKey) - KeyDown(RightKey)
	p\TurnSpeed# = p\TurnSpeed#  *p\TurnDrag + Turn * p\TurnAcceleration * FL\SpeedFactor
	TurnEntity p\Model , 0,0, p\TurnSpeed * FL\SpeedFactor

	;moving
	Direction# = KeyDown(UpKey) - KeyDown(DownKey)
	TFormNormal 0,1,0,  p\Model, 0
	Thrust# = ( Direction = 1 ) * p\MoveAcceleration - ( Direction = -1 ) * p\MoveDeceleration 
	p\SpeedX = p\SpeedX * p\MoveDrag + Thrust * FL\SpeedFactor * TFormedX() 
	p\SpeedY = p\SpeedY * p\MoveDrag + Thrust * FL\SpeedFactor * TFormedY()
	TranslateEntity p\Model, p\SpeedX , p\SpeedY , 0

End Function