Jump to content

[3.0 / C++] Set Angle


gamecreator
 Share

Recommended Posts

Seems like a silly thing to ask but how do you set the angle of a rigid, convex hull physics body?

 

I load it like so:

 

levelmodel=Model::Load("Models/levelbase.mdl");
Shape* shape;
shape=Shape::Load("Models/levelbase.phy");
levelmodel->SetShape(shape);
shape->Release();
levelmodel->SetPhysicsMode(Entity::RigidBodyPhysics);
levelmodel->SetMass(1000);
levelmodel->SetCollisionType(1);

 

And tried this in loop:

 

levelmodel->PhysicsSetPosition(0,0,0,1.0f);

if(window->KeyDown(Key:))
levelmodel->PhysicsSetRotation(0,0,-45,0.003f);

if(window->KeyDown(Key:))
levelmodel->PhysicsSetRotation(0,0,45,0.003f);

 

But that code acts unpredictably. It keeps rotating or slows or doesn't move depending on when I press the button. I changed the last value to 1, to 0 and anything in between. I'd like the angle to be set instantly.

 

It seems like I'm using the wrong function but I need a physics function to do this and there seem to be only two (the other sets position).

Link to comment
Share on other sites

Yes, it acts strangely there too. But I would think that in either case it should just go to the angle and stop (my project would constantly be giving it a changing angle to go to). Could anyone please take a look at what I could be doing wrong (it's only a few lines of code)? I've uploaded the project here:

http://www.mediafire.com/download/b7ma4md5a7vc2rx/TiltIt.zip

 

Thank you.

 

Here's the entire App.cpp

#include "App.h"

using namespace Leadwerks;

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL), levelmodel(NULL), ballmodel(NULL) {}

App::~App() { delete world; delete window; }

Model* box;
Shape* shape;

Vec3 camerarotation;
#if defined (PLATFORM_WINDOWS) || defined (PLATFORM_MACOS)
bool freelookmode=true;
#else
bool freelookmode=false;
#endif

bool App::Start()
{
   //Create a window
   window = Window::Create("TiltIt",0,0,1280,720);

   //Create a context
   context = Context::Create(window);

   //Create a world
   world = World::Create();

   Font* font = Font::Load("Fonts/Arial.ttf",24);
   context->SetFont(font);
   font->Release();

   //Create a camera
   camera = Camera::Create();
   camera->Move(0,7,-5);
   camera->SetRotation(60,0,0);

   //Hide the mouse cursor
   window->HideMouse();

   levelmodel=Model::Load("Models/levelbase.mdl");
   Shape* shape;
   shape=Shape::Load("Models/levelbase.phy");
   levelmodel->SetShape(shape);
   shape->Release();
   levelmodel->SetPhysicsMode(Entity::RigidBodyPhysics);
   levelmodel->SetMass(1000);
   levelmodel->SetCollisionType(1);

   ballmodel=Model::Load("Models/ball.mdl");
   shape=Shape::Load("Models/ball.phy");
   ballmodel->SetShape(shape);
   ballmodel->SetPosition(0,3,0);
   ballmodel->SetPhysicsMode(Entity::RigidBodyPhysics);
   ballmodel->SetCollisionType(1);
   ballmodel->SetMass(1);

   Light* light = DirectionalLight::Create();
   light->SetRotation(35,35,0);

   return true;
}

bool App::Loop()
{
   if(window->Closed() || window->KeyHit(Key::Escape)) return false;

   //  Just for testing purposes
   if(window->KeyHit(Key::Space))
   {
       box = Model::Sphere(32);
       shape = Shape::Sphere();
       box->SetShape(shape);
       box->SetPosition(-0.2,5,0);
       box->SetMass(1);
       box->SetCollisionType(1);
   }

   levelmodel->PhysicsSetPosition(0,0,0,1.0f);

   if(window->KeyHit(Key:))
       levelmodel->PhysicsSetRotation(0,0,-45,0.9f);

   if(window->KeyHit(Key:))
       levelmodel->PhysicsSetRotation(0,0,45,0.001f);

   Time::Update();
   world->Update();
   world->Render();

   context->SetBlendMode(Blend::Alpha);
   context->SetColor(1,1,1);
   context->DrawText("Z Angle: "+String(levelmodel->GetRotation().z),10,10);
   context->SetBlendMode(Blend::Solid);

   context->Sync(false);

   return true;
}

Link to comment
Share on other sites

from the looks of it you are dealing with quaternion not rotation.

 

I'm sorry I can't say anything in Leadwerks as I haven't tried it yet but in Unity 3D.

 

we have Quaternion AND Rotation

 

rotation is Vector3(x, y, z);

Quaternion is Quaternion(x,y,z,w);

I do not understand Quaternion my self but this 2 are totally different things.

 

I suggest you try finding another method witch will have Vector3 Degrees and will work the way you want it to.

 

in Unity 3D we would deal it like this:

 

Transform ItemObject = transform;
ItemObject.localEulerAngles = new Vector3(0,45,0);

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

Figured it out. You have to keep feeding it every frame, or at least until you stop on the angle you want, as it won't stop there automatically. This is a lot different than just SetRotation itself, which is instant.

 

if(window->KeyDown(Key:)) rot-=0.1*Time::GetSpeed();
if(window->KeyDown(Key:)) rot+=0.1*Time::GetSpeed();

levelmodel->PhysicsSetPosition(0,0,0,1.0f);
levelmodel->PhysicsSetRotation(0,0,rot,0.0001f);

 

Oddly, the documentation says the strength (last parameter) should be 1 or close to it, yet that's the value that barely moves it. The smaller the value the faster it moves. Not sure if that's a documentation error.

Link to comment
Share on other sites

If I understand right from another post, SetRotation doesn't do physics calculations like SetPhysicsRotation. If you use the first, your ball will fly off or fall through your floor or something else you don't want.

 

sooo if object is little bit through floor than it'll go through floor physics won't bounce it back a little?

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

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.

Guest
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.

 Share

×
×
  • Create New...