Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  Trigonometry problem  (Read 1576 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2011-11-30 03:38:38 »

Game is here: http://www.javadaemon.com/games/breakout

However, that control scheme is hard. I want the balls new direction to reflect where it hit on the bat.

This is how I currently do it, and it does not work one bit.

1  
float degreesPerPixel = (float)180/player.getWidth();

The bat is 80 pixels long, so this should equal 2.25, which is does.

1  
int whereBallHit = (int) ((player.getX()+player.getWidth())-(ball.getX()+(ball.getWidth()/2)));

This bit should reflect the pixel coordinate of where the center of the ball hit the bat. It is counted from left to right because of the way degrees work. They start with 0 on the left, working over at 180 in the right.
This bit also works.

1  
float degreesApplied = degreesPerPixel * whereBallHit;

This bit also works as intended. It is the amount of degrees on the angle the ball should now be flying in.

1  
2  
ball.setXV((float)Math.cos(Math.toRadians(degreesApplied)) * 1.8f);
ball.setYV((float)Math.sin(Math.toRadians(degreesApplied)) * 1.8f);


This bit however, has drawn my attention because of not working. I'm suspecting wrong use of the Math functions. The ball does not make a lot of air after this bit. How can I fix this? Also, how would I set the generel speed of the ball?

Thanks guys Smiley

Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-30 04:16:44 »

Cos = X, Sin = Y, you are correct. But shouldn't you be adding cos(deg)*1.8 to the X and sin(deg)*1.8 to the Y? Wink

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2011-11-30 04:24:17 »

Cos = X, Sin = Y, you are correct. But shouldn't you be adding cos(deg)*1.8 to the X and sin(deg)*1.8 to the Y? Wink

It does work quite a lot better now. However, it will become VERY slow if one of the velocities gets really low. How can I work around this?
Keeping one generel speed. You know, either having XV = 5 and VY = 0 or XV = 2.5 and YV = 2.5, instead of one of them just disappearing.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2011-11-30 04:41:49 »

Doing this
1  
2  
ball.setXV(ball.getXV() + (float)Math.cos(Math.toRadians(degreesApplied)) * 1.2f);
         ball.setYV(ball.getYV() + (float)Math.sin(Math.toRadians(degreesApplied)) * 1.2f);

Still slows down to a crawl, and acts wierd. Angle is off, and it gets more and more off the longer I play.

Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-11-30 04:44:51 »

What is the V in the XV and YV? Velocity? Why are you adding distance to your velocity?

Pseudocode of what you should be doing:
1  
2  
3  
4  
5  
deltaX = cos(degree) * velocity.x;
deltaY = sin(degree) * velocity.y;

x += deltaX;
y += deltaY;

Offline Time_Waave

Junior Newbie





« Reply #5 - Posted 2011-12-07 23:31:18 »

When you described how degrees worked, I thought your problem might have been that you went the wrong way (0° is on the right, 180° is on the left) but your code looks fine.  I am unsure why you are multiplying the sines and cosines by 1.8 and 1.2, but I do know that for any right triangle where the angle is supplied, X = hypotenuse * cos(Θ) and Y = hypotenuse * sin(Θ), where X and Y denote velocity, Θ is the angle, and hypotenuse is the ball's total velocity.  Your code seems to keep X and Y velocity separate, whereas they should both be based off of a single absolute velocity.

Try this code, see if it works:
1  
2  
ball.setXV((float)Math.cos(Math.toRadians(degreesApplied)) * ballV);
ball.setYV((float)Math.sin(Math.toRadians(degreesApplied)) * ballV);


You can also easily change the ball's speed by changing ballV.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (492 views)
2018-06-10 19:43:48

EgonOlsen (593 views)
2018-06-10 19:43:44

EgonOlsen (406 views)
2018-06-10 19:43:20

DesertCoockie (743 views)
2018-05-13 18:23:11

nelsongames (1016 views)
2018-04-24 18:15:36

nelsongames (981 views)
2018-04-24 18:14:32

ivj94 (1537 views)
2018-03-24 14:47:39

ivj94 (557 views)
2018-03-24 14:46:31

ivj94 (1287 views)
2018-03-24 14:43:53

Solater (560 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!