Jump to content

  • Log In with Google      Sign In   
  • Create Account


sheep19

Member Since 20 Jul 2007
Offline Last Active May 25 2014 02:41 PM
-----

#5093537 Wander behaviour in 3D

Posted by sheep19 on 12 September 2013 - 07:14 AM

Hi,

 

I am implementing a Wander behaviour. In the book I am studying (AI for games), the author uses a 2.5D world.

So what he does is:

Set a circle around the player and put a target that moves on edge of the circle. Then use Seek behaviour to get to it.

 

The problem is that I am in full 3D, so I need something more than a circle. I (naturally) tried a sphere, but I want to be able to define different rotation angles (i.e I want the character to rotate more left/right thatn up/down).

 

As a sphere is defined by two angles (theta and phi), I tried to use a larger value for one of the two to see what happens. But the position of the target depends on both the two angles, so it didn't turn up like I wanted to.

 

This is what I am currently doing:

public SteeringOutput GetSteering()
	{
		SteeringOutput steering = new SteeringOutput();
		
		// set linear velocity at the direction we are moving
		steering.linearVel = character.Transform().forward;
		
		// the velocity is along this direction, at full speed
		steering.linearVel.Normalize();
		steering.linearVel *= maxAcceleration;
		
		theta += Util.RandomBinomial() * maxAngle;
		phi += Util.RandomBinomial() * maxAngle ;
		
		var pos = character.Transform().position;
		pos.x += circleRadius * Mathf.Cos(theta * Mathf.Deg2Rad) * Mathf.Sin(phi * Mathf.Deg2Rad);
		pos.y += circleRadius * Mathf.Sin(theta * Mathf.Deg2Rad) * Mathf.Sin(phi * Mathf.Deg2Rad);
		pos.z += circleRadius * Mathf.Cos(phi * Mathf.Deg2Rad);
		
		target.Transform().position = pos;
		
		steering = seek.GetSteering();
		
		return steering;
	}

What can I do to get the effect I want?

 




#5052107 Will this 3D game (in addition to some 2D) be anough for me to get a job?

Posted by sheep19 on 11 April 2013 - 08:07 AM

Unfortunately, it's not that easy for me.

 

It's not easy for anybody, and it wasn't easy for anybody, including the people who have responded to your questions.

I know, sorry if the way I written it gave a wrong impression.




#5028170 [raytracing] Surface normal on triangle sometimes has the wrong direction

Posted by sheep19 on 02 February 2013 - 03:51 PM

Out of curiosity, are you sure the winding for the vertices in each triangle of the torus is consistent (that is, verts in each triangle should all be ordered clockwise or counter-clockwise; triangles with different windings will have flipped normals)? Also, what are the values for the vertices (is there potential precision error)? There isn't anything obviously wrong from your normal calculation, which is why I'm asking these questions. This is the code for triangles I used in my own raytracer (in case it provides any inspiration):
 

// When loading triangles, I set the normal as:
// Vector3f u = t.b - t.a; (note: t is the Triangle)
// Vector3f v = t.c - t.a;
// t.normal = u.cross(v);
// t.normal.normalize();

class Triangle : public Shape
{
public:
	Vector3f a, b, c;
	Vector3f normal; // normalized

	Triangle() : Shape(Shape::E_TRIANGLE)
	{
	}

	virtual bool intersects(const Ray& ray, float& t, Vector3f& n) const
	{
		// from http://softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm (and Plane.intersect())
		n = normal;
		
		float num = normal.dot(a - ray.start);
		float den = normal.dot(ray.direction);
		
		if (num != 0 && den == 0)
		{
			return false;
		}
		else if (num == 0 && den == 0)
		{
			t = 0; // parallel in same plane
			return true;
		}
		else
		{
			t = num / den;
		}

		Vector3f u = b - a;
		Vector3f v = c - a;
		Vector3f w = ray.start + ray.direction * t - a;

		float uv = u.dot(v);
		float wv = w.dot(v);
		float vv = v.dot(v);
		float wu = w.dot(u);
		float uu = u.dot(u);

		float s1 = (uv * wv - vv * wu) / (uv * uv - uu * vv);
		float t1 = (uv * wu - uu * wv) / (uv * uv - uu * vv);

		return s1 >= 0 && t1 >= 0 && (s1 + t1) <= 1;
	}
};

Actually I changed the floats to doubles for everything that has to do with geometry and those black pixels disappeared! So yes, they were precision errors.




#5017868 Ray-AABB collision detection

Posted by sheep19 on 05 January 2013 - 02:46 PM

Hi. To improve the performance of my raytracer, I decided to use  Uniform Space Partitioning. Basically, I divide the world into 3D boxes, and put objects (Surfaces) inside them. When tracing a ray, I check first with the boxes and if there is a hit, I check with the Surfaces it contains.

 

The problem is, from 11 seconds I dropped to only 8 seconds. Another thing I noticed is that when the world is divided into more boxes, it takes more time to render. So this means there is a problem with my AABB-Ray collision function.

 

bool intersects(const ref Ray r) const
	{
		Vector3 n = void,						// normal
				v = void, u = void;				// vectors
		
		float t = void;
		
		// plane 0 (front)
		v = Vector3(max.x, min.y, min.z) - min;
		u = Vector3(min.x, max.y, min.z) - min;
		n = cross(v, u);
		n.normalize();
		t = dot(r.d, n);
		
		/++writeln("t0 = ", t);
		if( t > 0 )
			writeln("plane 0 intersected");
		else
			writeln("plane 0 not intersected");++/
		
		Vector3 temp = min - r.e;
		Vector3 p = r.e + r.d * dot(temp, n) / t;
		//writeln("p0 = ", p);
		if( p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z )
		{
			//writeln("YES 0\n");
			return true;
		}
		/++else
			writeln("NO 0\n");++/
		
		
		// plane 1 (right)
		v = Vector3(max.x, max.y, min.z) - Vector3(max.x, min.y, min.z);
		u = Vector3(max.x, min.y, max.z) - Vector3(max.x, min.y, min.z);
		n = cross(v, u);
		n.normalize();
		
		t = dot(r.d, n);
		/++writeln("t1 = ", t);
		
		if( t > 0 )
			writeln("plane 1 intersected");
		else
			writeln("plane 1 not intersected");++/
		
		temp = Vector3(max.x, min.y, min.z) - r.e;
		p = r.e + r.d * dot(temp, n) / t;
		//writeln("p1 = ", p);
		if( p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z )
		{
			//writeln("YES 1\n");
			return true;
		}
		/++else
			writeln("NO 1\n");++/
		
		// plane 2 (left)
		v = Vector3(min.x, min.y, max.z) - Vector3(min.x, min.y, min.z);
		u = Vector3(min.x, max.y, min.z) - Vector3(min.x, min.y, min.z);
		n = cross(v, u);
		n.normalize();
		
		t = dot(r.d, n);
		/++writeln("t2 = ", t);
		
		if( t > 0 )
			writeln("plane 2 intersected");
		else
			writeln("plane 2 not intersected");++/
		
		temp = Vector3(min.x, min.y, min.z) - r.e;
		p = r.e + r.d * dot(temp, n) / t;
		//writeln("p2 = ", p);
		if( p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z )
		{
			//writeln("YES 2\n");
			return true;
		}
		/++else
			writeln("NO 2\n");++/
		
		// plane 3 (back)
		v = Vector3(max.x, min.y, max.z) - Vector3(min.x, min.y, max.z);
		u = Vector3(min.x, max.y, max.z) - Vector3(min.x, min.y, max.z);
		n = cross(v, u);
		n.normalize();
		
		t = dot(r.d, n);
		/++writeln("t3 = ", t);
		
		if( t > 0 )
			writeln("plane 3 intersected");
		else
			writeln("plane 3 not intersected");++/
		
		temp = Vector3(min.x, min.y, max.z) - r.e;
		p = r.e + r.d * dot(temp, n) / t;
		//writeln("p3 = ", p);
		if( p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z )
		{
			//writeln("YES 3\n");
			return true;
		}
		/++else
			writeln("NO 3\n");++/
		
		// plane 4 (top)
		v = Vector3(min.x, max.y, max.z) - Vector3(min.x, max.y, min.z);
		u = Vector3(max.x, max.y, min.z) - Vector3(min.x, max.y, min.z);
		n = cross(v, u);
		n.normalize();
		
		t = dot(r.d, n);
		/++writeln("t4 = ", t);
		
		if( t > 0 )
			writeln("plane 4 intersected");
		else
			writeln("plane 4 not intersected");++/
		
		temp = Vector3(min.x, max.y, min.z) - r.e;
		p = r.e + r.d * dot(temp, n) / t;
		//writeln("p4 = ", p);
		if( p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z )
		{
			//writeln("YES 4\n");
			return true;
		}
		else
			/++writeln("NO 4\n");++/
		
		// plane 5 (bottom)
		v = Vector3(max.x, min.y, min.z) - Vector3(min.x, min.y, min.z);
		u = Vector3(min.x, min.y, max.z) - Vector3(min.x, min.y, min.z);
		n = cross(v, u);
		n.normalize();
		
		t = dot(r.d, n);
		/++writeln("t5 = ", t);
		
		if( t > 0 )
			writeln("plane 5 intersected");
		else
			writeln("plane 5 not intersected");++/
		
		temp = Vector3(min.x, min.y, min.z) - r.e;
		p = r.e + r.d * dot(temp, n) / t;
		//writeln("p5 = ", p);
		if( p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z )
		{
			//writeln("YES 5\n");
			return true;
		}
		/++else
			writeln("NO 5\n");++/
		
		return false;
	}

What I do is check all six planes and then if the hit point is inside the boundaries. I know this is the worst method. What's a better way to do this?

 

Please, if you suggest something, provide an explanation why it works, as that's the important thing here - I want to learn this stuff.

Thank you :)




#5014042 Raytracing - circle looks "ugly"

Posted by sheep19 on 24 December 2012 - 04:43 PM

Thank you very much :)




#5013983 Raytracing - circle looks "ugly"

Posted by sheep19 on 24 December 2012 - 11:50 AM

Hello again. I noticed that, when having two lights, it's like one of them over-writes the other's color (for the specular highlights). Here's what I mean:

 

20ku0kk.jpg

 

If I remove one of the two lights, it is shown correctly.

 

Here is my shade function. Is there anything wrong?

 

Vector3 shade(HitInfo hitInfo, ref Scene scene) const
	{
		Vector3 finalColor = Vector3(0.0f, 0.0f, 0.0f); // the final color
		
		// normalize the surface normal
		hitInfo.surfaceNormal.normalize();
		
		foreach(light; scene.lights)
		{
			Vector3 lightVector = light.position - hitInfo.hitPoint; // vector from the hit point to the light
		
			lightVector.normalize();
			
			HitInfo hitInfo2;
			Ray ray = {hitInfo.hitPoint, light.position - hitInfo.hitPoint};
			ray.d.normalize();
			
			if( !scene.trace(ray, hitInfo2, 0.1f) )
			{
				// diffuse shading
				if( hitInfo.surfaceNormal.dot(lightVector) > 0 )
				{
					finalColor = finalColor + light.I * color * hitInfo.surfaceNormal.dot(lightVector);

					hitInfo.ray = -hitInfo.ray;
					hitInfo.ray.normalize();
					
					// specular shading
					Vector3 H = (lightVector + hitInfo.ray) * 0.5f; // find the half vector, H
					H.normalize();
					
					float specularDotProduct = dot(hitInfo.surfaceNormal, H);
					
					if( specularDotProduct > 0.0f )
						finalColor = finalColor + light.I * std.math.pow(specularDotProduct, 10.0f);
				}
			}
			else
			{
				// no color is added, shadow is shown
			}
		}
		
		return finalColor;
	}

 

By the way, I have triangles now! I will write an .obj loader soon, and I will have 3D meshes!!! :)




#4984792 #pragma once

Posted by sheep19 on 28 September 2012 - 11:52 AM

1) Do not use it in source files. Source files are not "included" anyway.
2) You should use it in every header file. If you don't, you are just asking for trouble. And no, there is no reason to include a header file more than once.
3) By using #pragma once you are not being "portable". That means your program is going to work only on windows, on the compiler that comes with visual studio (cl).
The correct (i.e. portable) way is to do it like this:
#ifndef SOME_FILE_H
#define SOME_FILE_H

// all your definitions here

#endif


#4953889 Learning SDL, need help to draw an object on screen between classes

Posted by sheep19 on 29 June 2012 - 02:39 AM

A small change:

class Player {
  private:
	SDL_Surface* image;

	 // better disable the copy constructor and the assignment operators because you didn't define them <img src='http://public.gamedev.net//public/style_emoticons/default/smile.png' class='bbc_emoticon' alt=':)' />
	 Player(const Player &);
	 Player &operator = (const Player &);

  public:
	Player() {
	  SLD_Surface *image = IMG_load("hello.png");  // Load image when creating the player
	}

	~Player() {
	  SDL_FreeSurface(image);					 // Free the image when destroying the player
	}

	void draw(SDL_Surface* screen) {
	  SDL_BlitSurface(image, NULL, screen, NULL); // Draw the player image on screen
	}
}

// in main loop
// ...
player.draw(screen);
SDL_Flip(screen)// to update the scene
// ...



#4821874 Think Fast!

Posted by sheep19 on 10 June 2011 - 03:35 PM

Think Fast! is a game that helps you sharpen your mathematical skills.

Features:
* 4 difficulty levels - Easy, Normal, Hard, and for real math experts, Genius
* Helps you sharpen your mathematical skill
* Makes you more comfortable for solving equations fast
* Suitable for all ages - from kids to adults

Available languages: English, Greek




#4806874 Why are most standard code files so confusing? And what is the type of std::v...

Posted by sheep19 on 05 May 2011 - 06:35 AM

It couldn't be int or unsigned it because those are different among different platforms.

It is an unsigned type for sure, though I;m not sure for its bits.

Use std::vector<yourTypeHere>::size_type and not int, because if the length of the vector exceeds INT_MAX, the size you will get will be negative.That's why there's a warning.


#4806861 Ultra fast fibonacci function in C++! :)

Posted by sheep19 on 05 May 2011 - 06:19 AM

Hi, I have created a fibonacci generation function, which is not recursive!!

Here it is:

double fibonacci(unsigned n)
{
	return 0.447213595 * pow(1.61803399, (double)n) - 0.447213595 * pow(-0.618033989, (double)n);
}



PARTNERS