|
-
March 23rd, 2012, 05:30 PM
#1
Need help with physics for this engine. I will explain in thread.
Thanks for clicking this post, I'll get right down to business.
I'm creating a physics engine for class.
The overall idea is a 2d screen with a spaceship flying around and asteroids.
You can rotate the spaceship and thrust (thrusting gives a force in a certain direction of 1).
I'm having a lot of trouble with the physics.
What I'm doing:
The force is 1 in a certain direction. I'm attempting to compute that force into a force for x and a force for y, which I can later translate into a velocity for each x and y.
I have the game running correctly for what I've done, but I just realized that the spaceship always goes at nearly a 45 degree angle (albeit in the correct general direction). If it's straight up, it goes straight up, same with straight down, left, and right. But if it's inbetween, it goes at a 45 degree angle.
I'll show my code, it's pretty sloppy because I've been trying to throw it all together for days.
Note: This is just the engine.cpp part of the code. Also, the origin of (0,0) is in the top left instead of the bottom left.
Code:
#include <vector>
#include <string>
#include "constants.h"
using namespace std;
void crunchCommand(string command, int pushCount, vector<int> id, vector<float> & x, vector<float> & y, vector<int> & angle, vector<float> & velX, vector<float> & velY, vector<float> & forceX, vector<float> & forceY)
{
float tempForce = 0;
for(int i = 0; i <= pushCount; i++)
{
forceX[i] = 0;
forceY[i] = 0;
}
if(command == "right")
{
if(angle.front() + 1 <= 71) //if a turn right doesn't overflow the 72 limit, turn right one.
angle.front() = angle.front() + 1;
else //else it does overflow, so wrap back to the first angle.
angle.front() = 0;
}
else
if(command == "left")
{
if(angle.front() - 1 >= 0) //if a turn left doesn't go below 0 (the first angle), turn left one.
angle.front() = angle.front() - 1;
else //else it does go below 1, so wrap back to the last angle (72)
angle.front() = 71;
}
else
if(command == "space")
{
//shoot a bullet .. currently using this for stopping the ship to help test for bugs
velX[0] = 0;
velY[0] = 0;
}
else
if(command == "up")
{
if(angle[0] == 0)
forceY[0] = -1;
else
if(angle[0] == 36)
forceY[0] = 1;
else
if(angle[0] == 18)
forceX[0] = 1;
else
if(angle[0] == 54)
forceX[0] = -1;
else
{
forceX[0] = sin(angle[0] * 5 * (PI / 180)) * 1; //force of 1 per tick
forceY[0] = -cos(angle[0] * 5 * (PI / 180)) * 1; //force of 1 per tick
}
}
/*
//add gravity pulls to forces
for(int i = 0; i <= pushCount; i++) //add gravity pulls for each object
{
for(int u = 1; u <= pushCount; u++) //each object should add a force for every other object
{
switch(id[i])
{
case ASTEROID_ID:
switch(id[u])
{
case ASTEROID_ID:
break;
case SPACESHIP_ID:
break;
}
break;
case SPACESHIP_ID:
switch(id[u])
{
case ASTEROID_ID:
break;
case SPACESHIP_ID:
break;
}
break;
}//id[i]tempForce += GRAV_CONST *
}
}
*/
//compute force to velocity
velX[0] += (forceX[0] / SPACESHIP_MASS) * 0.1;
if(velX[0] > MAX_VELOCITY)
velX[0] = MAX_VELOCITY;
else
if(velX[0] < MIN_VELOCITY)
velX[0] = MIN_VELOCITY;
velY[0] += (forceY[0] / SPACESHIP_MASS) * 0.1;
if(velY[0] > MAX_VELOCITY)
velY[0] = MAX_VELOCITY;
else
if(velY[0] < MIN_VELOCITY)
velY[0] = MIN_VELOCITY;
for(int i = 1; i <= pushCount; i++)
{
velX[i] += (forceX[i] / ASTEROID_MASS) * 0.1;
velY[i] += (forceY[i] / ASTEROID_MASS) * 0.1;
}
//change position of x and y
for(int i = 0; i <= pushCount; i++)
{
if((x[i] + velX[i]) > SCREEN_WIDTH) //if x gets bigger than maxX, wrap
x[i] = velX[i] - SCREEN_WIDTH - x[i];
else
if(x[i] + velX[i] < 0)
x[i] = SCREEN_WIDTH + velX[i] + x[i]; //if x gets smaller than minX, wrap
else
x[i] += velX[i];
if((y[i] + velY[i]) > SCREEN_HEIGHT) //if y gets bigger than maxY, wrap
y[i] = velY[i] - SCREEN_HEIGHT - y[i];
else
if(y[i] + velY[i] < 0)
y[i] = SCREEN_HEIGHT + velY[i] + y[i]; //if y gets smaller than minY, wrap
else
y[i] += velY[i];
}
}
I know the including of string isn't necessary, I just haven't changed it. I also know a lot of the code is redundant, but I'll clean it once it's working.
Any help will be greatly appreciated. I have almost no hope at this point.
(I do have a general idea on how to get the gravity working for each object, I've just yet to implement it.. I'd rather fix the 45 degree bug first)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|