Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Shooting Ball From Rotating Bitmap

Credits go to Dizzy Egg for helping out!
This thread is locked; no one can reply to it. rss feed Print
Shooting Ball From Rotating Bitmap
Scooter
Member #16,799
January 2018

Hi all:
I hate to post again so soon, but I have another
question:
I have this bitmap rotating at center of screen.
I want to shoot a ball from the bitmap across the
screen at the angle of the rotating bitmap. I am
pretty close but something is not right with what
I have now. I have uploaded the two files above
so you can see what I have now. It should be very
simple but in two days I haven't a clue what is
wrong. If you could point me in the right direction,
that would be great.

Thanks for your time!

DanielH
Member #934
January 2001
avatar

Each update should be off the last x and y, not new x,y.

ball_x = ball_x + cos(angle) * speed;

And the draw should be based off the current x and y.

Save yourself some hassle and pre-convert your pi/180 or 180/pi. Just a suggestion. Saves writing time.

#define PI_OVER_180 0.017453292

Actually confused as to why you have ball_x/y and new_x/y. What is the purpose of each?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Hi Scooter, nice to see you again.

So, to clear up a few things, you're mixing ball and turret updates together. The angle of the ball shouldn't change once it is fired. Track the start x and y of the ball and get the speed * delta time with the original firing angle to get the new position.

8-)

Scooter
Member #16,799
January 2018

Hi Daniel and Edgar:
Thanks for your replies!
I have posted an update using the instructions.
You can see my changes. I am now shooting the ball.
Great relief! But I still have a problem,
If I comment out 'updating the turret angle' and then press
the SPACE Key the ball will fire to the right like
it is supposed to. If I allow to turret to rotate it appears
the ball is not rotating with the angle of the turret. When
I press the SPACE Key the ball fires at a different angle.
Kinda crazy really. I still can't see what is going on! If
you run my update you will see what I mean!
No hurry, just when you have time!

Thanks for your time!!!

I know exactly what I need to do, but I can't make it happen!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If the ball is not centered on the turret, say because it's on the end, then you need to account for that. What is the length of the turret? Use trig to get the end of the turret. That is where the ball is when it has not been fired. When it is fired, store the position and angle of the ball.

Scooter
Member #16,799
January 2018

Hi Edgar:
Having no luck at all today.
Did you take a look at my update I posted above?
The trouble I am having is the ball position is
not following the turret. The ball_angle should
be the same as the turret_angle which is angleX
incremented at 2 degrees converted to radians.
The turret is located at center of screen at:

x = 640
y = 360
ball location at 690

turret is 100 x 80

2D rotations have always been a problem for me!
3D rotations are NOT this big of a problem!
If you remember, some time ago I was asking about
unfolding a 3D cube. Had no problem doing that'
I will be back at it again tomorrow to see if
I can solve the problem.

Thanks for your time.

Dizzy Egg
Member #10,824
March 2009
avatar

Hello Scooter, I had a play around with your code. It looked like you were updating the ball angle and new_x/y every frame, whcich I couln't really follow. Also it looked like the ball just appeared then disappeared?

I have changed some code and made a new function start_shoot_ball() that starts the ball firing when you press space.

Try my code and see if it's closer to what you wanted? I don't know if you want the ball to keep following the turret angle after firing, that seems odd but, let me know!!

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3#include <allegro5/allegro_image.h> 4#include <stdio.h> 5#include <stdlib.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8#include <math.h> 9#include <stdbool.h> 10 11#define PI 3.1415927 12 13float screen_width = 1280; 14float screen_height = 720; 15float image_width, image_height; 16 17float ball_x = 640; 18float ball_y = 360; 19float turret_x = 620; 20float turret_y = 360; 21float ball_speed = 10; 22 23bool done = false; 24bool shoot_ball = false; 25bool start = true; 26 27float angleX = 0; 28float ball_angle = 0; 29float turret_angle = 0; 30float dist = 50.0; 31 32int ball_dia = 6; 33 34void start_shoot_ball(); 35void update_turret_angle(); 36void update_ball(); 37 38int main() 39{ 40 ALLEGRO_DISPLAY* display = NULL; 41 ALLEGRO_BITMAP* turret = NULL; 42 ALLEGRO_TIMER* timer = NULL; 43 ALLEGRO_EVENT_QUEUE* event_queue = NULL; 44 45 if (!al_init()) 46 { 47 fprintf(stderr, "Couldn't initialize allegro!\n"); 48 return -1; 49 } 50 51 if (!al_install_mouse()) 52 { 53 fprintf(stderr, "failed to initialize the mouse!\n"); 54 return -1; 55 } 56 57 if (!al_init_primitives_addon()) 58 { 59 fprintf(stderr, "Couldn't initialize primitives addon!\n"); 60 return -1; 61 } 62 63 display = al_create_display(screen_width, screen_height); 64 65 if (!display) 66 { 67 fprintf(stderr, "Couldn't create allegro display!\n"); 68 return -1; 69 } 70 71 if (!al_init_image_addon()) 72 { 73 fprintf(stderr, "Couldn't display image!\n"); 74 return -1; 75 } 76 77 al_install_keyboard(); 78 al_init_font_addon(); 79 al_init_ttf_addon(); 80 al_install_mouse(); 81 82 turret = al_load_bitmap("turret.png"); 83 image_height = al_get_bitmap_height(turret); 84 image_width = al_get_bitmap_width(turret); 85 86 timer = al_create_timer(1.0 / 60); 87 ALLEGRO_KEYBOARD_STATE keyState; 88 event_queue = al_create_event_queue(); 89 90 al_register_event_source(event_queue, al_get_display_event_source(display)); 91 al_register_event_source(event_queue, al_get_mouse_event_source()); 92 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 93 al_register_event_source(event_queue, al_get_keyboard_event_source()); 94 95 al_clear_to_color(al_map_rgb(127, 128, 0)); 96 al_flip_display(); 97 al_start_timer(timer); 98 int done = false; 99 100 while (!done) 101 { 102 ALLEGRO_EVENT ev; 103 al_wait_for_event(event_queue, &ev); 104 al_get_keyboard_state(&keyState); 105 106 if (ev.type == ALLEGRO_EVENT_TIMER && ev.timer.source == timer) 107 { 108 al_clear_to_color(al_map_rgb(127, 128, 0)); 109 110 angleX = (turret_angle * PI) / 180.0; //convert angle to radians 111 al_draw_rotated_bitmap(turret, 50, 40, turret_x, turret_y, -angleX, 0); 112 113 update_turret_angle(); 114 115 if (shoot_ball == true) 116 { 117 update_ball(); 118 al_draw_filled_circle(ball_x, ball_y, ball_dia, al_map_rgb(255, 0, 0)); 119 } 120 121 al_flip_display(); 122 } 123 124 125 126 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 127 { 128 switch (ev.keyboard.keycode) 129 { 130 case ALLEGRO_KEY_ESCAPE: 131 done = true; 132 break; 133 134 case ALLEGRO_KEY_SPACE: 135 al_get_keyboard_state(&keyState); 136 start_shoot_ball(); 137 break; 138 139 } 140 141 } 142 143 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 144 145 { 146 done = true; 147 break; 148 } 149 } 150 151 al_destroy_display(display); 152 al_destroy_bitmap(turret); 153 al_destroy_timer(timer); 154 al_destroy_event_queue(event_queue); 155 return 0; 156} 157 158void start_shoot_ball() 159{ 160 shoot_ball = true; 161 ball_angle = (turret_angle * PI) / 180.0; 162 ball_x = 620 + (dist * cos(angleX)); 163 ball_y = 360 - (dist * sin(angleX)); 164} 165 166void update_turret_angle() 167{ 168 turret_angle += 2.0; 169 if (turret_angle >= 360) turret_angle = 2.0; 170} 171 172void update_ball() 173{ 174 ball_x += cos(ball_angle) * ball_speed; 175 ball_y -= sin(ball_angle) * ball_speed; 176 177}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Scooter
Member #16,799
January 2018

Hi Dizzy:
This looks GREAT!
I finally got the ball to track the turret but I was
never able to shoot the ball with the Space Key.
The only things I changed was:

I changed the ball speed to 15.
I advanced the turret angle 10 degrees to shoot the ball.

This made the ball track the turret angle closer. I might
still be off a little on that.

Everything seems to be perfect, just what I wanted.
Great job, as usual!

Thanks, have a great day!

Go to: