3D acceleration depending on orientation

Blitz3D Forums/Blitz3D Programming/3D acceleration depending on orientation

Tabb(Posted 2014) [#1]
Hi all,

I have another problem with my space game. I'm working on how the ship will move. Its movement is defined by 3 vectors on x, y and z in absolute direction (because the ship can accelerate in one direction then keep moving in this direction with inertial dampener off while it is rotating).

Here is the code I'm using
; Ship acceleration
	
		; Forward/backwards
		fDest_cam_z=fDest_cam_z + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(17)-KeyDown(31)) * Cos(EntityYaw(ship)) * Cos(EntityPitch(ship))
		fDest_cam_y=fDest_cam_y + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(31)-KeyDown(17)) * Sin(EntityPitch(ship))
		fDest_cam_x=fDest_cam_x + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(31)-KeyDown(17)) * Sin(EntityYaw(ship)) * Cos(EntityPitch(ship))
	
		; Up/down
		fDest_cam_z=fDest_cam_z + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(57)-KeyDown(45)) * Sin(EntityPitch(ship)) * Cos(EntityRoll(ship))
		fDest_cam_y=fDest_cam_y + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(57)-KeyDown(45)) * Cos(EntityPitch(ship)) * Cos(EntityRoll(ship))
		fDest_cam_x=fDest_cam_x + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(45)-KeyDown(57)) * Cos(EntityYaw(ship)) * Sin(EntityRoll(ship))
		
		; Strafe
		fDest_cam_z=fDest_cam_z + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(32)-KeyDown(30)) * Sin(EntityYaw(ship)) * Cos(EntityRoll(ship))
		fDest_cam_y=fDest_cam_y + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(32)-KeyDown(30)) * Sin(EntityRoll(ship))
		fDest_cam_x=fDest_cam_x + fShip_acc * (fShip_speed_limit-fShip_speed) * (KeyDown(32)-KeyDown(30)) * Cos(EntityYaw(ship)) * Cos(EntityRoll(ship))
	EndIf


It seems to be OK for Forward/backwards and Strafe (but I'm not sure), but not for Up/down.
I can't figure out what is wrong it is a real headache ...
I searched this forum but couldn't find anything about this.

Thank you for your help.


Floyd(Posted 2014) [#2]
This is exactly why the various TForm commands exist.

Start reading about them here: http://www.blitzbasic.com/b3ddocs/command.php?name=TFormVector&ref=3d_a-z


Tabb(Posted 2014) [#3]
Thank you that is what I was looking for :)

But there is something odd with my code below :

; Ship acceleration
	TFormNormal 0,0,1, ship,0
	x_angz#=TFormedZ()
	x_angy#=TFormedY()
	x_angx#=TFormedX()
	fShip_speedx=fShip_speed_x*TFormedZ()
	TFormNormal 0,1,0, ship,0
	y_angz#=TFormedZ()
	y_angy#=TFormedY()
	y_angx#=TFormedX()
	fShip_speedy=fShip_speed_y*TFormedY()
	TFormNormal 1,0,0, ship,0
	z_angz#=TFormedZ()
	z_angy#=TFormedY()
	z_angx#=TFormedX()
	fShip_speedz=fShip_speed_z*TFormedX()
	
	If KeyDown(17)=True Or KeyDown(31)=True Or KeyDown(57)=True Or KeyDown(45)=True Or KeyDown(32)=True Or KeyDown(30)=True Then
		fShip_speed_z=fShip_speed_z+iShip_acc_z * (KeyDown(17)-KeyDown(31)) * x_angz/1000+iShip_acc_y * (KeyDown(57)-KeyDown(45)) * y_angz/1000+iShip_acc_x * (KeyDown(32)-KeyDown(30)) * z_angz/1000
		fShip_speed_y=fShip_speed_y+iShip_acc_z * (KeyDown(17)-KeyDown(31)) * x_angy/1000+iShip_acc_y * (KeyDown(57)-KeyDown(45)) * y_angy/1000+iShip_acc_x * (KeyDown(32)-KeyDown(30)) * z_angy/1000
		fShip_speed_x=fShip_speed_x+iShip_acc_z * (KeyDown(17)-KeyDown(31)) * x_angx/1000+iShip_acc_y * (KeyDown(57)-KeyDown(45)) * y_angx/1000+iShip_acc_x * (KeyDown(32)-KeyDown(30)) * z_angx/1000
	EndIf
	
	If iInerz=1 Then
	; Ship deceleration
		If KeyDown(17)=False And KeyDown(31)=False
			fShip_speed_z=fShip_speed_z-Abs(x_angz) * fShip_speed_z * iShip_acc_z/1000
			fShip_speed_y=fShip_speed_y-Abs(x_angy) * fShip_speed_y * iShip_acc_z/1000
			fShip_speed_x=fShip_speed_x-Abs(x_angx) * fShip_speed_x * iShip_acc_z/1000
		EndIf
		If KeyDown(57)=False And KeyDown(45)=False
			fShip_speed_z=fShip_speed_z-Abs(y_angz) * fShip_speed_z * iShip_acc_y/1000
			fShip_speed_y=fShip_speed_y-Abs(y_angy) * fShip_speed_y * iShip_acc_y/1000
			fShip_speed_x=fShip_speed_x-Abs(y_angx) * fShip_speed_x * iShip_acc_y/1000
		EndIf
		If KeyDown(32)=False And KeyDown(30)=False
			fShip_speed_z=fShip_speed_z-Abs(z_angz) * fShip_speed_z * iShip_acc_x/1000
			fShip_speed_y=fShip_speed_y-Abs(z_angy) * fShip_speed_y * iShip_acc_x/1000
			fShip_speed_x=fShip_speed_x-Abs(z_angx) * fShip_speed_x * iShip_acc_x/1000
		EndIf
	EndIf


The acceleration is OK. But the inertial dampeners aren't. If iInerz=1 then if I don't press any command key, the ship speed should slowly decrease to 0. It does it quite good, but I prevent the ship to accelerate freely.
I don't know If this is clear.


Tabb(Posted 2014) [#4]
I almost did what I wanted to move the ship.

Could you test it and tell me if controling the ship feels natural for you ?

Download here


RemiD(Posted 2014) [#5]
I get a mav... Maybe a missing file ?


Tabb(Posted 2014) [#6]
Yep sorry I forgot some of them.

Here is .rar with everything.

Download here (updated with angular velocity)


RemiD(Posted 2014) [#7]
I understand how to control the ship but personally i don't like it because there is no way to move up and down and no angular velocity.
But your demo works properly... Which is a good start.


Tabb(Posted 2014) [#8]
Hum, I forgot to specify up and down keys:
Down : X key
Up : Spacebar

I'm working on the angular velocity right now.


RemiD(Posted 2014) [#9]
With linear velocity and angular velocity, the gameplay will be more similar to independence war 2 than to freelancer (the name of your executable was freelancer...)

I have started to make a game in space with forces and linear velocity and angular velocity and the challenge that i had was to find a way to manage the ai with these velocities. Not easy.

Good luck for the next steps