# Basic game phyics problem

## Recommended Posts

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,
[code]
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;
}
}
[/code]

if not this is everything [url="http://pastebin.com/pfpd6LN5"]http://pastebin.com/pfpd6LN5[/url]

Thanks for the help

#### Share this post

##### Share on other sites
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:
[code]
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
[/code]

As for really fast... right now your movement is totally dependent on how fast your frames per second. I would recommend:
[code]
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;
[/code]

#### Share this post

##### Share on other sites
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 [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;
[/code]
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.

#### Share this post

##### Share on other sites
Thanks for the input guys. However i have wrote this and i have a problem, so any help would be great.
[code]
#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;
}

[/code]

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

#### Share this post

##### Share on other sites
[quote name='Rya729' timestamp='1332956417' post='4926065']
Thanks for the input guys. However i have wrote this and i have a problem, so any help would be great.
[code]
#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;
}

[/code]

It ethier gives me 100 xVel or 100 yVel or 50/50. When i change the values, thanks for any help [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

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:
[code]
xVel = cos(angleDregees)*maxVelocity);
yVel = sin(angleDregees)*maxVelocity);
[/code]
That should give you want you want.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

• ### Forum Statistics

• Total Topics
627744
• Total Posts
2978895

• 10
• 10
• 21
• 14
• 14