Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4076448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:25:35+00:00 2026-05-20T17:25:35+00:00

I am working with some code to create a triangle that moves with arrow

  • 0

I am working with some code to create a triangle that moves with arrow keys. I want to create a second object that moves independently. This is where I am having trouble, I have created the second actor, but cannot get it to move. There is too much code to post it all so I will just post a little and see if anyone can help at all.

ogl_test.cpp

#include "platform.h"
#include "srt/scheduler.h"

#include "model.h"
#include "controller.h"

#include "model_module.h"
#include "graphics_module.h"

class blob : public actor {
public:
    blob(float x, float y) : actor(math::vector2f(x, y)) { }

    void render() {
        transform();

        glBegin(GL_TRIANGLES);
            glVertex3f(0.25f, 0.0f, -5.0f);
            glVertex3f(-.5f, 0.25f, -5.0f);
            glVertex3f(-.5f, -0.25f, -5.0f);
        glEnd();
        end_transform();
    }
    void update(controller& c, float dt) {
        if (c.left_key) {
            rho += pi / 9.0f * dt;
            c.left_key = false;
        }
        if (c.right_key) {
            rho -= pi / 9.0f * dt;
            c.right_key = false;
        }
        if (c.up_key) {
            v += .1f * dt;
            c.up_key = false;
        }
        if (c.down_key) {
            v -= .1f * dt;
            if (v < 0.0) { v = 0.0; }
            c.down_key = false;
        }
        actor::update(c, dt);
    }
};

class enemyOne : public actor {
public:
    enemyOne(float x, float y) : actor(math::vector2f(x, y)) { }

    void render() {
        transform();

        glBegin(GL_TRIANGLES);
            glVertex3f(0.25f, 0.0f, -5.0f);
            glVertex3f(-.5f, 0.25f, -5.0f);
            glVertex3f(-.5f, -0.25f, -5.0f);
        glEnd();
        end_transform();
    }
    void update(controller& c, float dt) {
        if (c.left_key) {
            rho += pi / 9.0f * dt;
            c.left_key = false;
        }
        if (c.right_key) {
            rho -= pi / 9.0f * dt;
            c.right_key = false;
        }
        if (c.up_key) {
            v += .1f * dt;
            c.up_key = false;
        }
        if (c.down_key) {
            v -= .1f * dt;
            if (v < 0.0) { v = 0.0; }
            c.down_key = false;
        }
        actor::update(c, dt);
    }
};

int APIENTRY WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    char* lpCmdLine,
    int nCmdShow
)
{

    model m;
    controller control(m);

    srt::scheduler scheduler(33);
    srt::frame* model_frame = new srt::frame(scheduler.timer(), 0, 1, 2);
    srt::frame* render_frame = new srt::frame(scheduler.timer(), 1, 1, 2);
    model_frame->add(new model_module(m, control));

    render_frame->add(new graphics_module(m));

    scheduler.add(model_frame);
    scheduler.add(render_frame);

    blob* prime = new blob(0.0f, 0.0f);
    m.add(prime);
    m.set_prime(prime);

    enemyOne* primeTwo = new enemyOne(2.0f, 0.0f);
    m.add(primeTwo);
    m.set_prime(primeTwo);

    scheduler.start();
    control.start();

    return 0;
}

model.h

#include <vector>
#include "vec.h"

const double pi = 3.14159265358979323;

class controller;

using math::vector2f;

class actor {
public:
    vector2f P;
    float theta;
    float v;
    float rho;

    actor(const vector2f& init_location) :
        P(init_location),
        rho(0.0),
        v(0.0),
        theta(0.0)
    { }
    virtual void render() = 0;
    virtual void update(controller&, float dt) {
        float v1 = v;
        float theta1 = theta + rho * dt;
        vector2f P1 = P + v1 * vector2f(cos(theta1), sin(theta1));
        if (P1.x < -4.5f || P1.x > 4.5f) { P1.x = -P1.x; }
        if (P1.y < -4.5f || P1.y > 4.5f) { P1.y = -P1.y; }
        v = v1;
        theta = theta1;
        P = P1;
    }

protected:
    void transform() {
        glPushMatrix();
        glTranslatef(P.x, P.y, 0.0f);
        glRotatef(theta * 180.0f / pi, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis
    }
    void end_transform() {
        glPopMatrix();
    }
};

class model {
private:
    typedef std::vector<actor*> actor_vector;
    actor_vector actors;
public:
    actor* _prime;

    model() { }

    void add(actor* a) {
        actors.push_back(a);
    }
    void set_prime(actor* a) {
        _prime = a;
    }
    void update(controller& control, float dt) {
        for (actor_vector::iterator i = actors.begin(); i != actors.end(); ++i) {
            (*i)->update(control, dt);
        }
    }
    void render() {
        for (actor_vector::iterator i = actors.begin(); i != actors.end(); ++i) {
            (*i)->render();
        }
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:25:36+00:00Added an answer on May 20, 2026 at 5:25 pm

    Your blob is erasing the keypresses before the second actor sees them:

    if (c.left_key) {
       rho += pi / 9.0f * dt;
       c.left_key = false; // <-- HERE
    }
    

    Clearing the keyboard state after each frame should not be the concern of any single actor, the controller itself should do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some Ruby code that needs to create primary keys on existing
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm working with some code that is confusing me and I'm wondering if I'm
I was working on some code recently and came across a method that had
I am working on some code coverage for my applications. Now, I know that
I'm working with some code that widely uses the idiom of returning a pointer
I'm having problems with some code to create a CDialog based window. The code
I am working on some code that scrapes a page for two css classes
I'm working on some code where I have a Time object with a member
I've been reading up on .NET Threading and was working on some code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.