Bullet spawning and rendering.

linux_template
sigonasr2 2 years ago
parent 3e52016067
commit 973eef9302
  1. 11
      Faceball2030/assets/enemies/bullet.mtl
  2. BIN
      Faceball2030/assets/enemies/bullet.png
  3. BIN
      Faceball2030/assets/enemies/bullet.wings
  4. 101
      Faceball2030/main.cpp
  5. 21
      Faceball2030/main.h

@ -0,0 +1,11 @@
# Exported from Wings 3D 2.2.9
newmtl sphere1_auv
Ns 19.999999999999996
d 1.0
illum 2
Kd 1.0 1.0 1.0
Ka 0.0 0.0 0.0
Ks 0.19 0.19 0.19
Ke 0.0 0.0 0.0
map_Kd sphere1_auv.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

@ -75,7 +75,7 @@ void FaceBall::LoadLevel(int level)
} }
} }
} }
objects.push_back({ mapMesh }); objects.push_back({ mapMesh,{0,0},0,0 });
} }
bool FaceBall::CheckCollision(vec3d movementVector,vf2d pos,float radius){ bool FaceBall::CheckCollision(vec3d movementVector,vf2d pos,float radius){
@ -85,8 +85,8 @@ bool FaceBall::CheckCollision(vec3d movementVector,vf2d pos,float radius){
for (int x = -1; x <= 1; x++) { for (int x = -1; x <= 1; x++) {
int offsetX = (int)pos.x + x; int offsetX = (int)pos.x + x;
int offsetY = (int)pos.y + y; int offsetY = (int)pos.y + y;
if (offsetX >= 0 && offsetX < MAP_SIZE.x && offsetY >= 0 && offsetY < MAP_SIZE.y) { if (offsetX >= 0 && offsetX < game->MAP_SIZE.x && offsetY >= 0 && offsetY < game->MAP_SIZE.y) {
MapSquare tile = map[offsetY][offsetX]; MapSquare tile = game->map[offsetY][offsetX];
if (tile.wallN != NULL) { if (tile.wallN != NULL) {
utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY},{(float)offsetX+1,(float)offsetY} }; utils::geom2d::line<float>wall{ {(float)offsetX,(float)offsetY},{(float)offsetX+1,(float)offsetY} };
if (((int)(newPos.pos.x-radius)==offsetX|| if (((int)(newPos.pos.x-radius)==offsetX||
@ -456,38 +456,13 @@ int FaceBall::Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle&
} }
} }
void FaceBall::RenderWorld() { void FaceBall::RenderMesh(mat4x4&matView,std::vector<Triangle>&vecTrianglesToRaster, Object&o) {
// Set up rotation matrices for (auto& tri : o.mesh.tris) {
mat4x4 matRotZ, matRotX, matWorld;
matRotZ = Matrix_MakeRotationZ(fTheta * 0.5f);
matRotX = Matrix_MakeRotationX(fTheta);
//matTrans = Matrix_MakeTranslation(0.0f, 0.0f, 0.0f);
matWorld = Matrix_MakeIdentity();
matWorld = Matrix_MultiplyMatrix(matRotZ, matRotX);
//matWorld = Matrix_MultiplyMatrix(matWorld, matTrans);
vec3d vUp = { 0,1,0 };
vec3d vTarget = { 0,sinf(pitch),cosf(pitch) };
mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw);
vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget);
vTarget = Vector_Add(freeRoam?freeRoamCamera:player.GetPos(), vLookDir);
mat4x4 matCamera = Matrix_PointAt(freeRoam?freeRoamCamera:player.GetPos(), vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<Triangle>vecTrianglesToRaster;
// Draw Triangles
for (auto& obj : objects) {
for (auto& tri : obj.mesh.tris) {
Triangle triProjected, triTransformed, triViewed; Triangle triProjected, triTransformed, triViewed;
//mat4x4 matTrans = Matrix_MakeTranslation(-mesh.size.x/2, 0, -mesh.size.x/2);
mat4x4 localMat = Matrix_MakeIdentity(); mat4x4 localMat = Matrix_MakeIdentity();
//localMat = Matrix_MultiplyMatrix(localMat, matTrans); mat4x4 rotMat = Matrix_MakeRotationY(o.rot);
mat4x4 rotMat = Matrix_MakeRotationY(obj.rot);
localMat = Matrix_MultiplyMatrix(localMat, rotMat); localMat = Matrix_MultiplyMatrix(localMat, rotMat);
mat4x4 matTrans = Matrix_MakeTranslation(obj.pos.x, 0, obj.pos.y); mat4x4 matTrans = Matrix_MakeTranslation(o.pos.x-o.radius, o.pos.y-o.radius, o.pos.z - o.radius);
localMat = Matrix_MultiplyMatrix(localMat, matTrans); localMat = Matrix_MultiplyMatrix(localMat, matTrans);
triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]); triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]);
@ -505,7 +480,7 @@ void FaceBall::RenderWorld() {
normal = Vector_CrossProduct(line1, line2); normal = Vector_CrossProduct(line1, line2);
normal = Vector_Normalise(normal); normal = Vector_Normalise(normal);
vec3d vCameraRay = Vector_Sub(triTransformed.p[0], freeRoam?freeRoamCamera:player.GetPos()); vec3d vCameraRay = Vector_Sub(triTransformed.p[0], freeRoam ? freeRoamCamera : player.GetPos());
if (Vector_DotProduct(normal, vCameraRay) < 0) { if (Vector_DotProduct(normal, vCameraRay) < 0) {
vec3d light_dir = Vector_Mul(vLookDir, -1); vec3d light_dir = Vector_Mul(vLookDir, -1);
@ -521,7 +496,7 @@ void FaceBall::RenderWorld() {
triViewed.uv[2] = triTransformed.uv[2]; triViewed.uv[2] = triTransformed.uv[2];
triViewed.col = Pixel(triTransformed.col.r * dp * dp, triTransformed.col.g * dp * dp, triTransformed.col.b * dp * dp); triViewed.col = Pixel(triTransformed.col.r * dp * dp, triTransformed.col.g * dp * dp, triTransformed.col.b * dp * dp);
float dist = std::sqrtf(std::powf((freeRoam ? freeRoamCamera : player.GetPos()).x - tri.p[0].x, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).y - tri.p[0].y, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).z - tri.p[0].z, 2)); float dist = std::sqrtf(std::powf((freeRoam ? freeRoamCamera : player.GetPos()).x - tri.p[0].x, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).y - tri.p[0].y, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).z - tri.p[0].z, 2));
float colorMult = dist>5*PI/3?0:std::sinf(0.3 * dist + PI / 2); float colorMult = dist > 5 * PI / 3 ? 0 : std::sinf(0.3 * dist + PI / 2);
triViewed.col = Pixel(triViewed.col.r * colorMult, triViewed.col.g * colorMult, triViewed.col.b * colorMult); triViewed.col = Pixel(triViewed.col.r * colorMult, triViewed.col.g * colorMult, triViewed.col.b * colorMult);
//triViewed.col = triTransformed.col; //triViewed.col = triTransformed.col;
@ -573,12 +548,43 @@ void FaceBall::RenderWorld() {
triProjected.p[1].y *= 0.5f * (float)ScreenHeight(); triProjected.p[1].y *= 0.5f * (float)ScreenHeight();
triProjected.p[2].x *= 0.5f * (float)ScreenWidth(); triProjected.p[2].x *= 0.5f * (float)ScreenWidth();
triProjected.p[2].y *= 0.5f * (float)ScreenHeight(); triProjected.p[2].y *= 0.5f * (float)ScreenHeight();
triProjected.tex = obj.mesh.texture; triProjected.tex = o.mesh.texture;
vecTrianglesToRaster.push_back(triProjected); vecTrianglesToRaster.push_back(triProjected);
} }
} }
} }
}
void FaceBall::RenderWorld() {
// Set up rotation matrices
mat4x4 matRotZ, matRotX, matWorld;
matRotZ = Matrix_MakeRotationZ(fTheta * 0.5f);
matRotX = Matrix_MakeRotationX(fTheta);
//matTrans = Matrix_MakeTranslation(0.0f, 0.0f, 0.0f);
matWorld = Matrix_MakeIdentity();
matWorld = Matrix_MultiplyMatrix(matRotZ, matRotX);
//matWorld = Matrix_MultiplyMatrix(matWorld, matTrans);
vec3d vUp = { 0,1,0 };
vec3d vTarget = { 0,sinf(pitch),cosf(pitch) };
mat4x4 matCameraRot = Matrix_MakeRotationY(fYaw-PI/2);
vLookDir = Matrix_MultiplyVector(matCameraRot, vTarget);
vec3d playerCenter = { player.GetPos().x, player.GetPos().y, player.GetPos().z };
vTarget = Vector_Add(freeRoam ? freeRoamCamera : playerCenter, vLookDir);
mat4x4 matCamera = Matrix_PointAt(freeRoam?freeRoamCamera: playerCenter, vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<Triangle>vecTrianglesToRaster;
// Draw Triangles
for (auto& obj : objects) {
RenderMesh(matView,vecTrianglesToRaster,obj);
}
for (auto& bullet : bullets) {
RenderMesh(matView, vecTrianglesToRaster, bullet);
} }
//std::sort(vecTrianglesToRaster.begin(),vecTrianglesToRaster.end(),[](triangle&t1,triangle&t2){return (t1.p[0].z+t1.p[1].z+t1.p[2].z)/3.0f>(t2.p[0].z+t2.p[1].z+t2.p[2].z)/3.0f;}); //std::sort(vecTrianglesToRaster.begin(),vecTrianglesToRaster.end(),[](triangle&t1,triangle&t2){return (t1.p[0].z+t1.p[1].z+t1.p[2].z)/3.0f>(t2.p[0].z+t2.p[1].z+t2.p[2].z)/3.0f;});
@ -668,6 +674,9 @@ void FaceBall::HandleKeys(float fElapsedTime) {
} }
else { else {
pitch = 0; pitch = 0;
if (GetMouse(0).bPressed) {
bullets.push_back({ bullet,{player.GetPos().x + player.GetRadius(),player.GetPos().y, player.GetPos().z + player.GetRadius()},fYaw,0.25f,{shotSpd * std::cosf(fYaw),shotSpd * std::sinf(fYaw)} });
}
} }
if (GetKey(W).bHeld) { if (GetKey(W).bHeld) {
if (freeRoam) { if (freeRoam) {
@ -748,10 +757,12 @@ bool FaceBall::OnUserCreate()
arrow = new Decal(new Sprite("assets/arrow.png")); arrow = new Decal(new Sprite("assets/arrow.png"));
enemy_ShootMe_tex = new Decal(new Sprite("assets/enemies/ShootMe.png")); enemy_ShootMe_tex = new Decal(new Sprite("assets/enemies/ShootMe.png"));
YAZAWA = new Decal(new Sprite("assets/yazawa.png")); YAZAWA = new Decal(new Sprite("assets/yazawa.png"));
bullet_tex = new Decal(new Sprite("assets/enemies/bullet.png"));
InitializeEnemyData(); InitializeEnemyData();
Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex); Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex);
bullet = { "assets/enemies/bullet.obj",bullet_tex };
mapMesh.texture = YAZAWA; mapMesh.texture = YAZAWA;
LoadLevel(1); LoadLevel(1);
@ -759,8 +770,28 @@ bool FaceBall::OnUserCreate()
return true; return true;
} }
bool Bullet::Update(float fElapsedTime) {
if (!FaceBall::CheckCollision({ spd.x,0,spd.y }, {pos.x,pos.z}, radius)) {
pos.x += spd.x*fElapsedTime;
pos.z += spd.y*fElapsedTime;
}
else {
return false;
}
return true;
}
bool FaceBall::OnUserUpdate(float fElapsedTime) bool FaceBall::OnUserUpdate(float fElapsedTime)
{ {
for (std::vector<Bullet>::iterator it = bullets.begin(); it != bullets.end(); ++it) {
Bullet& b = *it;
if (!b.Update(fElapsedTime)) {
it=bullets.erase(it);
if (it == bullets.end()) {
break;
}
}
}
switch (mode) { switch (mode) {
case GAME: { case GAME: {
HandleKeys(fElapsedTime); HandleKeys(fElapsedTime);

@ -139,8 +139,14 @@ private:
struct Object { struct Object {
Mesh mesh; Mesh mesh;
vf2d pos = { 0,0 }; vec3d pos = { 0,0 };
float rot = 0; float rot = 0;
float radius = 0.2f;
};
struct Bullet : Object{
vf2d spd = { 0,0 };
bool Update(float fElapsedTime);
}; };
struct EnemyData { struct EnemyData {
@ -171,13 +177,14 @@ class FaceBall : public PixelGameEngine
EnemyData GetData(EnemyID id); EnemyData GetData(EnemyID id);
Decal* circle,*arrow,*YAZAWA; Decal* circle,*arrow,*YAZAWA;
private: private:
Mesh mapMesh; Mesh mapMesh,enemy_ShootMe,bullet;
Decal* dot, * enemy_ShootMe_tex; Decal* dot, * enemy_ShootMe_tex,*bullet_tex;
vi2d MAP_SIZE = { 10,10 }; vi2d MAP_SIZE;
std::vector<std::vector<MapSquare>>map; std::vector<std::vector<MapSquare>>map;
std::map<EnemyID, EnemyData>enemyData; std::map<EnemyID, EnemyData>enemyData;
std::vector<Object>objects; std::vector<Object>objects;
std::vector<Bullet>bullets;
GAMEMODE mode=GAMEMODE::GAME; GAMEMODE mode=GAMEMODE::GAME;
Editor editor; Editor editor;
int level=1; int level=1;
@ -195,6 +202,8 @@ class FaceBall : public PixelGameEngine
Player player = { {3.7,0.3,0.7}, {{0.5,0.5},0.2} }; Player player = { {3.7,0.3,0.7}, {{0.5,0.5},0.2} };
vec3d freeRoamCamera = { 1,0.5,1 }; vec3d freeRoamCamera = { 1,0.5,1 };
float shotSpd = 1.5f;
vec3d Matrix_MultiplyVector(mat4x4& m, vec3d& i); vec3d Matrix_MultiplyVector(mat4x4& m, vec3d& i);
mat4x4 Matrix_MakeIdentity(); mat4x4 Matrix_MakeIdentity();
mat4x4 Matrix_MakeRotationX(float fAngleRad); mat4x4 Matrix_MakeRotationX(float fAngleRad);
@ -223,5 +232,7 @@ class FaceBall : public PixelGameEngine
void OnTextEntryComplete(const std::string& sText) override; void OnTextEntryComplete(const std::string& sText) override;
void InitializeEnemyData(); void InitializeEnemyData();
void LoadLevel(int level); void LoadLevel(int level);
bool CheckCollision(vec3d movementVector,vf2d pos,float radius); void RenderMesh(mat4x4&matView, std::vector<Triangle>&vecTrianglesToRaster,Object&o);
public:
static bool CheckCollision(vec3d movementVector,vf2d pos,float radius);
}; };
Loading…
Cancel
Save