Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Moving Objects along their local axis

Author
Message
Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 5th May 2010 21:56
So I am making a space shooter, and I am tyring to simulate zero gravity flight physics. So far I have been able to sucessfully code foward and backward motion, pitch, yaw, and roll. However, now I am trying to code the maneuvering thrusters, and I have discovered that my code is not working the way I want it to.
So what I want to happen is when I push the upkey I want the space ship to move in the positive direction on the axis that interesects the top and bottom of the ship. In other words the ships local Y axis. However what actually happens is the ship moves on the gobal Y axis, which mean that when the ship pitches up 90 degrees it still moves up vertically instead of moving in the negative direction on the Z axis like I want it to.
I was wondering if there was anyone out there in darkforum land that could help explain to me how to move objects along there local axis
Indicium
16
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 6th May 2010 01:12
Why not use the commands:

move object

roll object left

roll object right

turn object left

turn object right

pitch object up

pitch object down

Rawwrr. Sig Fail.
Newcastle is awesome
Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 6th May 2010 02:05
Mad NighMare,
Because I need to use vectors in order to simulate zero gravity. I don't think I use those commands and still simulate the zero gravity nature of space.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 6th May 2010 20:07
Those commands would still work for your rotation, but it sounds like what you are after is being able to rotate your object but leave it moving in it's original direction.

Make a small cube and hide it. Use that as your navigation object. You can keep it pointing in the direction of your inertia, and just position the ship where the cube is no matter what it's rotation might be.

Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 7th May 2010 06:34
KISTech, Thanks for your reply. I wouls have never taught of that myself, but I think I am going to stick with the method that I am using now. It involves vectors, trigonometry, and a little bit of physics, but its working great. The only problem I am having now is figuring out which trig function to use for a given angle.
Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 7th May 2010 17:50
ok so I decided to post some code so that people can get a better idea of what I am trying to do. Right now, the only problems I am having is getting the object to roll when facing to x axis. For some reason it pitches up and down instead of rolling left and right. I am also still working on moving the object corrrectly on the up/down vector. By the way, this code is an adaptation of Zotoaster's simple vehicle physics tutorial. So if you are out there Zotoaster any help you could give would be appreciated.

Sync on : Sync rate 60
Set Display mode 1024,768,16 : Hide Mouse
Autocam off : Set Camera Range 1,0x7fffffff

`Setup Types
Type Vector
X as Float
Y as Float
Z as Float
Endtype


`--------------------
` **Setup Player**
`--------------------
` —Variables
Global MDir as Vector `Direction of movement
Global Vel as Vector `Velocity
Global Acc as Vector `Acceleration
Global lForce as Vector
Global rForce as Vector
Global uForce as Vector
Global dForce as Vector
Global fForce as Vector
Global bForce as Vector
Global Force as Vector `Force
Global Drag as Vector `Drag
Global Traction as Vector `Traction
Global AngForce as Vector `Angular force
Global AngAcc as Vector `Angular Acceleration
Global AngVel as Vector `Angular Velocity
Global Angle as Vector `Angle
Global Pos as Vector `Position
Global Speed as Float `Speed

` —Constants
Global EngForce `Engine Force
Global Mass as Float `Mass
Global CDrag as Float `Constant Drag
Global CTraction as Float `Constant Traction
Global ATraction as Float `Angular Constant Traction


`---------------------
` **Configuration**
`---------------------
EngForce=1
Mass=50
CDrag=0.000000000000005
CTraction=.9999
ATraction=.98


`--------------
` **Create**
`--------------
`Create Player Vehicle
Make Object Box 1,100,50,70

`Create World
Make Matrix 1,10000,10000,50,50


`----------------
` ))Main Loop((
`----------------
Do


`----------------
` **Controls**
`----------------
If keystate(2)=1 then: forward=1 else forward=0
If keystate(3)=1 then: reverse=1 else reverse=0
If Leftkey()=1 then: left=1 else left=0
If Rightkey()=1 then: right=1 else right=0
If keystate(30)=1 then: mright=1 else mright=0
if keystate(32)=1 then: mleft=1 else mleft=0
if keystate(31)=1 then: up=1 else up=0
if keystate(17)=1 then: down=1 else down=0

`Apply Forces When Controls Are Pressed
if down=1
dForce.X=sin(Angle.Z)+Drag.X
dForce.Y=-cos(Angle.Z)*-cos(Angle.X)+Drag.Y
dForce.Z=-sin(Angle.X)+Drag.Z
else
dForce.X=0
dForce.Y=0
dForce.Z=0
endif
if up=1
uForce.X=sin(Angle.Z)+Drag.X
uForce.Y=cos(Angle.Z)*cos(Angle.X)+Drag.Y
uForce.Z=-sin(Angle.X)+Drag.Z
else
uForce.X=0
uForce.Y=0
uForce.Z=0
endif
if mleft=1
lForce.X=-cos(Angle.Y)+Drag.X
lForce.Y=1-cos(Angle.X)*1-sin(Angle.Z)*.5+Drag.Y
lForce.Z=sin(Angle.Y)+Drag.Z
else
lForce.X=0
lForce.Y=0
lForce.Z=0
endif
if mright=1
rForce.X=cos(Angle.Y)+Drag.X
rForce.Y=1-cos(Angle.X)*1-sin(Angle.Z)*.5+Drag.Y
rForce.Z=-sin(Angle.Y)+Drag.x
else
rForce.X=0
rForce.Y=0
rForce.Z=0
endif
if forward=1
fForce.X=Traction.X+Drag.X
fForce.Y=Traction.Y+Drag.Y
fForce.Z=Traction.Z+Drag.Z
else
fForce.X=0
fForce.Y=0
fForce.Z=0
endif
if reverse=1
bForce.X=-Traction.X+Drag.X
bForce.Y=-Traction.Y+Drag.Y
bForce.Z=-Traction.Z+Drag.Z
else
bForce.X=0
bForce.Y=0
bForce.Z=0
endif
Force.X=fForce.X+bForce.X+lForce.X+rForce.X-dForce.X+uForce.X
Force.Y=fForce.Y+bForce.Y-rForce.Y+lForce.Y+uForce.Y-dForce.Y
Force.Z=fForce.Z+bForce.Z+lForce.Z+rForce.Z+dForce.Z-uForce.Z
If Left=1 Then AngForce.Y=-(Speed/5+.5)
If Right=1 then AngForce.Y=(Speed/5+.5)
If Left=0 and Right=0 then AngForce.Y=0
if upkey()=1 then AngForce.X=(Speed/5+.5)
if downkey()=1 then AngForce.X=-(Speed/5+.5)
IF upkey()=0 and downkey()=0 then AngForce.X=0
If keystate(16)=1 then: AngForce.Z=-(Speed/5+.5)
If keystate(18)=1 then: AngForce.Z=(Speed/5+.5)
if keystate(16)=0 and keystate(18)=0 then AngForce.Z=0

`---------------
` **Physics**
`---------------
`Direction
MDir.X = Sin(Angle.Y) * Cos(Angle.X)
MDir.Y = -Sin(Angle.X)
MDir.Z = Cos(Angle.Y) * Cos(Angle.X)

`Forces
Traction.X=MDir.X * EngForce
Traction.Y=MDir.Y * EngForce
Traction.Z=MDir.Z * EngForce
Drag.X=-CDrag * Vel.X * Speed
Drag.Y=-CDrag * Vel.Y * Speed
Drag.Z=-CDrag * Vel.Z * Speed

`Movement
Acc.X = Force.X / Mass
Acc.Y = Force.Y / Mass
Acc.Z = Force.Z / Mass
Vel.X = (Vel.X + Acc.X) * CTraction
Vel.Y = (Vel.Y + Acc.Y) * CTraction
Vel.Z = (Vel.Z + Acc.Z) * CTraction
Speed=SQRT( Vel.X^2 + Vel.Y^2 + Vel.Z^2)

`Angles
AngAcc.X = AngForce.X / Mass
AngAcc.Y = AngForce.Y / Mass
AngAcc.Z = AngForce.Z / Mass
AngVel.X = (AngVel.X + AngAcc.X) * ATraction
AngVel.Y = (AngVel.Y + AngAcc.Y) * ATraction
AngVel.Z = (AngVel.Z + AngAcc.Z) * ATraction

`Apply Physics To Player
Pos.X=Pos.X+Vel.X
Pos.Y=Pos.Y+Vel.Y
Pos.Z=Pos.Z+Vel.Z
Angle.X = Wrapvalue( Angle.X + AngVel.X )
Angle.Y = Wrapvalue( Angle.Y + AngVel.Y )
Angle.Z = Wrapvalue( Angle.Z + AngVel.Z )

`Update player
Position Object 1, Pos.X, Pos.Y, Pos.Z
Rotate object 1, Angle.X, Angle.Y, Angle.Z

`--------------
` **Camera**
`--------------
ca# = curveangle(Angle.Y,ca#,20.0)
cx#=pos.x-Sin(ca#)*600
cy#=pos.y+150
cz#=pos.z-Cos(ca#)*600
position camera cx#,cy#,cz#
point camera pos.x,pos.y,pos.z
text 10,10,"use the number 1 and 2 keys to move forward and backward"
text 10,20,"use the up and down keys to change pitch"
text 10,30,"use the left and right keys to change yaw"
text 10,40,"use the A and D keys to move right and left"
text 10,50,"use the W and S keys to move down and up"
text 10,60,"use Q and E keys to chage to roll"
text 10,70,"Notice what happens when you roll the object left or right and use the A or D key."
text 10,80, "This is the same effect I want to achive with the W and S keys when the object pitches up or down."

Sync
Loop
Sorry I am new to the forum and I don't know how to make the code snippet boxes. If any of the forum administators could fix this for me i would appreciate it.
Serial Velocity
16
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 7th May 2010 22:27
KISTechs solution is probably the best, just simply create a dummy object, rotate it to the ships orientation, position it at ( 0, 0, 0 ), move it forward 1 unit and the dummy objects position is your forward vector. To get say the right vector (to manoeuvre the ship right), you would move the dummy object right instead. Hope that made sense .

Serge Adjo
18
Years of Service
User Offline
Joined: 3rd Aug 2006
Location:
Posted: 7th May 2010 22:56
@Spectre 117 if you try to move the spaceship up you can use this:


If you want the ship to strafe use Newxvalue() and Newzvalue() ....

-hope that helps, Serge.

Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 8th May 2010 00:43
Ok so the dummy object seems to be the popular way of doing this. So I was wondering if someone could post some code or a link and let to an example of using a dummy object.
Serial Velocity
16
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 8th May 2010 13:21 Edited at: 8th May 2010 13:35
This is an old example I made for someone else ages ago, I didn't over comment it but I hope its easy to follow:



This code will work regardless of how you rotate the object, so you can use the Turn, Pitch and Roll commands with it.

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th May 2010 23:29
It's primarily the popular method because it's easy to use, and requires fewer calculations. If you're doing it to learn the calculations then that's another story, but to save CPU cycles the dummy object method works quite well.

Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 9th May 2010 19:17
Thanks for your help everyone. The dummy object method is much easier to understand and implement than all the trig I was trying to do. Now the only problem I am having is getting the camera to follow the object correctly, but I am pretty sure that I can handle that myself.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th May 2010 18:05
Quote: "Now the only problem I am having is getting the camera to follow the object correctly"


Number 17 here might save you a little time:

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

I think you might also need the Set Camera To Object Orientation command to go with it - or possibly rotate the camera to suit afterwards.

TDK

Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 11th May 2010 19:14 Edited at: 11th May 2010 22:17
TDK, thanks for the reply, and the tutorials. I have actually read them already. Unfortunately, the "set camera to follow" command is not really the effect that I am trying to achieve. It is similar to the effect that I want, but I want the camera to change angle when the object pitches up or down and rolls right or left. I know that I can use the roll, pitch, and turn camera commands to do that, but I also want position the camera slightly above the object. So when I use the roll, pitch, and turn camera commands with the camera positioned above the object the camera doesn't stay behind the object correctly. So if anyone has a recommendation I would appreciate it.

Login to post a reply

Server time is: 2024-11-24 15:24:38
Your offset time is: 2024-11-24 15:24:38