Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (405)
games submitted by our members
Games in WIP (289)
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  
  Finding angles in an image?  (Read 1162 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Posted 2011-10-16 06:19:11 »

I'm working on a Scorched Earth / Worms type game.  The map is randomly generated "uneven" terrain.  Checking for collision is easy enough with .getRGB(), however I want some projectiles to bounce around before exploding.  How should I go about finding the angle of the surface that the projectile hit in a map like this? 

The blue background is a separate image from the ground graphic, and is not used for collision detection.  Also, when a projectile explodes, I will be erasing a circle out of the ground image.  I thought about using an array of lines to define the outline of the surface, but trying to figure out how to cut out a segment and adding lines for the hole that was created, sounds like a pain.  The easiest way seems to be scanning pixels around the projectile (after it collides with the ground) to try to figure out the angle, but then how far out should I check, and how would I figure what is too far out?  Has anyone here done something like this before?
Offline ReBirth
« Reply #1 - Posted 2011-10-16 06:37:32 »

For bounce you can implement physics library or do something harder. In my approach, I will identity each image based on the horizon so I can fugure out the angle given by colliding it. Use some standar small images for terrain like

####
# | #
# | #
####

####
#   /#
# /  #
####

Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #2 - Posted 2011-10-16 07:04:47 »

It's not so hard, actually. The easiest way to do it is just to count adjacent pixels and have pixels on the right do +1 to the slope and on the left -1, more or less. Depending on how many pixels you sample (3x3 or 4x4 usually) you can get more defined slopes.

Here is some code I have that I used to find angles in a game I made a while ago.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Vector hitVector = new Vector(0,0);
for(int i = 0; i < surrounding.length; i++)
{
   Point point = surrounding[i];

   if(image.getRaster().getPixel((int) c.x + point.x, (int) c.y + point.y,size)[3] == 0) continue;

   if(point.x < 0) hitVector.x += -1.0f / (-2.0f + point.x);
   if(point.x > 0) hitVector.x += -1.0f / (2.0f + point.x);
   if(point.y < 0) hitVector.y += -1.0f / (-2.0f + point.y);
   if(point.y > 0) hitVector.y += -1.0f / (2.0f + point.y);
}


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

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (60 views)
2013-05-17 12:29:12

alaslipknot (69 views)
2013-05-16 12:24:48

gouessej (99 views)
2013-05-15 15:53:38

gouessej (96 views)
2013-05-15 15:17:58

theagentd (107 views)
2013-05-15 06:01:13

theagentd (98 views)
2013-05-15 06:00:54

StreetDoggy (144 views)
2013-05-14 06:56:26

kutucuk (166 views)
2013-05-12 08:10:36

kutucuk (165 views)
2013-05-12 06:36:09

UnluckyDevil (175 views)
2013-05-11 20:09:57
Complex number cookbook
by Roquen
2013-04-24 03:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 07:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 07:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 07:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 08:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 07:17:38

Java Data structures
by Roquen
2013-03-29 04:21:12

Topic Request
by kutucuk
2013-03-22 12:42:01
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!
Page created in 0.381 seconds with 20 queries.