View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Basic game phyics problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Rya729  Members

Posted 26 March 2012 - 10:19 AM

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;
tempX = event.button.x;
tempY = event.button.y;  //x,y
Gradient = (ceil(tempY - y) / (tempX - x));
xVel = 50;
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

### #2Zael  Members

Posted 26 March 2012 - 11:33 AM

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;


### #3MotorBully  Members

Posted 27 March 2012 - 08:18 AM

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.

### #4Rya729  Members

Posted 28 March 2012 - 11:40 AM

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 xVel,yVel;
int b;

maxVelocity = 100;

//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 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

### #5BeerNutts  Members

Posted 28 March 2012 - 02:52 PM

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 xVel,yVel;
int b;

maxVelocity = 100;

//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 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

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)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.