Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (602)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Bullet Directional Shooting Images [Need Help]  (Read 749 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann
« Posted 2013-08-15 16:43:56 »

Hello Everyone,

I need some help with something. Right now i have a play and i use the mouse to shoot the bullet. The bullet shoots towards the mouse fine and dandy. Basically I would like the change the image of the bullet when ever the bullet is at a certain Degree or Radian. Could someone help me. I supplied arrows and an image as to what i want to do.

Thank you.
Online Jimmt
« League of Dukes »

JGO Kernel

Medals: 162
Projects: 5
Exp: 3 years

« Reply #1 - Posted 2013-08-15 16:45:59 »

So, a bullet shooting left would look different than a bullet shooting right (besides the flip)? Or are you asking how to flip/rotate bullet image per direction?
Offline GlennBrann
« Reply #2 - Posted 2013-08-15 16:52:07 »

I have an image with 8 bullets inside of it. for all 8 directions, "North", "North-East", "East", "South-East", "South", "South-West", "West", and "North-Wast" like the black arrows in the images. I want to change the image when the mouse is at the "North-East" of the player or if the mouse is at the "South-East" of the player etc....

I am using this to shoot the bullet towards the mouse

rad = Math.atan2(mouseY - bullet.getY(), mouseX - bullet.getX())
dx = speed * Math.cos(rad);
dy = speed * Math.sin(rad);

This is how i change the direction of the bullet
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sirkarpfen

Junior Devvie

Medals: 2
Exp: 2 years

« Reply #3 - Posted 2013-08-15 17:03:08 »

Why change the image? You can also rotate it by the angle you just calculated. If you use pure Java2D you could do something like that:

AffineTransform at = new AffineTransform();
Offline GlennBrann
« Reply #4 - Posted 2013-08-15 17:08:27 »

It seems to be rotating everything on the screen for example the hud and stuff.
Offline sirkarpfen

Junior Devvie

Medals: 2
Exp: 2 years

« Reply #5 - Posted 2013-08-15 17:31:04 »

Ok then you need to create a separat AffineTransform like this:

// You create an identity transform (this should never change):
AffineTransform identity = new AffineTransform();
// You create another transform (used for rotating):
AffineTransform at= new AffineTransform();
// set the transform for rotating to the identity (do this with every bullet):
// rotate your image:
// Draw your image:
g2d.drawImage(image, at, this);

The AffineTransform class is quite complicated and i don't even understand it fully, but that would maybe do the trick Smiley.

Edit: changed the code because it won't do anything else than my code posted before xD
Offline epicpunnum

Junior Newbie

Exp: 2 years

A man with a plan.

« Reply #6 - Posted 2013-08-17 04:17:38 »

I think what you're looking for is:
//rotate the Graphics2D object around the center of your bullet object
g.rotate(angle,x+(bulletWidth/2), y+(bulletHeight/2));
//draw the bullet as you would normally
g.drawImage(bulletImage, x,y, null);
//rotate it back in the using negative angle
g.rotate(-angle,x+(bulletWidth/2), y+(bulletHeight/2));

It's just a matter of rotating it back. Smiley
However, depending on your usage of rotation (especially with larger images), sometimes it is logical to cache an pre-rendered image array of,say every 5 or 10 degrees, so that you don't have to do the rotation each time.

Also, just food for thought, you could also render the bullet as just a line if you don't want to deal with all that.

Currently designing a puzzle-horror game called SLEEP in my free time.
I also draw and animate stuff on Youtube. Cheesy
Offline SHC
« Reply #7 - Posted 2013-08-17 05:05:45 »

A free bullet image by me.

And to rotate use @epicpunnum's method.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (15 views)
2015-07-29 04:35:27

Riven (37 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (29 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!