September 19, 2021, 15:16:37

Author Topic: Question about free 3d rotation  (Read 184 times)

Offline Pappons

  • Jr. Member
  • **
  • Posts: 3
Question about free 3d rotation
« on: September 15, 2021, 20:17:55 »
Hi guys,

I'm having a spot of trouble with free rotation in 3D. I am trying to recreate controls similar to those of that old Freelancer game where you direct your ship with the mouse, and it sort of works, but not fully.

My code:
Code: [Select]
'
'
'
Strict

Framework Openb3dmax.B3dglgraphics

'- init -----------------------------------------------------------------------------------
Graphics3D DesktopWidth(),DesktopHeight(), 32, 60

ClearTextureFilters

Local cam:TCamera=CreateCamera()
PositionEntity cam, 0, 3, 0
CameraRange cam, .1, 750  ' set the camera range To something reasonable

'- light ---------------------------------------------------------------------------------
Local light:TLight=CreateLight(2,cam)
LightColor light,100,100,100
PositionEntity light,0,0,0

AmbientLight 255,255,255

'- sky box ---------------------------------------------------------------------------------
Local sky:TMesh=CreateSphere(24)
ScaleEntity sky,500,500,500
FlipMesh sky
EntityFX sky,1
Local sky_tex:TTexture=LoadTexture("data\tilingspacebackground.jpg")
ScaleTexture sky_tex, 0.5, 0.5
EntityTexture sky,sky_tex

'- player ship ---------------------------------------------------------------------------------
Local ship:TEntity = LoadMesh("data\InterstellarRunner.b3d")
ScaleEntity ship, 0.15, 0.15, 0.15
PositionEntity ship, 5, 0.1, 4

Local shiptex:TTexture=LoadTexture("data\InterstellarRunner.png")
EntityTexture ship, shiptex
EntityShininess(ship,0.1)

'- player ship ---------------------------------------------------------------------------------
Local cx:Float = DesktopWidth() / 2
Local cy:Float = DesktopHeight() / 2

While Not KeyDown(KEY_ESCAPE)

If KeyHit(KEY_ENTER) Then DebugStop

MoveEntity ship,0,0,0.10 ' move straight forward

Local dx:Float = ( MouseX() - cx ) / (cx ) ' get the mouse position
Local dy:Float = ( MouseY() - cy ) / (cy ) ' relative to the center of the screen

If MouseDown(1)
TurnEntity ship,(dy), -(dx),0 ' turn the ship in direction of the mouse
EndIf

PointEntity( cam, ship ) ' point camera at ship

If EntityDistance( cam, ship ) > 10
MoveEntity cam, 0, 0, 0.1 ' move towards ship if at a certain distance
EndIf

UpdateWorld
RenderWorld

Flip
GCCollect

Wend

I have a feeling the solution has to do with quaternions, but my experience with it is limited and any tinkering I've done has been fruitless so far.

Would anyone be able to nudge me in the right direction here?

Thanks in advance!

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 499
Re: Question about free 3d rotation
« Reply #1 on: September 17, 2021, 20:47:52 »
Hi,

I tried your code but I don't know what type of controls are in Freelancer even after watching a video.

I assume your main problem is that the ship ends up dis-orientated, upside down, etc. so I tried to implement a simple self-correcting system where it stabilizes itself automatically but I couldn't really manage it, it was trickier than I thought it would be. It shouldn't be a problem with quaternions they should work fine.

One thing that might help is to use RotateEntity instead of TurnEntity because then it's easier to know where you are and can start re-balancing the ship. Another thing that might help is to limit the value of dx and dy to a maximum so you don't end up over-turning the ship.

Offline STEVIE G

  • Hero Member
  • *****
  • Posts: 608
Re: Question about free 3d rotation
« Reply #2 on: September 17, 2021, 22:08:38 »
I'm assuming minib3d works like b3d in terms of rotations. Turnentity should use quarternions. The problem is turning on 2 axis at the same time.  You could try turnentity separately on each axis or do this ...

Make your ship entity a pivot. Load and parent your ship model to this pivot. Turnentity the pivot only  on the yaw axis. Turnentity the ship model only on the local pitch axis. 

Offline Pappons

  • Jr. Member
  • **
  • Posts: 3
Re: Question about free 3d rotation
« Reply #3 on: September 17, 2021, 23:30:02 »
Thanks for the replies, guys. I really appreciate it!

Turning on each axis didn't seem to change much. I'm noticing a certain behavior where I can pretty much keep turning to either side but if I keep guiding the ship either up or down, it causes it to "flip" and turn in the opposite direction once it passes 180 degrees.

Here's a video that shows what I am aiming to do:
Code: [Select]
https://www.youtube.com/watch?v=NODc1lXn0ak&t=27s - Watching the video again, makes it seem like it's barely rotating compared to the camera.

That leads me to wonder; could it be that the camera chasing the ship could be confusing the view and the ship indeed is moving as it should?
« Last Edit: September 17, 2021, 23:32:27 by Pappons »

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal