Example #1
0
void NPC_Agent::init(float screenWidth, float screenHeight, float speed) {
	this->screenWidth = screenWidth;
	this->screenHeight = screenHeight;

	static std::mt19937 randomGenerator(std::time(NULL));

	std::uniform_real_distribution<float> randDstX(-screenWidth / 2.0f, screenWidth / 2.0f);
	std::uniform_real_distribution<float> randDstY(-screenHeight / 2.0f, screenHeight / 2.0f);
	std::uniform_real_distribution<float> randDir(-1.0f, 1.0f);
	//Get the position
	pos.x = randDstX(randomGenerator);
	pos.y = randDstY(randomGenerator);

	// Get the direction
	vel.x = randDir(randomGenerator);
	vel.y = randDir(randomGenerator);

	// Make the vector length 1, so that it is a direction vector. Direction vectors ALWAYS have length of 1.
	vel = glm::normalize(vel);

	width = 140.0f;
	height = 100.0f;

	// Get the velocity
	// Velocity is speed * direction
	vel *= speed;
}
Example #2
0
bool hitTriang(Photon *photon, Triang *triang, const float distance, const Material *materials)
{
    Shader shader = DIFFUSE;

    // Better idea?
    if (fDot(triang->direct, photon->direct) > -EPSILON)
        triang->direct = vNeg(triang->direct);

    // Update ray's position to the point where the ray hits the sphere
    photon->origin = fFMA(photon->direct, distance - UM(10), photon->origin);
    photon->lambda = materials[triang->mID].lambda;

    if (shader == CRAZY) {
        photon->direct = vNorm(vSub(newVec(0.99, 0, 0), photon->origin));
        return REFLECTED;
    }

    else if (shader == SPECULAR) {
        // Angle between new and old vector is twice the angle between old vector and normal
        Vec reflect = vDot(photon->direct, triang->direct);
        photon->direct = vSub(photon->direct, vMul(triang->direct, vAdd(reflect, reflect)));

        return REFLECTED;
    }

    else if (shader == DIFFUSE) {
        photon->direct = randDir();
        if (fDot(photon->direct, triang->direct) < EPSILON)
            photon->direct = vNeg(photon->direct);

        return REFLECTED;
    }

    else return ABSORBED;
}
Example #3
0
void Ball::Update(float elapsed, Paddle& left, Paddle& right) {
	if (keys[SDL_SCANCODE_SPACE] && dir_x == 0 && dir_y == 0) { dir_x = randDir(); dir_y = randDir(); } // start the ball by hitting the space bar
	x += elapsed * speed * dir_x;
	y += elapsed * speed * dir_y;

	if (y <= -0.975f) { dir_y = 1.0f; } // ball hitting bottom of the screen
	else if (y >= 0.975f) { dir_y = -1.0f; } // ball hitting the top of the screen


	if (-1.652f + left.width / 2 >= x - size / 2 &&
		left.y - left.height / 2 <= y - size / 2 &&
		left.y + left.height / 2 >= y + size / 2) {
		Mix_PlayChannel(-1, hitSound, 0);
		dir_x = 1.0f;
	} // ball hitting left paddle

	if (1.652f - right.width / 2 <= x + size / 2 &&
		right.y - right.height / 2 <= y - size / 2 &&
		right.y + right.height / 2 >= y + size / 2) {
		Mix_PlayChannel(-1, hitSound, 0);
		dir_x = -1.0f;
	} // ball hitting right paddle

	if (x >= 1.752f || x <= -1.752) {
		Mix_PlayChannel(-1, dieSound, 0);
		x = 0.0f; y = 0.0f; dir_x = 0.0f; dir_y = 0.0f;
		left.y = 0.0f;  right.y = 0.0f;
	} // reset if somebody loses
}
Example #4
0
int main(int argc, char **argv)
{
    AABBox box(Vec3f(-1), Vec3f(1));
    gen.seed(0);
    for (uint32_t i = 0; i < 16; ++i) {
        Vec3f randDir(2 * dis(gen) - 1, 2 * dis(gen) - 1, 2 * dis(gen) - 1);
        randDir.normalize();
        Ray ray(Vec3f(0), randDir);
        float t;
        if (box.intersect(ray, t)) {
            Vec3f Phit = ray.orig + ray.dir * t;
            std::cerr << ray.orig << " " << Phit << std::endl;
        }
    }
    return 0;
}
Example #5
0
Photon triangEmit(const Triang *triang, const Material *materials)
{
    Photon p;
    float u = drand48(), v = drand48();
    Vec point = vAdd(fMul(triang->vectab, u), fMul(triang->vectac, v));

    // If point is outside triangle
    if (!triang->rectan && u + v < EPSILONONE)
        p.origin = vSub(vAdd(triang->origin, vAdd(triang->vectab, triang->vectac)), point);
    else
        p.origin = vAdd(triang->origin, point);

    p.direct = randDir();
    if (fDot(p.direct, triang->direct) > EPSILON)
        p.direct = vNeg(p.direct);

    p.lambda = materials[triang->mID].lambda;
    return p;
}
Example #6
0
void MainGame::initBalls() {

	// Initializes the grid
	_grid = std::make_unique<Grid>(m_screenWidth, m_screenHeight, CELL_SIZE);
	

#define ADD_BALL(p, ...) \
    totalProbability += p; \
    possibleBalls.emplace_back(__VA_ARGS__);

    // Number of balls to spawn
    const int NUM_BALLS = 5000;

    // Random engine stuff
    std::mt19937 randomEngine((unsigned int)time(nullptr));
    std::uniform_real_distribution<float> randX(0.0f, (float)m_screenWidth);
    std::uniform_real_distribution<float> randY(0.0f, (float)m_screenHeight);
    std::uniform_real_distribution<float> randDir(-1.0f, 1.0f);

    // Add all possible balls
    std::vector <BallSpawn> possibleBalls;
    float totalProbability = 0.0f;

	std::uniform_real_distribution<float> r1(2.0f, 6.0f);
	std::uniform_int_distribution<int> r2(0, 255);

    // Adds the balls using a macro
    ADD_BALL(20.0f, Engine::Color(255, 255, 255, 255),
             2.0f, 1.0f, 0.1f, 7.0f, totalProbability);
    ADD_BALL(10.0f, Engine::Color(0, 0, 255, 255),
             3.0f, 2.0f, 0.1f, 3.0f, totalProbability);
	ADD_BALL(1.0f, Engine::Color(255, 0, 0, 255),
			5.0f, 4.0f, 0.0f, 0.0f, totalProbability);
	for (int i = 0; i < 10000; i++){
		ADD_BALL(1.0f, Engine::Color(r2(randomEngine), r2(randomEngine), r2(randomEngine), 255), r1(randomEngine), r1(randomEngine), 0.0f, 0.0f, totalProbability);
	}

    // Random probability for ball spawn
    std::uniform_real_distribution<float> spawn(0.0f, totalProbability);

    // Small optimization that sets the size of the internal array to prevent
    // extra allocations.
    m_balls.reserve(NUM_BALLS);

    // Set up ball to spawn with default value
    BallSpawn* ballToSpawn = &possibleBalls[0];
    for (int i = 0; i < NUM_BALLS; i++) {
        // Get the ball spawn roll
        float spawnVal = spawn(randomEngine);
        // Figure out which ball we picked
        for (size_t j = 0; j < possibleBalls.size(); j++) {
            if (spawnVal <= possibleBalls[j].probability) {
                ballToSpawn = &possibleBalls[j];
                break;
            }
        }

        // Get random starting position
        glm::vec2 pos(randX(randomEngine), randY(randomEngine));

        // Hacky way to get a random direction
        glm::vec2 direction(randDir(randomEngine), randDir(randomEngine));
        if (direction.x != 0.0f || direction.y != 0.0f) { // The chances of direction == 0 are astronomically low
            direction = glm::normalize(direction);
        } else {
            direction = glm::vec2(1.0f, 0.0f); // default direction
        }

        // Add ball
        m_balls.emplace_back(ballToSpawn->radius, ballToSpawn->mass, pos, direction * ballToSpawn->randSpeed(randomEngine),
                             Engine::ResourceManager::getTexture("Textures/circle.png").id,
                             ballToSpawn->color);
		// Add the ball to the grid.
		_grid->addBall(&m_balls.back());
    }
}
Example #7
0
Triang randTriang(const bool rectan)
{
    return newTriang(fMul(randVec(), 10), randDir(), randDir(), rectan, 0);
}
Example #8
0
File: point.c Project: nixberg/lumo
Photon pointEmit(const Point *point, const Material *materials)
{
    return (Photon) {point->origin, randDir(), materials[point->mID].lambda};
}
Example #9
0
Photon randPhoton()
{
    return (Photon) {zeroVec(), randDir(), WL_MAXLUM};
}