Allegro.cc - Online Community

Allegro.cc Forums » The Depot » projectile physics

This thread is locked; no one can reply to it. rss feed Print
projectile physics
altalena
Member #13,639
October 2011
avatar

{"name":"607671","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/1\/0103dee20ab34629b707f92cac41a18a.png","w":2560,"h":1440,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/1\/0103dee20ab34629b707f92cac41a18a"}607671

I'm trying to eventually make something that resembles Shadow of Chernobyl. Here's the projectile physics. There are no collisions yet, but I'm planning to have projectiles collide with the terrain by finding out each frame if it's height is above or below the terrain with the same x and y as the projectile.

I'll try to upload an OpenGL template program later, if anyone wants to help me with implementing textures in it that would be cool, since I suck at learning new things in programming and learn best by example.

edit: Here's the template program. I've improved the Perlin noise 2d algorithm to repeat every 65536 units instead of 256, and to behave the same with negative inputs as with positive inputs.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Arthur Kalliokoski
Second in Command
February 2005
avatar

I got the NoisyTrees example to compile and run on linux by setting bool compatibility to 1, even though I'm not using an analog monitor. Otherwise it just said "Failed to create display". The template example wouldn't run with compatibility = 1.

It appears the projectiles are dropping off in the distance due to gravity?

To use textures:

#SelectExpand
1 terrainimg = al_load_bitmap(terrain_name); 2 if(!terrainimg) 3 { 4 //al_show_native_message_box(0,"Error","Unable to load terrain file","",NULL,0); 5 fprintf(stderr,"Can't load terrain image\n"); 6 return 1; 7 } 8 9 terrainimgID = al_get_opengl_texture(terrainimg); 10 glBindTexture(GL_TEXTURE_2D,terrainimgID); 11 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 12 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 13 14 //Assuming you have one large image for terrain 15 terrainimg = al_load_bitmap(terrain_image_filename); 16 if(!terrainimg) 17 { 18 //al_show_native_message_box(0,"Error","Unable to load terrain file","",NULL,0); 19 fprintf(stderr,"Can't load terrain image\n"); 20 return 1; 21 } 22 23 terrainimgID = al_get_opengl_texture(terrainimg); //It needs a number to associate which image you want to show at the moment 24 glBindTexture(GL_TEXTURE_2D,terrainimgID); //the actual association 25 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //make it look nicer 26 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 27 glEnable(GL_TEXTURE_2D); 28 . 29 . 30 . 31 glColor4f(1.0,1.0,1.0,1.0); //leave color full strength, the image will do the colors now 32 33 //in the display loop 34 35 glBegin(GL_TRIANGLES); 36 37 //glColor4f(0.2f,0.2f,0.2f,1.0f); 38 glTexCoord2f(float x, float y); //where x and y vary across the texture (no matter what size) from 0.0 to 1.0 39 //The x is 0.0 at left edge, 1.0 at right edge. 40 //The y is 0.0 at bottom edge, 1.0 at top edge. Or you could flip the image in a paint app. 41 glVertex3d(wc[0],wc[2],wc[4]);

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

altalena
Member #13,639
October 2011
avatar

Thanks for the help, your code showed me that my problem is with al_load_bitmap.
al_load_bitmap("c:\noise.png") and al_load_bitmap("noise.png") both return zero, and i've got a copy of the .png in both the .exe folder and in the root of my c drive. I've attached the updated code. Maybe it works in Linux!

The program uses 9.8 m/s^2 to simulate gravity, and uses a formula for parasitic drag forces to slow down the projectile. I'm using a separate camera to handle the gun, but the same camera as the terrain to handle the projectiles, as I just accidentally figured out how to rotate points using a pre-computed matrix, and that's what I use to make the projectile start where the muzzle is, instead of the middle of the screen. I just gotta use that matrix to make the pistol use the same camera as the terrain and icosahedron-bullets.

This explanation should help anyone who doesn't get vertex rotation using matrices:
generate a unit vector starting from your eye, going directly to your right. now make another going straight up, and another straight ahead. multiply your vertex's x coordinate by the components of the right vector, the y by the components of the up vector, and the z by the components of the forward vector. add all three vectors, and you've rotated your point.

I'm glad I finally figured that out, I'd given up hope of understanding how matrix multiplication could rotate a set of points.

edit: the parasitic drag was making the bullets follow a weird bendy trajectory, fixed!

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Arthur Kalliokoski
Second in Command
February 2005
avatar

Here's an example that came from some 20 year old Abrash post on Dr. Dobbs or something.

#SelectExpand
1//Multiply a point (4x1 matrix) by an OpenGL matrix to transform/rotate into correct camera position 2void xform_vertex_to_mat4(float *matrix, float *vecin, float *vecout) 3{ 4 vecout[0] = 5 vecin[0]*matrix[0]+ 6 vecin[1]*matrix[4]+ 7 vecin[2]*matrix[8]+ 8 matrix[12]; 9 10 vecout[1] = 11 vecin[0]*matrix[1]+ 12 vecin[1]*matrix[5]+ 13 vecin[2]*matrix[9]+ 14 matrix[13]; 15 16 vecout[2] = 17 vecin[0]*matrix[2]+ 18 vecin[1]*matrix[6]+ 19 vecin[2]*matrix[10]+ 20 matrix[14]; 21}

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

altalena
Member #13,639
October 2011
avatar

Wow i feel stupid for not figuring this matrix vector thing out sooner. One less thing on my to-do list i guess.

            unit_vector[0]=1.0*cos(phi*M_2PIE/400.0)*cos(theta*M_2PIE/400.0);
            unit_vector[1]=1.0*sin(phi*M_2PIE/400.0)*cos(theta*M_2PIE/400.0);
            unit_vector[2]=1.0*sin(theta*M_2PIE/400.0);
            unit_vector[3]=1.0*cos((phi-100.0)*M_2PIE/400.0)*cos(theta*M_2PIE/400.0);
            unit_vector[4]=1.0*sin((phi-100.0)*M_2PIE/400.0)*cos(theta*M_2PIE/400.0);
            unit_vector[5]=1.0*sin(theta*M_2PIE/400.0);
            unit_vector[6]=1.0*cos(phi*M_2PIE/400.0)*cos((theta+100.0)*M_2PIE/400.0);
            unit_vector[7]=1.0*sin(phi*M_2PIE/400.0)*cos((theta+100.0)*M_2PIE/400.0);
            unit_vector[8]=1.0*sin((theta+100.0)*M_2PIE/400.0);

That's how to make the unit vectors for 2-axis rotation, I'm using 400ths of a circle to avoid rounding errors from using radians. Obviously, use 360 for 400 and 90 for 100 if you're using degrees. 0-2 is out vector, 3-5 is right vector, and 6-8 is up vector.

I have no idea how to get the image to load in my program, however.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Arthur Kalliokoski
Second in Command
February 2005
avatar

altalena said:

I have no idea how to get the image to load in my program, however.

If you're using an IDE, it's probably messing up your current directory, so try this
  ALLEGRO_PATH *path;
  path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
  al_change_directory(al_path_cstr(path,ALLEGRO_NATIVE_PATH_SEP));

What? You already have allegro_image.h in your include list.

  ALLEGRO_BITMAP *random_image_ptr;
  random_image_ptr = al_load_bitmap("my_random_image.bmp");
  if(random_image_ptr == 0)
  {
     //al_show_native_message_box(0,"Error","Unable to load terrain file","",NULL,0);
     fprintf(stderr,"Can't load terrain image\n");
     return 1;
  }

[EDIT]

Oh, yeah, you'll have to link in allegro_image.lib or whatever depending on your compiler.

[EDIT2]

FIXED: I should have stuck in the path stuff before the al_load_bitmap part :-/

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

altalena
Member #13,639
October 2011
avatar

Ok. I'm linking in C:\allegro\liballegro_image-5.0.8-static-mt.a and I'm linking to all the other static libraries suggested by the code::blocks+mingw page on the wiki.

I'm compiling the project and then running the .exe from an icon pinned to my w7 task bar. I put in the code

  ALLEGRO_PATH *path;
  path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
  al_change_directory(al_path_cstr(path,ALLEGRO_NATIVE_PATH_SEP));

before trying to load the bitmap.

I've attached my latest code, attempting to get this working. I appreciate the help.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Arthur Kalliokoski
Second in Command
February 2005
avatar

I can't get it to load either, and I've added al_init_image_addon(), removed the memory bitmap option, and have a "noise.png" (512x512, 24bpp) that loads fine in other A5 programs.
I also tried a "noise.bmp" (windows bitmap).

By compiling the debug stuff, I get an allegro.log which says "No handler for bitmap extensions .bmp - therefore not trying to load noise.bmp". Why don't you get the examples and work with and extend those, specifically ex_gldepth.c?

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

altalena
Member #13,639
October 2011
avatar

Thanks! The example program ex_bitmap.c compiles and runs with my current setup. I'll update as I try to extend the example into the program I'm using. With luck, it should be as easy as getting the bitmap to load.

edit1: the noise.png loads in the example, so it's not a problem with the image file.

edit2: My program failed because I forgot al_init_image_addon();. Hilarious. Now the image loads.

edit3: My polys are textured now, just with text instead of the nice repeating perlin noise .png that I made.

edit4: gun and bullets textured now.

edit5: Textures are working!!! Sort of. They aren't being modified by glColor4d. It's probably a setting in glTexParameteri or glTexEnvi. Also I figured out how to turn texturing on and off. Does it work for you on Linux Arthur?

edit6: Working completely! Thanks for all the help. Too many edits?

edit7: forgot the .tif file, it goes in the program folder.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Go to: