Jump to content

  • Log In with Google      Sign In   
  • Create Account

Álvaro

Member Since 07 Mar 2002
Offline Last Active Today, 02:19 AM

#5302362 How To Make My Crosshair Red When Over An Enemy?

Posted by Álvaro on 24 July 2016 - 04:10 PM

I can't find any material on collision detection between a ray and a box in any of my books. This means that it should be easy, but nothing comes to mind, any ideas?


Figure out how to compute the intersection between a ray and a plane. You can do it in such a way that the result is expressed in a frame of reference in the plane such that both coordinates will be between 0 and 1 precisely when the rectangular face has been hit. Now use that six times.


#5301779 Rotating Camera

Posted by Álvaro on 21 July 2016 - 09:58 AM

So you need to translate to make the camera be at the origin, rotate the "look-at" point and then undo the translation.

Here's some C++ code that does what you want:
#include <iostream>

struct Vector {
  float x, y, z;
  
  Vector(float x, float y, float z) : x(x), y(y), z(z) {
  }
};

struct Point {
  float x, y, z;
  
  Point(float x, float y, float z) : x(x), y(y), z(z) {
  }
};

std::ostream &operator<<(std::ostream &os, Point p) {
  return os << '(' << p.x << ',' << p.y << ',' << p.z << ')';
}

Vector operator-(Point p1, Point p2) {
  return Vector(p1.x-p2.x, p1.y-p2.y, p1.z-p2.z);
}

Point operator+(Point p, Vector v) {
  return Point(p.x+v.x, p.y+v.y, p.z+v.z);
}

// --- Start reading here ---

Vector rotate_about_y_axis(Vector v, float cos, float sin) {
  return Vector(v.x * cos - v.z * sin, v.y, v.x * sin + v.z * cos);
}

Point rotate_about_y_axis_around_center(Point p, float cos, float sin, Point center) {
  return center + rotate_about_y_axis(p - center, cos, sin);
}

int main() {
  Point eye(0.0f, 1.2f, 0.0f);
  Point lookat(0.0f, 1.0f, -2.0f);
  
  float cos = 0.0f; // std::cos(angle)
  float sin = 1.0f; // std::sin(angle)
  
  Point rotated_lookat = rotate_about_y_axis_around_center(lookat, cos, sin, eye);
  std::cout << "lookat after rotation = " << rotated_lookat << '\n';
}




#5301662 Rotating Camera

Posted by Álvaro on 20 July 2016 - 08:13 PM

You talk about rotating around your character, but you then don't include the character anywhere in the details of your problem. If you mean to rotate the camera without displacing it, you only need to rotate the center point around the eye point. If the character and the camera are not at the same point (unclear from your post), you'll need to rotate both `center' and `eye' around the character.

In order to rotate a point around another point, first apply a translation that makes the center of rotation be the origin, then rotate, then undo the translation.
 
Point rotate_around_point(Point p, Point center, Rotation rotation) {
  return rotate_around_origin(p - center, rotation) + center;
}

Are you missing anything?


#5301514 How To Start Game Programming With C++

Posted by Álvaro on 20 July 2016 - 06:10 AM

I would start by installing SFML and going through their tutorials.


#5301193 Move The Player Along The Direction It Is Facing

Posted by Álvaro on 18 July 2016 - 06:07 AM

I would generally recommend against using quaternions unless you can explain in great detail why they are superior to matrices. There are cases where quaternions are the answer, but pitch, yaw, and roll (which are not Euler angles by the way) not being functional is not one of them.


Quaternions are more compact, much easier to renormalize and better for interpolating. Is that enough detail for you?

Regardless of how you are representing rotations internally, you should have a mechanism to apply a rotation to a vector, without worrying about the details. So your code should look something like this:
void InputPlayer::MoveForward(float dt) {
  player->SetPos(player->GetPos() + apply_rotation(player->GetAttitude(), glm::vec3(0, 0, 5)));
}



#5301116 Particle Lighting 101

Posted by Álvaro on 17 July 2016 - 05:30 PM

It looks like HL2 does mean Half Life 2. They seem to use a particular orthonormal basis in their lighting computations. The slide "Resurrecting an old friend: HL2 basis" has the exact vectors. You can also find them here: http://www.valvesoftware.com/publications/2006/SIGGRAPH06_Course_ShadingInValvesSourceEngine.pdf


#5300002 Finding that balance between optimization and legibility.

Posted by Álvaro on 10 July 2016 - 11:06 AM

I have no idea what Carmack or Abrash have to do with this thread. I also don't understand what those posts from Carmack have to do with my recommendation.

The supposed need to reach a balance between code speed and code readability is largely false. If you write clean code with a bit of common sense, chances are it will be fast enough.

Using quaternions or 3x3 orthogonal matrices to represent rotations, for instance, would get rid of practically every trigonometric function call in your code and make it run faster. And that's while making the code easier to read.


#5299965 Finding that balance between optimization and legibility.

Posted by Álvaro on 10 July 2016 - 05:40 AM

You should have a class that represents rotations. Whether it's implemented as three axis rotations, as quaternions or as something else should be fairly opaque to most of the code.

The "footprint" of the change shouldn't matter too much. Don't be lazy.


#5299002 Hashing by key range

Posted by Álvaro on 04 July 2016 - 06:35 AM

for (auto it = range.first; it != range.second; ++it) {
       return it->second;
}


That is some funny-looking code. It's a loop, but it immediately returns from the function. Are you sure that's what you want?


#5298824 Sticky power up?

Posted by Álvaro on 02 July 2016 - 01:21 PM

You cannot add two positions and expect something good will happen. You can add a position and a vector, though. So something like
 

  float dx = however_you_figure_out_how_much_to_move_the_paddle();

Im guessing this however() is the speed I am adding to the paddle? Am i correct?


It's the speed of the paddle multiplied by the duration of a frame.


#5298821 Sticky power up?

Posted by Álvaro on 02 July 2016 - 01:10 PM

You cannot add two positions and expect something good will happen. You can add a position and a vector, though. So something like

  float dx = however_you_figure_out_how_much_to_move_the_paddle();
  paddle.position.X += dx;
  
  if (ball_is_stuck())
    ball.position.x += dx;



#5298477 Seeding the random number generator in <cstdlib>

Posted by Álvaro on 28 June 2016 - 05:53 PM

If you reseed it repeatedly you're making it less deterministic. This is a good thing if you want it to be unpredictable [...]


No, this is never a good thing. If you want things to be unpredictable, you use some source of entropy and fold it into your PRNG (see /dev/urandom in Linux). By seeding the PRNG repeatedly you might get ridiculously bad numbers, especially if you use some sort of clock to produce the seeds.


Note that the rand function in most c libraries isn't that good and doesn't produce a good spread of random figures, if this is important to you, e.g. for a card game look into marsienne twister or similar Instead.


C++11 provides very nice pseudo-random numbers. Learn how to use them. You can start here, for instance.


#5298184 what means these error?

Posted by Álvaro on 26 June 2016 - 11:38 PM

You code like a beginner, you don't know what the call stack is, and you don't seem to understand segmentation faults. And yet you are writing a multi-threaded C++ program and you have your own hack to implement some sort of runtime type system, full of dangerous pointer casts.

 

It's just not going to work. You can stop trying now.




#5297391 Non-duplicating pairs from a set

Posted by Álvaro on 20 June 2016 - 05:50 PM

I am late to the thread and I didn't read the whole thing, but I think I know a very easy way to do it. You basically have 14 volleyball teams (including "nowhere") and you want to organize a league, so over 13 rounds every team plays every other team. The traditional way to do it is having 7 courts and having team 1 fixed and everyone else rotate, like this:

 

Round 1:
 1   2   3   4   5   6   7
14  13  12  11  10   9   8
 
Round 2:
 1  14   2   3   4   5   6 
13  12  11  10   9   8   7
 
Round 3:
 1  13  14   2   3   4   5
12  11  10   9   8   7   6
 
...

 

Does that make sense?




#5297378 what means these error?

Posted by Álvaro on 20 June 2016 - 04:06 PM

eis o meu Debugger Output:

 

Adoro a mistura of languages you did lá. :)






PARTNERS