Moving Objects & Printing Vectors



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/02/2011 at 13:02, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   12 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    Question #1

    I'm trying to do a simple incremental object move just in the X direction. But I'm not getting the expected results.
    Here's my code:

      
    BaseObject *op = doc->GetActiveObject();  
    Vector pos = op->GetRelPos();  
    while(pos.x < 200)  
     {  
    pos.x = ++pos.x;   
    op->SetRelPos(pos.x);   
    EventAdd();  
     }
    

    This works as expected in Coffee. But in C++ the object moves in all three directions even though I've told it to only move in the X axis direction.

    Question#2

    How do you print all three vector values to the console?
    Vector values = op->GetRelPos();
    GePrint(VectorToString(value)) // No such type conversion available?

    Sorry for the really newb questions. But I'm new to the C++ API and I'm trying my best to convert from Coffee.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/02/2011 at 18:44, xxxxxxxx wrote:

    I managed to figure out how to move things in just one axis.

      
    BaseObject *op = doc->GetActiveObject();  
    Vector pos = op->GetRelPos().x;  
    while(pos.x < 200)  
     {  
    pos.x = ++pos.x;   
    op->SetRelPos(Vector(pos.x,0,0));   
    EventAdd();  
     }
    

    But I'm still stuck on the printing of the vectors.
    I know how to print the vectors separately using: GePrint(RealToString(pos.x));
    But in Coffee you can write Println(pos) and it will print all three vectors. And I can't find the C++ equivalent of that.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 06:38, xxxxxxxx wrote:

    You have to print each vector component seperatly and concatenate them into one string.

    GePrint(RealToString(pos.x)+" "+RealToString(pos.y)+" "+RealToString(pos.z));
    

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 08:20, xxxxxxxx wrote:

    Howdy,

    That's the beauty of C++. You can create a "VectorToString()" function yourself and reuse it.

    For example create a header file called "MyDebug.h":

    #idndef _MYDEBUG_H_
    #define _MYDEBUG_H_
      
    #include "c4d.h"
      
    String VectorToString(Vector v);
      
    #endif
    

    ... and a source file called "MyDebug.cpp":

    #include "MyDebug.h"
      
    String VectorToString(Vector v)
    {
        return RealToString(pos.x)+", "+RealToString(pos.y)+", "+RealToString(pos.z);
    }
    

    Then in your project's source code files where you want to print a vector simply add the line:

    #include "MyDebug.h"
    

    ... at the top, and add the "MyDebug.cpp" file to your project. 😉

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 08:30, xxxxxxxx wrote:

    Thanks a lot guys.
    That's very helpful.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 09:19, xxxxxxxx wrote:

      
    String VectorToString(Vector v)
    {
        return RealToString(v.x)+", "+RealToString(v.y)+", "+RealToString(v.z);
    }
    

    I think you'd want to return the vector argument that was passed to the funtion though?    do this by replacing the pos.x with v.x    etc.
     
    Unless I misunderstand what you are doing.  :)



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 09:28, xxxxxxxx wrote:

    I think Dan implied that with his example. At least that's how I interpreted it.
    I know enough about methods that you construct them with generic parameters( like v.z, instead of pos.z). Then substitute them later on in the execution code.

    But thanks for looking out for me.🍺

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 09:30, xxxxxxxx wrote:

    Howdy,

    OH, DOH! you're right. I just saw the typo. Embarrassed

    Edit:
    Those are the perils of "copy/paste" when writing code. LOL

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 09:37, xxxxxxxx wrote:

    Glad it makes sense.  Didn't mean to insult anyone.  :)  I just know from when I first started that when you are learning C++ you rely a lot on the code examples people give..  Dan has always been very helpful with giving examples to the beginners.  :)    Thanks for that Dan!  :)



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 09:39, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    Howdy,

    OH, DOH! you're right. I just saw the typo. Embarrassed

    Edit:
    Those are the perils of "copy/paste" when writing code. LOL

    Adios,
    Cactus Dan

    LOL.. been there!



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 10:12, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    Glad it makes sense.  Didn't mean to insult anyone.  :)  I just know from when I first started that when you are learning C++ you rely a lot on the code examples people give..  Dan has always been very helpful with giving examples to the beginners.  :)    Thanks for that Dan!  :)

    You have no idea what I know, and don't know. So please don't feel bad. And please continue to spell things out as if I don't know anything at all. Because many times I don't. 🙂
    You're questions and code snippets made all the difference to me in getting started with the C++ API.

    Plus. These coding forums aren't private conversations between a few guys.
    There's a lot of lurkers that scan through them (like me). And spelling things out, even to people you think know better, is always a good idea for the other members.
    That's just MHO.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 13:43, xxxxxxxx wrote:

    And to complete this, for speed purposes you would rather pass by reference ;)

    String VectorToString(const Vector &v)
    
        return RealToString(v.x)+", "+RealToString(v.y)+", "+RealToString(v.z);
    
    }
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 13:59, xxxxxxxx wrote:

    Howdy,

    Ah, thanks for the tip. 😉

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/02/2011 at 15:57, xxxxxxxx wrote:

    Thanks Katachi,

    Out of all the things in programming. Pointers, reference operators, and dereference operators are the only things that really scare and confuse me.
    I understand how they work in theory. But in practice they just confuse the heck out of me.
    I can hide from them in Coffee, C#, Java, etc. But the C4D API is absolutely loaded with them. And that's what took me so long to get to the point of even doing the basics with it.

    I invent silly little rules to deal with my ignorance about them.
    Like if I need to create something new for example. Then I need to use one of the API classes.
    And if I use an API class. Then I have to construct it with a pointer *variable.
    So even if I don't really have a strong grasp of pointers. I can still use the API.

    If I didn't do these kinds of silly things in my head. I wouldn't even be able to create a stupid simple cube with the C++ API. 😵

    -ScottA


Log in to reply