Basic game phyics problem

Started by
3 comments, last by BeerNutts 12 years ago
So i have been reading lazy foo's tuts and been playing around with SDL, i have created a little thing where if you click the dot should fly that way and be pulled down by gravity (it is really simple) the idea is kinda like angry birds very basic concept, however i keep getting werid results such as going really fast, shooting up and gravity breaking. So i was woundering is someone could have a look and help thanks i think its today with these parts,

void Dot::handle_input()
{
if(shoot == false)
{
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
int tempX,tempY;
int Gradient;
tempX = event.button.x;
tempY = event.button.y; //x,y
Gradient = (ceil(tempY - y) / (tempX - x));
xVel = 50;
yVel = 50*Gradient;
shoot = true;

}
}
}
}

void Dot::handle_AI()
{
//Gravity
yVel = yVel + 1;
if((y >= SCREEN_HEIGHT) && (yVel >= 0))
{
yVel = 0;
}
}


if not this is everything http://pastebin.com/pfpd6LN5

Thanks for the help
Advertisement
It looks like you are approaching this like moving a point on a graph. At the moment, if a user clicks left of the dot, the dot would still move right, and the y would now go the opposite direction you intend because of change of sign in deltaX. It also seems to me that your gravity is substantially smaller than your shooting. If your yVel starts out at -50 (that would be when the user clicks a 45 degree angle with the dot), It won't halt it's journey upward until it is at 1275 pixels higher than where it started. If you did just over a 60 degree angle from the horizontal then it would be 5050 pixels (just over 12 and a half screens at 640x400 resolution). I would try increasing your gravity affects by a bit, and/or bumping down your launch velocity.

Your current initial velocity depends a lot on angle because you have a constant x velocity which the y velocity is based. If you want to better model projectile motion, you should have an initial velocity that does not depend on angle, and use that to calculate both x velocity and y velocity. By that I mean something similar to:

float initialVelocity = 50.0f;
float deltaX = tempX - x;
float deltaY = tempY - y;
float distance = sqrt(detaX*deltaX + deltaY*deltaY);
xVel = initialVelocity * deltaX/distance //deltaX/distance gets us the x component of a unit vector
yVel = initialVelocity*deltaY/distance //deltaY/distance gets us the y component of a unit vector


As for really fast... right now your movement is totally dependent on how fast your frames per second. I would recommend:

unsigned int currentTime = SDL_GetTicks();
float elapsedTime = ((float)oldTime-currentTime)/1000.0f; // in seconds
x += xVel * elapsedTime; //xvel is in pixels per second.
y+= yVel * elapsedTime;
oldTime = currentTime;
First,you must be sure that there has no problem in your code.The key is your algorithm.If your want to make a Physics Class game,your have to think about the Range Problem,you cannot let your dog jump or fly too fast to show it within the scene.The friends on top of me mentioned Direction Problem,but he hadn't solve the problem that
the dog fly too fast or breaking of gravity and so on.I can give you a current idea,here is my code //This is the unreal code
if(the dog.y >= ceiling)
{
the dog.y = ceiling +- the dog.height;
the dog.v -= the dog.v;
}
//and you can according to this concept,add wall or floor;

How to deal with the gravity,the gravity is closely linked to the floor.
If the dog fall to floor,dog cannot fall under the floor ,so you must add some code to that.The gravity must be remove,and the dog's place must be on the floor when it's under floor.
I haven't got any related code in my computer,but you can refer to my concept.
Thanks for the input guys. However i have wrote this and i have a problem, so any help would be great.

#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>

int main()
{
int x = 0;
int y = 380;
int clickX = 40;
int clickY = 360;
float angleRadians,angleDregees,maxVelocity;
float xVel,yVel;
int b;

maxVelocity = 100;

//The angle in radians
angleRadians = atan((y-clickY)/(clickX-x));
//The angle in degrees
angleDregees = (angleRadians * 180 / M_PI);
//velocitys
xVel = ceil(((90-angleDregees)/90)*maxVelocity);
yVel = ceil((angleDregees/90)*maxVelocity);

std::cout << "angle in radians :" << angleRadians << std::endl;
std::cout << "angle in degees :" << angleDregees << std::endl;
std::cout << "xVelocity :" << xVel << std::endl;
std::cout << "yVelocity :" << yVel << std::endl;
std::cin >> b;
}



It ethier gives me 100 xVel or 100 yVel or 50/50. When i change the values, thanks for any help :)

Thanks for the input guys. However i have wrote this and i have a problem, so any help would be great.

#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>

int main()
{
int x = 0;
int y = 380;
int clickX = 40;
int clickY = 360;
float angleRadians,angleDregees,maxVelocity;
float xVel,yVel;
int b;

maxVelocity = 100;

//The angle in radians
angleRadians = atan((y-clickY)/(clickX-x));
//The angle in degrees
angleDregees = (angleRadians * 180 / M_PI);
//velocitys
xVel = ceil(((90-angleDregees)/90)*maxVelocity);
yVel = ceil((angleDregees/90)*maxVelocity);

std::cout << "angle in radians :" << angleRadians << std::endl;
std::cout << "angle in degees :" << angleDregees << std::endl;
std::cout << "xVelocity :" << xVel << std::endl;
std::cout << "yVelocity :" << yVel << std::endl;
std::cin >> b;
}



It ethier gives me 100 xVel or 100 yVel or 50/50. When i change the values, thanks for any help smile.png


When working with floats or doubles, you should make your literals obviously floats; change 90 to 90.0f

However, why aren't you using sin and cos to determine the vector for y and x? 1st, look into using atan2, not atan. 2nd, don't convert from radians to degrees, 3rd, do this:

xVel = cos(angleDregees)*maxVelocity);
yVel = sin(angleDregees)*maxVelocity);

That should give you want you want.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement