- #1
Xcrypt
- 21
- 0
Hello, I am studying game development, I'm in my first year and have to make a 2D game in C++ for a project. I'm sorry if this is considered homework and thus posted it in the wrong board. Also my knowledge of physics is very basic so please excuse me for my amateurism. I tried to contact my teachers about this problem but they don't seem to understand it.
The game is a 2D shooter game in space, where you have control over a tank as player. In one level, there are different planets. The projectiles the tank shoot follow paths based on the addition of multiple 'planet-object' attraction force vectors. The tank itself also has to be attracted to the planets. I will show you some code of how I did this:
it is based on the formula: F=G*((m1*m2)/r^2)
Vector2D PlanetList::CalculateGravityVector(Point2D ObjectPosition, double ObjectMass, InputState* inputStatePtr)
{
//Calculate vector: the addition of all attraction force vectors of the object to a specific planet
double F,distance,angle;
Point2D PlanetPosition;
Vector2D vdistance, tempGravityVector;
Vector2D GravityVector(0,0);
Vector2D AngleZero(1,0);
for (int x=0 ; x<MAXNRPLANETS ; ++x)
{
if (m_PlanetPtrArr[x]!=0)
{
//First: Calculate the attraction force between the object and the [x]'nd planet in the list
//F = G * ((m1*m2)/r^2)
PlanetPosition = m_PlanetPtrArr[x]->GetPosition();
vdistance = PlanetPosition-ObjectPosition;
distance = vdistance.Length();
F = m_GravitationConstant*((ObjectMass*m_PlanetPtrArr[x]->GetMass())/(distance*distance));
//Put the Force in a Vector2D
//To achieve that, calculate the angle first
angle = vdistance.AngleWith(AngleZero);
tempGravityVector.x = cos(angle)*F;
tempGravityVector.y = sin(angle)*F;
//Add this vector to the Vector2D we need.
GravityVector+=tempGravityVector;
}
}
return GravityVector;
}
This function will be implemented in a Tick() function, this means that in the program, the vector will be calculated for every tick (usually 1/60 s).
I think I have this part right. But I also want to implement a force (I think it is called normalforce), that makes it so when the tank and the planet collide, that makes the tank unable to fall through the crust of the planet.
My question is: what kind of force is this, and how do I calculate this?
(I made a quick sketch of the problem in paint, check attachments.)
At first I thought this was Fn = -Fz, but then I realized this wouldn't work, because suppose the tank is moving at 200pixels/second, with an acceleration of only 2px/s^2, and a mass of 500 units, only the acceleration and the mass play a role, meaning that the velocity of the tank would not increase anymore, but still stay at 200px/s when the tank collides with the crust of a planet.
Help is much appreciated.
The game is a 2D shooter game in space, where you have control over a tank as player. In one level, there are different planets. The projectiles the tank shoot follow paths based on the addition of multiple 'planet-object' attraction force vectors. The tank itself also has to be attracted to the planets. I will show you some code of how I did this:
it is based on the formula: F=G*((m1*m2)/r^2)
Vector2D PlanetList::CalculateGravityVector(Point2D ObjectPosition, double ObjectMass, InputState* inputStatePtr)
{
//Calculate vector: the addition of all attraction force vectors of the object to a specific planet
double F,distance,angle;
Point2D PlanetPosition;
Vector2D vdistance, tempGravityVector;
Vector2D GravityVector(0,0);
Vector2D AngleZero(1,0);
for (int x=0 ; x<MAXNRPLANETS ; ++x)
{
if (m_PlanetPtrArr[x]!=0)
{
//First: Calculate the attraction force between the object and the [x]'nd planet in the list
//F = G * ((m1*m2)/r^2)
PlanetPosition = m_PlanetPtrArr[x]->GetPosition();
vdistance = PlanetPosition-ObjectPosition;
distance = vdistance.Length();
F = m_GravitationConstant*((ObjectMass*m_PlanetPtrArr[x]->GetMass())/(distance*distance));
//Put the Force in a Vector2D
//To achieve that, calculate the angle first
angle = vdistance.AngleWith(AngleZero);
tempGravityVector.x = cos(angle)*F;
tempGravityVector.y = sin(angle)*F;
//Add this vector to the Vector2D we need.
GravityVector+=tempGravityVector;
}
}
return GravityVector;
}
This function will be implemented in a Tick() function, this means that in the program, the vector will be calculated for every tick (usually 1/60 s).
I think I have this part right. But I also want to implement a force (I think it is called normalforce), that makes it so when the tank and the planet collide, that makes the tank unable to fall through the crust of the planet.
My question is: what kind of force is this, and how do I calculate this?
(I made a quick sketch of the problem in paint, check attachments.)
At first I thought this was Fn = -Fz, but then I realized this wouldn't work, because suppose the tank is moving at 200pixels/second, with an acceleration of only 2px/s^2, and a mass of 500 units, only the acceleration and the mass play a role, meaning that the velocity of the tank would not increase anymore, but still stay at 200px/s when the tank collides with the crust of a planet.
Help is much appreciated.
Attachments
Last edited: