Advertisement

Basic phisics and colisions

Started by August 20, 2001 04:03 PM
5 comments, last by MAD_Mask 23 years, 3 months ago
I newbe so... I understand that this is the place to post.. ( for now anyway ) I am writeing a 2D game (like that old Arc-Voley) and it involves a ball that gets up in the air... 1. How do I calculate the ball''s trajectory taking in acount gravitation and ball''s speed . For the movement of the ball I use iBallX and iBallY as coordinates and add or substract 1 to obtain somme offset. But could somme one show me a more good way to calculate the new iBallX and iBallY than ''+1'' and ''-1'' operations? ( sommething like angles, orientation vectors and staff like that ) 2. And second, if you some one can , a litle input on colisions detections in the same context( gravitation and speed ) ? I realy need your help , because I''dd realy like to make this in the right way .... so please "F1" me The Mask
1) define a constant gravity factor. in real life it''s 9.8 k/s/s or something like that. then give a vector to your ball. each frame if the ball is not acted upon by an outside outject (besides gravity) then you recaulate the vector based the the gravity factor. otherwise you change the vector based on how it is acted upon.
2) bounding sphere would be easiest with a ball. check out some tutorials on Flipcode.com



How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Advertisement

You are going to need to give your ball a velocity. Use a struct to hold your ball data:

struct Ball{    float pos_x;    float pos_y;    float vel_x;    float vel_x;    float radius;}Ball ball; 


So you have position components (x and y) and velocity components (x and y) and a radius.

To make the ball go a certain direction, just modify it''s velocity. So let''s say you wanted to make it go at a 30 degree angle at 2 pixels per update:

angle = 6.283185 * 30.0 / 360.0;ball.vel_x = 2.0*cos(angle);ball.vel_y = 2.0*sin(angle); 


To move your ball according to it''s velocity, do this, once per game update:

ball.pos_x += ball.vel_x;ball.pos_y += ball.val_y; 


Also to make it act like gravity is pulling on it, do this once per update:

#define GRAVITY  (0.5)ball.vel_y += GRAVITY;  


You could use a const instead of a define. Also try values other than 0.5.
First I whant to apologise for that double thread I started.It may internet conection that gives me trouble. Sorry again.

Anon. Poster:
Thanks for the input. I had a similar struct but no radius.
And the number "6.283185" is, in fact 2*pi ? I think so , anyway may math formula is like that.
I''ll make the changes in my code to make rome for a radius.
But today I foud another problem .. "BltFast" wants integers or
long integers for the x, y parameters wich defines where to put my sprite. So? what I am going to do ? for now I converted the
parameteres to int " BltFast( (int) Player.dcurrX,
(int) Player.dcurrY, ...etc. )
Julio:
Thank you for your replay , and flipcode page is good stuff
much , very much to learn from that page....( I added to my favorites )

I am working very hard in this game but is dificult for me because is my second game in DX ( I made a very ugly tetris clone last week ).
So , I caunting on all of you to give me a help when needed
And sorry for my spelling, I am not so great at writeing in english ,( but I am working at that to )



Ok , so I managed to put some gravity in my game ( I am still adjusting it ) but now I am having problems with the player coliding with the ball .
I made a function that ( tries to ) manages ball vs player colisions and it has many if like
" if( ball.dCurrY + BALL_DIAMETER > player.dCurrY )
if ( ball.dCurrX +.... " well you get the ideea....
Well there are 2 problems :
- the function is not very elegant , ( lots cheks like
if(e < x+4 - and most importantly , the function works randomly. ( ratio 2 from 10 , I know , I know it is not very good )
The reason I post is mainly for the last problem: Can somebody tell me more about this colisions....?
I read what Julio sugested at flipcode.com , but there were more advanced thing (like 3D stuff ) and I didn''t understud much.


Advertisement

Hei, I''d realy need your help.....

This topic is closed to new replies.

Advertisement