Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carrierautopilot #9

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Code to enable Carrier to go to specific destination. There is still …
…work to do to see if the conversion between coordinates is Ok and to improve the code that perform the conversion. The game is very stable.
  • Loading branch information
faturita committed Jan 7, 2020
commit d63d87939ab8e0c72fd3d5e978eaf2b327e5f497
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 太陽系外惑星航空母艦

Excited warfare on the seas of a Kepler IV.

The time has come to conquer Kepler IV. Check [video](https://www.youtube.com/watch?v=_LJj1x4orbU).
Expand Down Expand Up @@ -137,3 +139,5 @@ References
* https://pleiades.ucsc.edu/hyades/OpenGL_on_OS_X
* https://www.opengl.org/archives/resources/features/KilgardTechniques/oglpitfall/
* https://forums.khronos.org/showthread.php/4991-The-Solution-for-gluLookAt()-Function!!!!
* www.videotutorialsrock.com
* https://learnopengl.com/Advanced-OpenGL
5 changes: 4 additions & 1 deletion carrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ void update(int value)
}
if (!controller.pause)
{

// Auto Control: The controller can be controlled by the user or by the AI
// Each object is responsible for generating their own controlregisters as if it were a user playing
// Hence this code gets the controlregisters if AUTO is enabled. And then it uses the controlregister
// to control each object as if it were exactly the user (with doControl() in the loop ahead).
if (controller.controlling != CONTROLLING_NONE)
{
if (!entities[controller.controlling]->isAuto())
Expand Down
4 changes: 2 additions & 2 deletions keplerivworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ void nearCallback (void *data, dGeomID o1, dGeomID o2)
if (ground == contact[i].geom.g1 && v2 && isManta(v2) && groundcollisions(v2)) {}
if (ground == contact[i].geom.g2 && v1 && isManta(v1) && groundcollisions(v1)) {}

if (v1 && isWalrus(v1)) { v1->inert = true;}
if (v2 && isWalrus(v2)) { v2->inert = true;}
if (v1 && isWalrus(v1)) { v1->inert = false;}
if (v2 && isWalrus(v2)) { v2->inert = false;}

} else {
// Object against object collision.
Expand Down
24 changes: 23 additions & 1 deletion map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void zoommapin()

void zoommapout()
{
if (mapzoom>=1)
if (mapzoom>1)
mapzoom--;
}

Expand All @@ -126,6 +126,28 @@ void centermap(int ccx, int ccy)
cy = (int)(ccy*(ysize)/900.0)+cy-ysize/2;
}

Vec3f setLocationOnMap(int ccx, int ccy)
{
// @FIXME: Parametrize all the resolution values.
int xsize = 1200/mapzoom;
int ysize = 800/mapzoom;

// @FIXME: This should be adapted according to the screen resolution.
ccx = (int)(ccx*(xsize)/1500.0)+cx-xsize/2;
ccy = (int)(ccy*(ysize)/900.0)+cy-ysize/2;

// Map coordinates in kmf are centered at (0,0) which is the center of the screen. Positive is upwards, left.
ccx = ccx - 600;
ccy = ccy - 400;

ccy = ccy * (-1);
ccx = ccx * (-1);

Vec3f loc(ccx*1 kmf, 0.0f, ccy * 1 kmf);

return loc;
}


void drawMap()
{
Expand Down
4 changes: 4 additions & 0 deletions map.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#ifndef MAP_H
#define MAP_H

#include "math/vec3f.h"

void drawMap();
void zoommapin();
void centermap(int ccx, int ccy);

void zoommapout();

Vec3f setLocationOnMap(int ccx, int ccy);

#endif // MAP_H
2 changes: 2 additions & 0 deletions math/vec3f.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <iostream>

#define kmf *1000.0f

class Vec3f {
private:
float v[3];
Expand Down
108 changes: 84 additions & 24 deletions units/Balaenidae.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern GLuint _textureMetal;

extern GLuint _textureRoad;

extern std::vector<std::string> messages;

Balaenidae::~Balaenidae()
{
delete _model;
Expand Down Expand Up @@ -107,20 +109,97 @@ void Balaenidae::embody(dBodyID myBodySelf)

}

void Balaenidae::doControl(struct controlregister regs)
void Balaenidae::doControl()
{
if (getThrottle()==0 and regs.thrust != 0)
honk();
setThrottle(regs.thrust*2*5);
Controller c;

Balaenidae::rudder = -regs.roll;
c.registers = myCopy;


Vec3f Po = getPos();

Po[1] = 0.0f;

Vec3f Pf(145 kmf, -1.0f, 89 kmf - 3.5 kmf);

Pf = destination;

Vec3f T = Pf - Po;

float eh, midpointpitch;


if (!reached && T.magnitude()>500)
{
float distance = T.magnitude();

Vec3f F = getForward();

F = F.normalize();
T = T.normalize();


float e = acos( T.dot(F) );

float signn = T.cross(F) [1];


printf("T: %10.3f %10.3f %10.3f\n", distance, e, signn);

if (abs(e)>=0.5f)
{
c.registers.roll = 30.0 * (signn>0?+1:-1) ;
} else
if (abs(e)>=0.4f)
{
c.registers.roll = 20.0 * (signn>0?+1:-1) ;
} else
if (abs(e)>=0.2f)
c.registers.roll = 10.0 * (signn>0?+1:-1) ;
else {
c.registers.roll = 0.0f;
}

c.registers.thrust = 1500.0f;

if (distance<10000.0f)
{
c.registers.thrust = 800.0f;
}

if (distance<2000.0f)
{
c.registers.thrust = 150.0f;
}

} else {
if (!reached)
{
char str[256];
sprintf(str, "Balaenidae has arrived to destination.");
messages.insert(messages.begin(), str);
reached = true;
c.registers.thrust = 0.0f;
c.registers.roll = 0.0f;
}
}

doControl(c);
}

void Balaenidae::doControl(Controller controller)
{
doControl(controller.registers);
}

void Balaenidae::doControl(struct controlregister regs)
{
if (getThrottle()==0 and regs.thrust != 0)
honk();
setThrottle(regs.thrust*2*5);

Balaenidae::rudder = -regs.roll;
}

void Balaenidae::getViewPort(Vec3f &Up, Vec3f &position, Vec3f &forward)
{
Expand Down Expand Up @@ -177,10 +256,6 @@ void Balaenidae::doDynamics(dBodyID body)
}


// Buyoncy
//if (pos[1]<0.0f)
// dBodyAddRelForce(me,0.0,9.81*20050.0f,0.0);

dReal *v = (dReal *)dBodyGetLinearVel(body);

dVector3 O;
Expand Down Expand Up @@ -208,21 +283,6 @@ void Balaenidae::doDynamics(dBodyID body)
wrapDynamics(body);
}

void Balaenidae::doControl()
{
Controller c;

c.registers = myCopy;

c.registers.roll = 1;
if ((rand() % 100 + 1)<10)
c.registers.thrust = 20;
else
c.registers.thrust = 0;

doControl(c);
}

void Balaenidae::offshore()
{
Balaenidae::offshoring = 100;
Expand Down
20 changes: 20 additions & 0 deletions units/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Vehicle::Vehicle()
throttle=0.0f;

steering=0.0f;

destination = Vec3f(0.0f, 0.0f, 0.0f);
}

Vehicle::~Vehicle()
Expand Down Expand Up @@ -332,6 +334,18 @@ void Vehicle::antigravity(dBodyID myBodySelf)
dBodyAddForce(myBodySelf, 0,9.81f,0);
}

float Vehicle::getBearing()
{
Vec3f f = (getForward().normalize())*30;

float val = atan2(f[2], f[0])*180.0/PI;

if (val>=90) val -= 90;
else val += 270;

return val;
}

Vehicle* Vehicle::fire(dWorldID world, dSpaceID space)
{
assert(0 || !"This should not be executed.");
Expand Down Expand Up @@ -479,6 +493,12 @@ int Vehicle::getFaction()
return faction;
}

void Vehicle::setDestination(Vec3f dest)
{
Vehicle::destination = dest;
reached = false;
}

/**
* Check model consistencies.
*
Expand Down
7 changes: 7 additions & 0 deletions units/Vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Vehicle : Observable
float throttle=0;
float engine[3];
float steering;
Vec3f destination;

// @FIXME: This is super ugly.
bool reached=false;

dBodyID me=NULL;
dGeomID geom=NULL;
Expand Down Expand Up @@ -117,6 +121,9 @@ class Vehicle : Observable
struct controlregister getControlRegisters();
void setControlRegisters(struct controlregister);

float getBearing();
void setDestination(Vec3f target);

virtual Vehicle* fire(dWorldID world, dSpaceID space);

virtual int getTtl();
Expand Down
3 changes: 2 additions & 1 deletion units/Walrus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ void Walrus::doDynamics(dBodyID body)

if (getStatus() == Walrus::SAILING)
{
dBodyAddForce(body, v[0]*(-0.01), 0, v[2]*(-0.01));
//dBodyAddForce(body, v[0]*(-0.01), 0, v[2]*(-0.01));
dBodyAddRelTorque( body, 0, -xRotAngle*0.1,0 );
}

else if (getStatus() == Walrus::SAILING && speed > 5.0f)
Expand Down
21 changes: 17 additions & 4 deletions usercontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void processMouseEntry(int state) {
void processMouse(int button, int state, int x, int y) {


//int specialKey = glutGetModifiers();
int specialKey = glutGetModifiers();
// if both a mouse button, and the ALT key, are pressed then
if ((state == GLUT_DOWN)) {

Expand All @@ -82,8 +82,21 @@ void processMouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
//buttonState = 1;
printf("Mouse down %d,%d\n",x,y);
centermap(x,y);
zoommapin();
if (controller.view == 2)
{
if (specialKey == GLUT_ACTIVE_SHIFT)
{
Vec3f target = setLocationOnMap(x,y);
entities[controller.controlling]->setDestination(target);

printf("Destination set to (%10.2f,%10.2f,%10.2f)\n", target[0],target[1],target[2]);


} else {
centermap(x,y);
zoommapin();
}
}
}
// set the color to pure green for the middle button
else if (button == GLUT_MIDDLE_BUTTON) {
Expand Down Expand Up @@ -309,7 +322,7 @@ void handleKeypress(unsigned char key, int x, int y) {
std::cout << "Value:" << std::endl; std::cin >> controller.param[param] ;
}
break;
case '!':controller.view = 1;break;
case '!':( (controller.view == 1)?controller.view=2:controller.view=1);break;
case '@':controller.view = 3;break;
case '~':Camera.control = 0;break;
case '?':gltWriteTGA("file.tga");break;
Expand Down