Simple movement code

Hi, i´m making a program in wich, while the program is running, a box model should be moving up in the Z axis.
This is the code i have:

#include “pandaFramework.h”
#include “pandaSystem.h”

PandaFramework framework;

int main(int argc, char *argv[]) {
framework.open_framework(argc, argv);
framework.set_window_title(“Solar”);
WindowFramework *window = framework.open_window();

NodePath b = window->load_model(framework.get_models(), “models/box”);
int altura;
altura = 1 ;
altura = altura+1;
b.reparent_to(window->get_render());
b.set_scale(5.25, 5.25, 5.25);
b.set_pos(-8, 42, altura);
framework.main_loop();
Thread *current_thread = Thread::get_current_thread();
while(framework.do_frame(current_thread));
framework.close_framework();
return (0);
}

Basically all i want to do is the box model to move to the "altura"int´s value. And that int to have a progressive value. Any ideas of what to do ?

Hi

Well, it looks like you have two main_loops; you should only do one or the other of
framework.main_loop();
or
while(framework.do_frame(current_thread));

framework.main_loop() actually does the while loop.

So try removing the call to framework.main_loop() and do something like:

int altura;
altura = 1 ;

b.reparent_to(window->get_render());
b.set_scale(5.25, 5.25, 5.25);

Thread *current_thread = Thread::get_current_thread();
while(framework.do_frame(current_thread)){
  b.set_pos(-8, 42, altura);
  altura = altura+.01;
}
framework.close_framework();

You should add a little more control, since the box will go zooming up and out of your view, so set up a camera somewhere up higher and looking at the initial position of the box, and maybe choose a very small step size like altura = altura + .01;

All that I see with my code (With the variable set o .01) is the box model standing in the center of the screen, no movement. Also I have no idea of how to setup a camera. What can I do ?

Hi there

here are some updates to your program for camera and light. I do see the model move up the window.

#include "pandaFramework.h"
#include "pandaSystem.h"
#include "directionalLight.h"

PandaFramework framework;

int main(int argc, char *argv[]) {

  framework.open_framework(argc, argv);
  framework.set_window_title("Solar");
  WindowFramework *window = framework.open_window();

  NodePath rootNode = window->get_render();

  // get the default camera
  NodePath camera = window->get_camera(0);
  camera.set_pos(0.,0.,10.);
  camera.look_at(-8, 42,1);

  //place a light to make things look nice
  PT(DirectionalLight) d_light = new DirectionalLight("Directional");
  NodePath dlnp = rootNode.attach_new_node(d_light);
  dlnp.set_hpr(-45.,-45.,0.);
  rootNode.set_light(dlnp);

  NodePath b = window->load_model(framework.get_models(), "models/box");

  float altura = 1.;

  b.set_scale(5.25, 5.25, 5.25);

  Thread *current_thread = Thread::get_current_thread();
  while(framework.do_frame(current_thread)){
    b.set_pos(-8., 42., altura);
    altura = altura+.01;
  }
  framework.close_framework(); 

  return (0);
} 

I´m using that code and i can´t see anything but a gray screen :frowning:

oops, I’m sorry

I forgot to put b.reparent_to(rootNode); after the load_model. I didn’t use the box model at first, I used my own model and wrote the load_model line a little differently where I didn’t need to have reparent_to, and so when I put back the load_model the way you had it I forgot to add back the reparent_to.

here’s the code again:

#include "pandaFramework.h"
#include "pandaSystem.h"
#include "directionalLight.h"

PandaFramework framework;

int main(int argc, char *argv[]) {

  framework.open_framework(argc, argv);
  framework.set_window_title("Solar");
  WindowFramework *window = framework.open_window();

  NodePath rootNode = window->get_render();

  // get the default camera
  NodePath camera = window->get_camera(0);
  camera.set_pos(0.,0.,10.);
  camera.look_at(-8, 42,1);

  //place a light to make things look nice
  PT(DirectionalLight) d_light = new DirectionalLight("Directional");
  NodePath dlnp = rootNode.attach_new_node(d_light);
  dlnp.set_hpr(-45.,-45.,0.);
  rootNode.set_light(dlnp);

  NodePath b = window->load_model(framework.get_models(), "models/box");
  b.reparent_to(rootNode);

  float altura = 1.;

  b.set_scale(5.25, 5.25, 5.25);

  Thread *current_thread = Thread::get_current_thread();
  while(framework.do_frame(current_thread)){
    b.set_pos(-8., 42., altura);
    altura = altura+.01;
  }
  framework.close_framework(); 

  return (0);
} 

Wow, thanks this helps me a lot