Jump to content

Object to follow direction of travel

Richard C

Recommended Posts

Help again please.

With a lot of help from Wingnut I have a mesh that is joystick being used to control the movement of an object (submarine in this case). The sub moves around based on the rotation of the joystick but isn't facing the direction of travel thus yawing across the screen rather than turning to face the direction of travel.

Any pointers to set me off in the right direction (pun intended) would be gratefully received.


Richard C 


Link to comment
Share on other sites

:)  http://playground.babylonjs.com/#2JXSKY#4

You just need to stretch your imagination, RC... and change camera to sub.

Way at the top of Adam's #14 demo... are two "overload" functions.  Overloading is term for adding extra functionality to an "established" class of object.  All BJS mesh are subClasses of Mesh, which are subClasses of AbstractMesh, which are subClasses of Node.  Those two funcs... and the way they are defined... cause ALL mesh to have two new functions... getDirection and getDirectionToRef.  Cool, huh?

Sidenote:  Cameras and lights are ALSO subClasses of Node.  That is why we used the same overloads... for Adam's camera-based usage, AND our mesh-based usage.

So, you can see those two functions added to our original joystick demo... at lines 125-135.

Down in the renderLoop...

Line 146  - I set a slow rotation on the sub... just for testing.
Lines 148-151  - establish some helpers - calling our new sub.getDirection() ... which also calls its friend... sub.getDirectionToRef()
Lines 154-155  - Here we apply our beloved xspeed and zspeed, but in a more-advanced and wiser way.

As you can see, as the sub rotates around, its forward, backward, left, and right... all act correctly, yes?  They take the sub's rotation into consideration.

The code lines for submarine positioning AFTER sub pitch-up and pitch-down are there, but remarked-out.  I don't know if your sub is allowed to pitch the nose up/down.

This should get you rolling... and let it be known that I could not have built this new joystick demo... without Adam's help.  He showed us a fine demo... but you were just a little scared to convert it from camera... to submarine.  It's ok, we'll let you live another day.  :D  (I was a little scared, too.)  Thanks Adam.  Hope this helps, RC.

Link to comment
Share on other sites

Sorry to say, but unless I'm missing something more that I ought to be doing but it isn't doing what's necessary. I am struggling to understand the code. The sub isn't rotating in line with the joystick rotation and thus isn't moving as it should. For example, if the joystick is pulled back from the forward position, the sub should turn around to face the opposite way and then follow the direction.

Sorry to sound ungrateful for all your help but I really am struggling.

Thanks so much

Link to comment
Share on other sites

Hmm... you seem to be mixing rotation with position.  Our current joystick demo is a position-setting joystick, not a rotation-setting joystick.  Are you using two joysticks now?

All in all, you've only been "struggling" with this new issue... for about an hour.  Keep experimenting... and think about what you want this joystick to actually do.  Up to this point, you wanted back-pull... to back-up the sub.  Now, suddenly, you want back-pull to turn the sub 180 degrees and throttle "forward" after the turn?  hmm.

Link to comment
Share on other sites

The problem with forums / messaging / chat - so easy to not give a clear explanation of what is required. 

Yes you have summed it up and it was always so except I obviously didn't make that clear. Turn the sub in line with the rotation of the joystick and then / or at the same time throttle forward.

Are you thinking it can't be achieved with one joystick?


Link to comment
Share on other sites

I think it's possible.  Allow me to try to clarify.  We are talking about the in-cockpit joystick, right?

IF sub is aimed north (all stop)... and IF pilot pulls joystick south (toward the pilot) and holds it there...

then sub turns south, and then moves forward... south-bound?

So, a southbound sub... is driven "forward"... by pulling the joystick toward the pilot?

hmm.  Seems strange.  I think I am confused.  :)

Link to comment
Share on other sites

Yes the cockpit JS. I'm back to not having explained things well so let me try to get it right this time!

The sub has two horizontal motors/propellers (thrusters). It also has two vertical thrusters - forget them for now - they control up and down movement via a separate toggle switch.

They are all variable speed thrusters. 

Using compass points for this description rather than nautical terms (port/starboard/aft etc) .........

Pulling the JS back towards the pilot will indeed send the sub backwards - used mainly to manoeuvre and like any marine thruster reversing forward movement bringing the sub to a halt.. 

Pushing the JS away from the pilot sends the sub forward.

So to turn south from north, the JS to the left or to the right turns the sub on it's own footprint as it were and once the sub is facing whatever the pilot wants to navigate to he pushes the JS forward.

The JS to the left operates the left thruster only. JS to the right operates the right thruster only. Pushing JS forward operates both thrusters.

If the pilot wants to 'bear' left rather than turn on the spot he would move the JS to north west. This causes the right thruster to slow and therefore left thruster is rotating more than the right thruster and the sub is moving north west.

Hope that explains things more clearly.

I have been trying pickpoints from the ground plane start and current position - your diff statement. I have declared 3 variables - axis1, axis2 and axis3 as per a number of PG tutorials.  Axis1 = diff, Axis2 = vector3(0,0,0) and axis3 is a cross of axis1 and 2.   sub.rotation = BABYLON.Vector3.RotationFromAxis(axis2, axis3, axis1); But the results are somewhat irregular!

I shall keep trying to perfect it. Unless of course there is a better and easier alternative.

Thanks so much again for your patience.


Link to comment
Share on other sites

ahh, thanks.   Ok, rotate sub (around Y axis) with JS right/left.  Translate (along local Z axis) with JS forward/backward.  I think that can be done.

No such move as "stafe" or "slew"  (side-sliding).  *nod*. 

Xspeed needs to be applied to sub.rotation.y and not sub.position.x.  Yep, yep, yep.  :)  I'll take a look in the morning.  I just finished a pair of busy Halloween weekend gigs with the band... and I need a bowl of rice krispies and some quality snoring-time.  :)  Talk soon, party on. 

Aw heck, let's try one.  :)


Left/right (xspeed) is now applied to sub.rotation.y (line 137).  I attempted a JS forward/backward dead-zone in line 138.  (Allows you to turn left/right in-place, easier.  Lower chance of accidentally translating forward/backward... when attempting left/right rotation in-place.)  ("in-place" == "in footprint")  :)  You probably knew that.  Hope this helps.  Cya tomorrow.

Link to comment
Share on other sites

hehe.  Yeah, did you see the size of those amplifiers?  Yeah, we're quite the smash-mouth mosh-pit welders, eh?  hah.  (not!) 

Before the gig, we all eat/snort Doan's Pills, and wash 'em down with Geritol and Ensure.  :)

We can't LIFT bigger amps.  We would break a hip... or poop our pants.  :D

So, I'm curious about that long PM from you... that got lost.  Sorry I missed that. 

Were you offering me bus tickets to come see the real submarines?  Could I get there by bus?  If ya don't mind me asking, what part of Earth do you hang-out-at?  (thx)

Link to comment
Share on other sites

I am in Oxford, England although born and bred in Wales.

The sub business is in Cairns, Australia

None of these places have introduced me to Doan's Pills, Geritol or Ensure but I imagine in the right combination and enough strength they would have the power to get you anywhere in the world without the aid of car, bus, boat or plane. However you got there, it would be a pleasure and honour to take you diving I one of our little yellow beauties, although passing the medical tests may be a bit of a strain.

I also sent you a short message yesterday - did you get that. I will re-send the first.

Link to comment
Share on other sites


Yep, yesterday's message arrived fine.  I answered in public... here... as you know. 

Heck, it would be MY pleasure and honor to meet you guys and SEE the subs.  No need for me to enter into one.  :)  Perhaps someday you can take us on a little ride... with a video.  I would enjoy seeing that.

Very kind words, RC... I appreciate that.  And thanks for the info about yourself... very cool. 

I live here in the sticks (woodlands)...  a tiny friendly town, 15 miles south of Lake Superior, in an area called the snow belt.  We get lots of "Lake Effect" snow, and it is a bit hilly, here.  So... I am surrounded by snow-skiing facilities/resorts.  I am a professional snow blower operator out of pure postal-service and vehicle-parking necessity.  :)

I feed/train black and gray squirrels as a hobby, and with that activity... it seems every other woodland critter comes to visit, too.  Deer, bear, raccoons, wolves, they all have visited me, and been caught on my webcams.  A red fox and I... had a short conversation the other day... discussing whether or not he/she should be trying to snack from my bluejay-proof squirrel feeder (loaded with Walmart peanuts). 

Mid-winter, squirrel feeding can cost up to $80 per month.  Weird hobby, huh?  In previous years, I placed webcams near mirrors (on my front porch) and have watched how squirrels and birds react when they become self-aware (when they see themselves in the mirrors).  Quite interesting!  (But I am a simpleton, so nearly everything interests me.)  :)  And, I never kill animals if I can avoid it, but... I am still not vegan... so... you know... animals ARE killed to feed me.  Darn.  But if I were a vegan, I'd surely kill plants, and I don't like doing that, either.  :)  I prefer NOT "cropping" plants, but instead, wait for them to "fall" their bounty.  The truck-it-green fruits and vegetables industries don't agree, and prefer to haul the food to the herds (of people) instead of trucking the people to the naturally-fallen foods.

I also don't kill insects... and have studied some of those, too.  I have learned a technique to "befriend" house flies, and get them to come land on the back of my hands, and ride-along with me, as I go about my day.  (Weird!)  (I do that MOSTLY when I'm outdoors, of course)

You just think loving thoughts toward them, then place your finger near them, and keep thinking how much you love them.  The fly will, within 20 seconds... walk over to your finger and taste your skin.  Once they taste the love juice on your skin, they are no longer scared, and will walk around on your skin, licking love juice.  :)   Truth.  It works 90% of the time, and on MANY breeds of flies and other insects.  WEIRD!!!  I think MOST of nature... can smell or taste a human's intent.

In another apartment in Minneapolis, I "ran" an unknown-to-landlord colony of Pharoah ants... and we were all good buddies.  I have a couple of stereoscopic microscopes that I was given... by the local high school... when they upgraded to microscope-cameras and big screen monitors.  Some of my friends call me a bug-hugger.  :)  I am also fascinated by the vast occurrences of Fibonacci number sequences... found in nature... and have studied that.  And I have studied how critters react to music, but have learned very little from that. 

I'm also anti-capitalism... so I guess I am a people-who-hate-competing -hugger, too.  heh.

TMI, eh?  Way more than ANYONE EVER wanted to know about Mr. Wingnut.  :)  RC/anyone... you are always cordially invited to visit Wingy's squirrel farm and snow-blower training facility... anytime.  heh.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...