Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: 2d asteroids  (Read 422 times)
Canning
Community Member
*
Posts: 592


« on: September 18, 2011, 11:24:08 PM »

I am after a little bit of help with modifying the 2d asteroids example.

I am wanting to specify an x and y coordinate and have the ship move to that coordinate bearing in mind that it may already be moving.

The following are the important variables that I have:

PlayerFacingAngle, PlayerPositionX, PlayerPositionY, PlayerVelocityX and PlayerVelocityY

thanks 
« Last Edit: September 19, 2011, 01:11:12 AM by Canning » Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
asia
Customers
Community Member
*****
Posts: 183


« Reply #1 on: September 19, 2011, 01:55:37 AM »

You also should have a targetx,targety.
In that case you should correct the angle to face the target.
The desiderated angle is  targetangle=tan(targety-PlayerPositiony,targetx-PlayerPositionX).
You can correct the PlayerFacingAngle directly (PlayerFacingAngle= targetangle) or by a step (for example deltaAngle).
Than the speed module is: Speed=Sqr(PlayerVelocityX ^2+ PlayerVelocityXy^2)
Actually you should keep the value of speed as scalar since the beginning.
Finally correct the position with speed.
Code:
PlayerPositionx= PlayerPositionx+speed*cos(PlayerFacingAngle)
PlayerPositiony= PlayerPositiony+speed*sin(PlayerFacingAngle)
A more interesting solution will take in to account forces (gravity). Than you should include masses and accelerations (maybe to both player and target)
Hope this will help.
Logged

Fabio Musmeci
ENEA
CR Casaccia
Via Anguillarese 301
00060 Rome
Italy
musmeci@enea.it
+39 3333934898
Learning to live better on a smaller footprint..
Canning
Community Member
*
Posts: 592


« Reply #2 on: September 19, 2011, 07:38:00 PM »

Thanks asia, I will try that when I have some time.

Quote
A more interesting solution will take in to account forces (gravity). Than you should include masses and accelerations (maybe to both player and target)

Interesting that you should say that... rather than the way the 2d asteroids example works,I am quite keen to change the physics of the ship.
I like the idea Space Sonata does it, where you can rotate the ship (without changing its momentum) and when you press the thrust key the ship moves, taking into account its current movement/momentum.

I used to have an example of this in a directx game called shpshoot.zip, but have lost it. Do you asia (or anyone else) have this example or some code similar that I could have a look at?

thanks
« Last Edit: September 19, 2011, 07:43:46 PM by Canning » Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
Dimple
Community Member
*
Posts: 580


« Reply #3 on: September 20, 2011, 05:54:04 PM »

 Smiley

Is this where you got the file before?

http://www.xtremevbtalk.com/showthread.php?t=37387

Logged

Using VB.NET, TV3D 6.5, VISTA
~~~~~~~~~~~~~~~~~~~~~~~

"Know how to ask. There is nothing more difficult for some people, nor for others, easier."

- Baltasar Gracian
Canning
Community Member
*
Posts: 592


« Reply #4 on: September 20, 2011, 09:56:52 PM »

Thanks Dimple, that is exactly what I was after. Smiley

OK, about that example... I am wanting to specify a TargetX and a TargetY coordinate, and have the ship move to there.

The main variables are as follows:
Code:
Const PI = 3.14159          'Mmmm.. Pi
Const ACCEL = 0.1           'Rate of increase of speed
Const ROTATION_RATE = 15    'Rotation speed
Const SHIP_RADIUS = 10      'Distance from center of triangle to any vertex
Const MS_DELAY = 25         'Milliseconds per frame (25 = 40 frames per second)

Dim msngFacing As Single    'Angle the ship is facing (ok, ok, it's a triangle, not a ship! Shut it!)
Dim msngHeading As Single   'Current direction in which ship is moving
Dim msngSpeed As Single     'Current speed with which ship is moving
Dim msngX As Single         'Current X coordinate of ship within form
Dim msngY As Single         'Current Y coordinate of ship within form

Is this what i need to do:

1. Convert the current movement into a vector.
2. Calculate the thrust needed to get to the destination vector.
3. Apply the thrust

Can I have a little bit of help with this if possible? The maths eludes me.

If the ship isnt moving, it is quite easy... just rotate to the desired target and apply some thrust. The problem is that it is more difficult if the ship is already moving.

thanks
« Last Edit: September 22, 2011, 01:37:56 AM by Canning » Logged

I am using 2010 VB.NET and TV 6.5 with Windows 7
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks