Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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 565 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: 139
Projects: 4
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

1  
2  
3  
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:

1  
2  
3  
AffineTransform at = new AffineTransform();
at.rotate(angle);
g2d.transform(at);
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
// 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):
at.setTransform(identity)
// rotate your image:
at.rotate(angle);
// 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:
1  
2  
3  
4  
5  
6  
//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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (67 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (12 views)
2014-11-26 15:20:08

SHC (25 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (30 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (53 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!