Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Mar 2002
Offline Last Active Today, 02:32 PM

#5135179 code -> .data & data -> .code

Posted by Álvaro on 27 February 2014 - 03:23 PM

Other than hearing this, have you also read it somewhere on the interwebs? Perhaps you can post a link to such a claim, so we can know exactly what you are talking about?

#5135174 Vectors Newb

Posted by Álvaro on 27 February 2014 - 03:06 PM

Your loop never ends because in each step you only go 1/110th of the way to the target. Is your character's name "Zeno"? :)


You shouldn't run a special loop when the mouse is pressed. If you do (even if you get it to work), your program won't respond to "quit" events until the character makes it to its target, for instance.


The idea is that there is only one loop (the event loop), and when you detect a mouse button has been pressed you set up a plan for your character to move a particular way (for instance by setting some variables). Then each time the loop runs you execute part of the plan.


I don't know much about Python, so sorry if I write something terribly wrong:


    def update(self, mlocX, mlocY, n_steps):
        self.rect.x += (mlocX - self.rect.x) / n_steps
        self.rect.y += (mlocY - self.rect.y) / n_steps

running = True

while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mlocX, mlocY = pygame.mouse.get_pos()
            n_steps = 110
        screen.blit(terrain, terrainRect)
        oPC.update(mlocX, mlocY, n_steps)
        if n_steps > 1:
            n_steps -= 1

#5134852 Vectors Newb

Posted by Álvaro on 26 February 2014 - 03:18 PM

If you want to arrive there in 10 steps, compute the vector that goes from (2,0) to (33,33), which is just (33-2, 33-0) = (31,33), and divide it by 10. That gives you v = (3.1, 3.3). Now add that to your player position at each step.


If that makes sense, you already understand vectors, to some degree.


You mentioned "arriving smoothly", so perhaps you are looking for a more sophisticated movement that decelerates as you get close to the target. If that's the case, you may want to look into steering behaviors. There is one called "arrive" precisely for this.

#5134812 This is what atan2() does if given two Vector2D points on 2D plane?

Posted by Álvaro on 26 February 2014 - 12:05 PM

You keep saying that you need to find the angle, but knowing (cos(angle), sin(angle)) is usually enough. You get that simply from normalizing P2-P1, as you say. If you really need the angle, you can use atan2. The code looks something like this:


Vector2D diff = P2-P1;
float angle = atan2(diff.y,diff.x);

#5134629 Inheritance question

Posted by Álvaro on 25 February 2014 - 08:59 PM

I believe destructing a Derived through a pointer to Base is indeed undefined behavior if Base doesn't have a virtual destructor.

To make matters less clear smile.png , this does work:
#include <iostream>
#include <memory>

struct Base {
  Base() {
    std::cout << "Base\n";

  ~Base() {
    std::cout << "~Base\n";

struct Derived : Base {
  Derived() {
    std::cout << "Derived\n";

  ~Derived() {
    std::cout << "~Derived\n";

int main() {
  std::shared_ptr<Base> p(new Derived);
  // The constructor of std::shared_ptr<Base> is a template and it will remember to call the correct destructor for the type passed!

#5134611 Inheritance question

Posted by Álvaro on 25 February 2014 - 07:31 PM

Also note that you don't have a virtual destructor.  A destructor should either be public and virtual (meaning you can use it as a base class) or protected and non-virtual (meaning you cannot inherit from it.) This has the same basic problem, if you don't do the destructor properly the object can be incompletely destroyed.

I know this is the correct thing to do, and I am in no way advocating NOT making it virtual, but in this case does it actually matter in practice?
i.e. Derived has no extra memory overhead, so the allocated space for Base is released? Just a question.

The fact that Derived has nothing extra to do in the destructor is a detail that Base shouldn't be aware of. In particular, if that changes in the future, only the code in Derived should have to change.

#5134475 Inheritance question

Posted by Álvaro on 25 February 2014 - 11:59 AM

What BitMaster said. I would use std::vector<std::unique_ptr<Base>>.


Also, your base classes should always have a virtual destructor.

#5134438 Can you talk the programming techno talk?

Posted by Álvaro on 25 February 2014 - 09:52 AM

It is impolite to use abbreviations without introducing them. Some of the people reading your thread might not think immediately of what "the SE/SO site" means. I guess you are talking about "Stack Exchange" and "Stack Overflow". So instead of saving yourself a few keystrokes you could save others some confusion.

#5134312 An Ever Changing/Learing AI

Posted by Álvaro on 24 February 2014 - 08:49 PM

Could you point me in the direction of a good starting place to learn the basics of AI. I don't intend to dive into it much as I stated I am mainly graphics oriented however I would like to have a better understanding of what I am talking about so that in the future I don't make myself look foolish like I did in this post.

As already mentioned, the sticky at the top of this forum is a good starting place. If you want to get more serious about it, I particularly recommend the text book "Artificial Intelligence: A Modern Approach" (which is listed in the sticky).

EDIT: Oh, and I meant to say that you didn't look foolish at all. Although I do think you are wrong, the way you were thinking was the way everyone thought in the 70s, and is still very common.

#5134222 Immediate values come from the ALU?

Posted by Álvaro on 24 February 2014 - 04:16 PM

Why is this thread tagged "C Language" and "C++"? I am pretty sure in the context of those languages there is no such thing as an ALU, and even "immediate value" is a foreign concept (although there are constants, which are related).

#5134160 How to move particle on sphere

Posted by Álvaro on 24 February 2014 - 12:20 PM

I don't understand how to write a "vector3.rotateAround(vector axis, float angle)"-function.



It's very easy to do with quaternions. You just need to know how to construct the quaternion that represents the rotation you want, which is cos(angle/2) + sin(angle/2)*x*i + sin(angle/2)*y*j + sin(angle/2)*z*k, and how to rotate a point by the rotation represented by a unit-length quaternion, which is q' = q * v * conj(q).

#5134158 How to move particle on sphere

Posted by Álvaro on 24 February 2014 - 12:14 PM

If (x,y,z) is a point on the surface of the unit sphere centered at the origin, the East vector is something like (z, 0, -x) and the North vector is something like (-xy/S, S, -yz/S), with S:=sqrt(1-y^2). You can then express your direction as North * cos(angle) + East * sin(angle).


I kind of rushed through the Math, so I am not 100% certain that the formulas are correct, but you can try to reproduce them yourself.

#5134141 An Ever Changing/Learing AI

Posted by Álvaro on 24 February 2014 - 10:56 AM

This notion that in order to have learning AI you need programs that modify their own code is preposterous, but also very common. The divide between code and parameters is an arbitrary one, and it's much more productive to think of learning as changing parameters.


The other thing that should be mentioned is that adaptive AI is something you probably don't want in a video game. It's hard enough to debug a non-adaptive AI to make sure that the bad guys don't get stuck in corners or do other stupid things that break the illusion. If the AI can change once it's in the hands of the player, this can become a much harder problem, unless the adaptability is very limited and controlled.

#5134133 How to move particle on sphere

Posted by Álvaro on 24 February 2014 - 10:33 AM

Weird. I just answered a question about quaternions that is essentially the same question.


If your time step is small, move the particle in R^3, forgetting about the sphere for a moment, then project back to the sphere. If that approximation is not good enough, there is a formula you can use that involves computing cosine and sine. I'll figure it out and post it, if you really need it.

#5134125 Can't get Quaternion Time Derivative to work?

Posted by Álvaro on 24 February 2014 - 10:13 AM

Notice that my formula is exact only if the angular velocity is constant for the whole second. In your simulation that will probably not be true, and you'll have to split time into smaller steps. At that point the steps will probably be small enough that your original approximation is perfectly acceptable.


I should have mentioned in my previous post that if you make the approximations cos(epsilon) = 1 and sin(epsilon)=epsilon (which are good for small epsilon), my formula turns into the one you posted.