Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Sin and Cos calculations for "realistic" zero-gravity movement in a game

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, CodeRanch - first timer here!

I have this book where we are working through a simple Space Invaders-like game. I have all of the core functionality of the game working, but I am having some issues with controls.
I am using KeyListeners for input, and then based on the Space-ship's faceAngle, moveAngle and some nifty sin and cos functions, I actually do the movement/acceleration of the space-ship.
However, I am facing a problem:
Horizontal movement is working good as it should, but as soon as I tilt the ship upwards or downwards by 1 degree, the acceleration does not work correctly.

Here's my KeyListener-code that handles all of this:
http://pastebin.com/HGfexDX9
(incVelX and Y just adds the incoming value to the VelX, instead of changing it).

You can check out the whole source-code here:
https://github.com/vegarab/asteroids

I believe the issues are within my sin and cos functions at the bottom there, but I can't seem to figure out why they are not able to calculate the correct acceleration vertically.
Thank you!

 
Saloon Keeper
Posts: 12030
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Vegar!

You'll have an easier time getting help if you post the relevant code on our boards, using [code] tags. People are reluctant to leave the forums to check out code.

Can you post the listener code, and the code that calculates the velocity and acceleration?
 
Rancher
Posts: 893
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not find the section of code you are talking about in your post.

You say you don't get the acceleration to work right, but I don't see that calculation either.

Let me use a shotgun style of attempt to help you--just throw a bunch of stuff out there and hope it this:

Acceleration is constant, a fixed value in magnitude? I'll assume it is.

Velocity = Velocity' + Acceleration * Time .... this is your result Velocity is a function of your initial velocity (Velocity') plus your Acceleration over time (Acceleration * Time) where Time is the time to apply the acceleration. This is all fine when doing simple speed--horizontal velocity.

When you go into 2D then you have to add three different direction vectors:

(Speed @ Angle) = (Speed' @ Angle') + (Acceleration @ Angle'') * Time

At any point in time your velocity is broken into X and Y movement (Speed @ Angle).  The X component is broken into Speed * COS(Angle) and Y is broken into Speed * SIN(Angle)

Please Note that the only things you actually use in the calculation becomes your Acceleration and your ship's instantaneous angle (Angle'').

Which gives:

X = X + Acceleration * COS(Angle'') * Time and Y = Y + SIN(Angle'') * Time

If you need the angle of your direction, then you get it by Angle = ATAN(Y/X) and your Speed at any point in time is SQRT(X*X+Y*Y)

The previous 2 lines are what you need to check your formulas.
 
Vegar Andreas Bergum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weeeeow. Long time since I posted this. Almost forgot about, since alot of other projects have taken a lot of time lately.

I reviewed my formulas, and I am still unsure where the problem is. The relevant code was in the first pastebin, but I'll post it directly as well:

 
Les Morgan
Rancher
Posts: 893
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You formulas in calcAngleMoveX, and calcAngleMoveY are fine; it looks like there is acutally a problem in your key events.
 
Vegar Andreas Bergum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Movement works fine horizontally, as mentioned. That meaning when the ship is facing to the right (90 degrees) and to the left, (270) degrees. At either of those degrees, the speed is set correctly. The further I move toward 0/360 degrees (looking up) or 180 degrees (looking down), the added velocity in that direction is proportionally less. From the current method of getting input from the user, I don't see why 90 and 270 degrees would have the speed added correctly, and the closer  you get to 0/360 or 180 would not.
 
Vegar Andreas Bergum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vegar Andreas Bergum wrote:Movement works fine horizontally, as mentioned. That meaning when the ship is facing to the right (90 degrees) and to the left, (270) degrees. At either of those degrees, the speed is set correctly. The further I move toward 0/360 degrees (looking up) or 180 degrees (looking down), the added velocity in that direction is proportionally less. From the current method of getting input from the user, I don't see why 90 and 270 degrees would have the speed added correctly, and the closer  you get to 0/360 or 180 would not.



Also, upwards, the moveAngle is -9000, to the right its 0, down, its 9000, left its -18000. Now I'm confused how I thought this would work.
 
Les Morgan
Rancher
Posts: 893
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vegar,

First off you know that 0 degrees gives you (1, 0) for x=cos(faceAngle) and y=sin(faceAngle), so your orientation at 0 degrees if facing to the right.

Your formula for calcAngleMoveX and calcAngleMoveY are correct and returns a number between 0 and 1, so for the distanceX you have to have shipSpeed * calcAngleMoveX, and for the distanceY you have shipSpeed * calcAngleMoveY.

If your faceAngle is less than 0 you just need to add 360 to it, and if your faceAngle is greater than 360 you just need to subtract 360 from it.

You do not need to attempt any vectoring of your thrust: calcAngleMoveX and calcAngleMoveY does that for you when you when you figure out the distance moved, so your speed is a simple number that you increment or decrement by a constant when you press a button: shipSpeed = shipSpeed + increment or shipSpeed = shipSpeed - increment.

In those 4 lines of comment, you have the entire movement basis for your game. Don't make it any more difficult than that.

Les

PS: there is good code example in this thread and the comments links in it. GameLink


 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic