How to Add Torque

wolfrockwolfrock Posts: 7Member
in 3D

Hey everyone - super newbie here. Any help would be much appreciated. Using the latest Godot stable 3.2.1

I am trying to make a 3d space game in where i have a ship that can fly around. I was able to get movement working (not great)

If I paste wombatstampede's code into my script - it doesn't really work. source for his code - spaceship zip at the end of the post - https://godotforums.org/discussion/comment/19918/#Comment_19918

I can move my ship forward and print to console for each key show up when the button is pressed, but no movement other than forward takes place, so I know my input map appears to be correct.

My original code that "works" (lol i think i get orientation lost since im not using any sort of basis and eventually spin into oblivion) https://pastebin.com/ryMgFQF5 - a user on reddit suggested i use torques which i just cant seem to get working.

His code pasted into my script (commented out things i dont have like animations, and changed z front direction because i was moving backwards instead of forwards. https://pastebin.com/PHC33wDw

His original movement code https://pastebin.com/9wtAfnFT

The rigid body properties seem mostly the same - i cant set my weight and mass independently but the angular velocity and dampening look the same to me. Mine is on the left. His is on the right from his project that works. https://imgur.com/a/O7rCae0

I have put the movement outside to try and get it to go forward and torque up and right but the ship only goes forward

I did fix the weight/mass ratio and then the thrust/turning speeds in the code - so that is a bit out of date. sorry forgot to make that change since this was just largely a copy of my reddit post

Any ideas? Thanks in advance! Happy to provide any additional info.

Best Answer

  • wombatstampedewombatstampede Posts: 175
    edited July 4 Accepted Answer

    Had a look. Took me half an hour to figure out that you assigned a "nil" collision shape to the ship. (Some shape with 0 vertices)

    I assigned an ordinary box shape (for testing) and it worked.

    The physics engine uses the collision shapes to calculate physics. It is the shape for it to work with (it ignores the visible mesh). If there's basically "none" this doesn't seem to work. Think of an object with size 0 (a point). Turning wouldn't change a thing.

    Unrelated:
    I saw that you did some scaling around the space station. Try to avoid that.
    (1) You added the SpaceStation rigid body to some (uneven) scaled parent.
    (2) You scaled the collision shape. (Set the extents/sizes of the collision shape instead and reset scale to 1,1,1)
    Scaling Collision shapes (also via their parents) may lead to very unexpected effects. Especially when scaled uneven (i.e. 1,8,2).

Answers

  • wombatstampedewombatstampede Posts: 175Member

    I don't see any of the attachments in your post.
    Is this already answered on reddit? (Might've been good to add the link)

    I'd say disable (=0) the angular damping to get started. The force values differ quite much here between my and your code.

    An alternative to applying torque is applying two opponent forces to the sides of the rigid body (like a real spacecraft might do). I.e. left side down, right side up. But setting up the global force vectors is slightly more complex.

  • wolfrockwolfrock Posts: 7Member
    edited July 2

    Hi wombat,

    I never figured out why this doesnt work :(

    The reddit post is https://old.reddit.com/r/godot/comments/hf0esu/help_please_rigidbody_movement/

    I copied your movement code in the spaceship.zip to my project - My original paste bin has expired so I re pasted below.

    Your code that works
    https://pastebin.com/SPAs1zfN

    "My" code that only moves forward (lol)
    https://pastebin.com/BFnW2Bid

    Properties of my ship

    https://imgur.com/ortMDTV

    The force values differed originally because I didn't have the project gravity set to 0. I fixed this, was able to set the mass and weight of my ship and was then able to set force to your values but still only get forward movement.

    I tried disabling angular dampening - but still the same behavior.

    I may try applying forces in the way you suggested - although I fear what makes the torque not work, would prevent that from working too.

    Thanks so much for replying

  • wombatstampedewombatstampede Posts: 175Member
    edited July 3

    This is difficult to tell from outside.

    Maybe you've checked one (or all) of the Axis-Lock properties (usually z) so that the object is prevented to roll.

    Also keep in mind that if you keep the main scene and the spaceship in different scene files (which I do also in my example) you will have to check the properties in the main scene because the rigid body properties can be overriden in the parent scene.

    Theoretically you could also be editing the wrong script. I.e. the ships script is embedded but you edit a *.gd script thinking it is used. But that would be obvious when none of the changes shows results.

    Also some code from outside (parent) or collision could force the ship/rigid body to stay level.

  • wolfrockwolfrock Posts: 7Member
    edited July 3

    Hi Wombat,

    Just wanted to say I really appreciate the time you've taken to reply.

    I am using my ship instanced in my spatial level scene - however the ship scene has no lock properties. (all are unchecked)

    In the main scene - what properties would cause this? My main level scene is just a spatial object and I only see transform /rotation type information - nothing I can see that would lock or stop my object from moving.

    For script verification / input verification - I did add under the key presses to output print console commands when I press keys - and that successfully works showing the key presses.- just no rigid body movement other than forward.

    I thought maybe my camera child object was causing - so i set it up like yours but that doesnt follow my character and didnt work haha

    I dont have any code on my main scene - my only script is the instanced ship and its script.

    If you (or anyone really) wants to or has time to take a closer look, I've zipped up my project available at the link below.

    https://github.com/ddubois89/brokencode

    The scene res://dev/devlevel.tscn is what I've been working in with res://dev/devship.tscn using ship_control.gd

    Edit: Just as a side note, I realized I forgot the attach the script to the camera - not really concerned about that - now attached it centers on my player object but still doesn't follow. My main concern was getting movement before camera.

  • wombatstampedewombatstampede Posts: 175Member
    edited July 4 Accepted Answer

    Had a look. Took me half an hour to figure out that you assigned a "nil" collision shape to the ship. (Some shape with 0 vertices)

    I assigned an ordinary box shape (for testing) and it worked.

    The physics engine uses the collision shapes to calculate physics. It is the shape for it to work with (it ignores the visible mesh). If there's basically "none" this doesn't seem to work. Think of an object with size 0 (a point). Turning wouldn't change a thing.

    Unrelated:
    I saw that you did some scaling around the space station. Try to avoid that.
    (1) You added the SpaceStation rigid body to some (uneven) scaled parent.
    (2) You scaled the collision shape. (Set the extents/sizes of the collision shape instead and reset scale to 1,1,1)
    Scaling Collision shapes (also via their parents) may lead to very unexpected effects. Especially when scaled uneven (i.e. 1,8,2).

  • wolfrockwolfrock Posts: 7Member

    Hi Wombat,

    Thanks so much for figuring it out - I'm not sure how that nil collision shape was set - I originally was using some stuff I imported from blender, so maybe that was it.

    Thanks also for the info on scaling - the collision shape scale i think i had fixed (just not in the version i gave you)

    I have so much to learn lol. I really appreciate the help with this - it was driving me nuts

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file