Skip to content

Commit

Permalink
Factor out asteroid rotation code
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen M. Cameron <stephenmcameron@gmail.com>
  • Loading branch information
smcameron committed Aug 2, 2015
1 parent e46ff57 commit 1b063b7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 32 deletions.
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -320,7 +320,7 @@ GLEWCFLAGS:=$(shell pkg-config --cflags glew)
COMMONOBJS=mathutils.o snis_alloc.o snis_socket_io.o snis_marshal.o \
bline.o shield_strength.o stacktrace.o snis_ship_type.o \
snis_faction.o mtwist.o names.o infinite-taunt.o snis_damcon_systems.o \
string-utils.o c-is-the-locale.o starbase_metadata.o
string-utils.o c-is-the-locale.o starbase_metadata.o arbitrary_spin.o
SERVEROBJS=${COMMONOBJS} snis_server.o starbase-comms.o \
power-model.o quat.o vec4.o matrix.o snis_event_callback.o space-part.o fleet.o \
commodities.o docking_port.o
Expand Down Expand Up @@ -676,6 +676,9 @@ quat.o: quat.c Makefile
vec4.o: vec4.c Makefile
$(Q)$(COMPILE)

arbitrary_spin.o: arbitrary_spin.c arbitrary_spin.h Makefile
$(Q)$(COMPILE)

mtwist.o: mtwist.c Makefile
$(Q)$(COMPILE)

Expand Down
58 changes: 58 additions & 0 deletions arbitrary_spin.c
@@ -0,0 +1,58 @@
/*
Copyright (C) 2010 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "arbitrary_spin.h"

void compute_arbitrary_spin(float frame_rate_hz, double timestamp,
union quat *orientation,
union quat *rotational_velocity)
{
/* reduce to axis and angle */
float x, y, z, a;
quat_to_axis(rotational_velocity, &x, &y, &z, &a);

/* current rotation is universe timestamp * rotation per timestamp
rotational_velocity is frame_rate_hz and universe is 1/10 sec */
a = a * frame_rate_hz / 10.0 * timestamp;

quat_init_axis(orientation, x, y, z, a);
}

union quat random_orientation[NRANDOM_ORIENTATIONS];
union quat random_spin[NRANDOM_SPINS];

void initialize_random_orientations_and_spins(int mtwist_seed)
{
int i;
struct mtwist_state *mt;

mt = mtwist_init(mtwist_seed);
for (i = 0; i < NRANDOM_ORIENTATIONS; i++) {
float angle = mtwist_float(mt) * 2.0 * M_PI;
consistent_random_axis_quat(mt, &random_orientation[i], angle);
}
for (i = 0; i < NRANDOM_SPINS; i++) {
float angular_speed = ((float) mtwist_int(mt, 100) / 10.0 - 5.0) * M_PI / 180.0;
consistent_random_axis_quat(mt, &random_spin[i], angular_speed);
}
mtwist_free(mt);
}

17 changes: 17 additions & 0 deletions arbitrary_spin.h
@@ -0,0 +1,17 @@
#ifndef ARBITRARY_SPIN_H__
#define ARBITRARY_SPIN_H__

#include <math.h>
#include "quat.h"

#define NRANDOM_ORIENTATIONS 20
#define NRANDOM_SPINS 20
extern union quat random_orientation[NRANDOM_ORIENTATIONS];
extern union quat random_spin[NRANDOM_SPINS];
extern void initialize_random_orientations_and_spins(int mtwist_seed);
extern void compute_arbitrary_spin(float frame_rate_hz, double timestamp,
union quat *orientation,
union quat *rotational_velocity);

#endif

34 changes: 3 additions & 31 deletions snis_client.c
Expand Up @@ -67,6 +67,7 @@
#include "infinite-taunt.h"
#include "mathutils.h"
#include "quat.h"
#include "arbitrary_spin.h"
#include "snis.h"
#include "snis-culture.h"
#include "snis_alloc.h"
Expand Down Expand Up @@ -395,27 +396,6 @@ struct my_vect_obj placeholder_part_spun[128];
static struct snis_entity *curr_science_guy = NULL;
static struct snis_entity *prev_science_guy = NULL;

#define NRANDOM_ORIENTATIONS 20
static union quat random_orientation[NRANDOM_ORIENTATIONS];
#define NRANDOM_SPINS 20
static union quat random_spin[NRANDOM_SPINS];
static void initialize_random_orientations_and_spins(void)
{
int i;
struct mtwist_state *mt;

mt = mtwist_init(mtwist_seed);
for (i = 0; i < NRANDOM_ORIENTATIONS; i++) {
float angle = mtwist_float(mt) * 2.0 * M_PI;
consistent_random_axis_quat(mt, &random_orientation[i], angle);
}
for (i = 0; i < NRANDOM_SPINS; i++) {
float angular_speed = ((float) mtwist_int(mt, 100) / 10.0 - 5.0) * M_PI / 180.0;
consistent_random_axis_quat(mt, &random_spin[i], angular_speed);
}
mtwist_free(mt);
}

void to_snis_heading_mark(const union quat *q, double *heading, double *mark)
{
quat_to_heading_mark(q,heading,mark);
Expand Down Expand Up @@ -1776,15 +1756,7 @@ static void spin_wormhole(double timestamp, struct snis_entity *o)

static void arbitrary_spin(double timestamp, struct snis_entity *o, union quat *rotational_velocity)
{
/* reduce to axis and angle */
float x, y, z, a;
quat_to_axis(rotational_velocity, &x, &y, &z, &a);

/* current rotation is universe timestamp * rotation per timestamp
rotational_velocity is frame_rate_hz and universe is 1/10 sec */
a = a * (float)frame_rate_hz / 10.0 * timestamp;

quat_init_axis(&o->orientation, x, y, z, a);
compute_arbitrary_spin(frame_rate_hz, timestamp, &o->orientation, rotational_velocity);
if (o->entity)
update_entity_orientation(o->entity, &o->orientation);
}
Expand Down Expand Up @@ -13528,7 +13500,7 @@ int main(int argc, char *argv[])
init_keymap();
read_keymap_config_file();
init_vects();
initialize_random_orientations_and_spins();
initialize_random_orientations_and_spins(COMMON_MTWIST_SEED);
if (read_planet_material_metadata(&nplanet_materials)) {
fprintf(stderr, "Failed reading planet material metadata\n");
exit(1);
Expand Down

0 comments on commit 1b063b7

Please sign in to comment.