It is currently Thu Dec 12, 2013 12:59 pm

 All times are UTC

 Page 1 of 1 [ 20 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: shpshootPosted: Wed Sep 21, 2011 3:38 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
I have an old example of a space ship moving with physics called shpshoot.zip in vb6 - http://www.canning.co.nz/shpshoot.zip

I have tried converting this to VB2010, but havent had any luck with it. Is there an updated VB2010 version of this?

Basically, I am wanting to modify this so that the user can enter a 2d coordinate, and have the space ship move there, but in VB2010.

Can I have a little bit of help with this please?

thanks

Top

 Post subject: Re: shpshootPosted: Wed Sep 21, 2011 6:38 am
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6888
Location: Wilts, Englandshire
Welcome!

Have you looked at the SpaceShip Physics page on the wiki? Does that help?

_________________
20 GOTO 10

Top

 Post subject: Re: shpshootPosted: Wed Sep 21, 2011 6:47 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
I have had a look. The example there is the same one that i referred to in my post... just a different name.

I am having a bit of trouble specifying a targetx and a targety coordinate to move to in this example. Is there some sample code that might be able to help me? Or maybe even a bit of advice on the formulas.

thanks

Top

 Post subject: Re: shpshootPosted: Wed Sep 21, 2011 8:13 am
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6888
Location: Wilts, Englandshire
Sorry, didn't realise that you'd already looked there.

The maths on that page is beyond me, but I've done similar things in my homing missile code using vectors. Basically, a 'velocity' vector is used to represent the current speed and direction of your object. You can adjust it by applying a 'thrust' vector, possibly derived from a 'target' vector which is calculated from the position of your object and the desired destination position.

Does that make sense?

This is one of my homing missile tests (yes I know they've all missed the target), you can see the flight path curving as the thrust vector adjusts the velocity vector.

_________________
20 GOTO 10

Top

 Post subject: Re: shpshootPosted: Wed Sep 21, 2011 9:52 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
It does make sense. Its the math that I am a bit lost with.

Is your homing missile test done in VB? Can you give some examples of the physics that you played around with?

Top

 Post subject: Re: shpshootPosted: Wed Sep 21, 2011 2:45 pm
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6888
Location: Wilts, Englandshire
Sorry, my code was C++. I'll see what I can dig up later.

_________________
20 GOTO 10

Top

 Post subject: Re: shpshootPosted: Thu Sep 22, 2011 8:57 pm
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6888
Location: Wilts, Englandshire
Can't find it now. However it went something like this:

• Each update, add the currentHeading vector to the position to calculate the new position
• If you are looking for a target, subtract the ship position from the target position and normalise the result to get a target vector.
• Add a fraction of the target vector to the currentHeading vector. (Because the vector is normalised, you can simply multiply each value by a float thrust value. e.g. tv.x * 0.025f, tv.y * 0.025f to get a consistent thrust effect.)
• On the next update the currentHeading will have shifted towards the target.

You should probably normalise the currentHeading vector if the length is over 1.0 just to control the speed. Otherwise, as the currentHeading aligns with the target vector, the speed will build and build. However, don't normalise currentHeading lengths under 1.0 or your ship object will always travel at the same speed.

I hope that makes sense, I did start to knock up a little PyGame demo, but my Python-fu is weak and I was spending too much time in the documentation.

_________________
20 GOTO 10

Top

 Post subject: Re: shpshootPosted: Fri Sep 23, 2011 8:29 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
OK, I have done a bit of work on this game. I now have 2 vectors, an acceleration vector and a velocity vector for the ship. When moving to a new target, I will make the ship come to a complete stop, then rotate to the new destination and then apply some thrust.

So what I am after now is the method to make the ship come to a complete stop... Do i rotate the ship to the opposite direction of its momentum and then apply some thrust until it reaches a speed of 0? Or something to that effect? If so, how can i calculate this direction? Is it something to do with the Ship.msngHeading variable?

Here is my physics code:

Code:
Private Sub Physics()

Dim sngXComp As Single  'Resultant X and Y components
Dim sngYComp As Single
Dim i As Integer

'Thrust
If mblnUpKey Then
mblnUpKey = False

Ship.Acceleration.x = Ship.ACCEL * Sin(Ship.msngFacing)
Ship.Acceleration.y = Ship.ACCEL * Cos(Ship.msngFacing)

sngXComp = Ship.Velocity.x + Ship.Acceleration.x
sngYComp = Ship.Velocity.y + Ship.Acceleration.y

Ship.msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)
If Ship.msngSpeed > 5 Then Ship.msngSpeed = 5
If sngYComp > 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp)
If sngYComp < 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp) + PI
End If

Ship.msngX = Ship.msngX + Ship.Velocity.x
Ship.msngY = Ship.msngY - Ship.Velocity.y

If Ship.msngX < 0 Then Ship.msngX = ScreenWidth
If Ship.msngY < 0 Then Ship.msngY = ScreenHeight
If Ship.msngX > ScreenWidth Then Ship.msngX = 0
If Ship.msngY > ScreenHeight Then Ship.msngY = 0

End Sub

Top

 Post subject: Re: shpshootPosted: Fri Sep 23, 2011 11:35 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
OK, I have got the ship stopping. Now I want to turn to the new target, accelerate towards it and when I get close, turn and apply the thrust again to stop dead on the target.

Top

 Post subject: Re: shpshootPosted: Fri Sep 23, 2011 1:52 pm
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6888
Location: Wilts, Englandshire
This is where my maths starts to fail. This isn't something I had to worry about with homing missles, they don't have to worry about slowing down as they approach the target.

If you're stopping the ship, I take it you've inverted the velocity vector to create a thrust vector in the opposite direction?

Working out the magnitude of thrust to apply to slow the ship down would have to be calculated from the distance to the target. Obviously, if the ship is at the target position, the thrust vector would have to be equal and opposite to the velocity in order to cancel it out. However, at a distance the thrust required would have to be smaller in order to slow the ship down gradually.
Perhaps the length of the target vector before normalisation could be used to dictate the level of thrust required?

After looking at the code, the msngHeading variable seems to be an angle, so we're using polar coordinates. You can subtract 180 degrees (or 3.14 radians) to find the opposite thrust direction.

_________________
20 GOTO 10

Top

 Post subject: Re: shpshootPosted: Fri Sep 23, 2011 11:00 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
Calculate stopping distance for the speed the ship is currently travelling at.

Calculate the distance to the target.

Allow a little extra on the stopping distance because you don't want to cut things too fine.

If one is bigger than two, then decelerate, otherwise accelerate.

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Re: shpshootPosted: Sat Sep 24, 2011 3:20 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
To calculate when to turn and apply thrust in my opposite heading to stop on the target, I presume I will have to use a formula that takes into account my current heading and my ships acceleration.

What would that formula be?

Top

 Post subject: Re: shpshootPosted: Sat Sep 24, 2011 10:59 am
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
If I'm reading this correct, the player will push the up key, and the ship will accelerate along the bearing (Ship.msngHeading).

Even though the ship may be facing a different direction (Ship.msngFacing) when the key is pressed, motion remains constrained along the bearing Ship.msngHeading, and the ship cannot move off that line.

Is that correct?

Next, do you want the ship to automatically slow itself down to a stop when it approaches it's destination? What if the Up key is pressed during this time? What if the player attempts to turn the ship during this time?

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Re: shpshootPosted: Sat Sep 24, 2011 11:39 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
I have a ship that has a Facing angle, a Heading, a velocity and an acceleration.
When the 'up' key is pressed, the ship thrusts in its facing direction. When the ship is moving, you can rotate it around without affecting its momentum. This only changes when you press the thrust key.

While moving to a destination, if the thrust key is pressed, it will cancel its movement. You can still rotate the ship while moving to a target.

Quote:
Next, do you want the ship to automatically slow itself down to a stop when it approaches it's destination?

Yes, that is what I am up to.

Top

 Post subject: Re: shpshootPosted: Sat Sep 24, 2011 12:11 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
Well for a start, I think your physics code can be simplified to this:

Code:
Private Sub Physics()
'Thrust
If mblnUpKey Then
mblnUpKey = False
Ship.Velocity.x = Ship.Velocity.x + Ship.ACCEL * Sin(Ship.msngFacing)
Ship.Velocity.y = Ship.Velocity.y + Ship.ACCEL * Cos(Ship.msngFacing)
Ship.msngSpeed = Sqr(Ship.Velocity.x  * Ship.Velocity.x + Ship.Velocity.y * Ship.Velocity.y)
If Ship.msngSpeed > 5 Then  SF = 5/Ship.msngSpeed :  Ship.Velocity.x = Ship.Velocity.x * SF : Ship.Velocity.y = Ship.Velocity.y * SF
End If

Ship.msngX = (Ship.msngX + Ship.Velocity.x + ScreenWidth) MOD ScreenWidth
Ship.msngY = (Ship.msngY + Ship.Velocity.y + ScreenHeight) MOD ScreenHeight
End Sub

Canning wrote:
Quote:
Next, do you want the ship to automatically slow itself down to a stop when it approaches it's destination?

Yes, that is what I am up to.

The ship isn't necessarily going to be heading towards the destination point when the "Automatic Slow Down" kicks in. The ship may sail past the destination on it's port or starboard side. So in addition to slowing it down to a stop, you'll also have to line it up on course with the destination.

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Re: shpshootPosted: Tue Oct 04, 2011 9:53 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
OK, before I write the code to stop, I am after some help to rotate my ship to a destination angle.

When I want to start turning, this is the code I run:

Code:
Ship.DestAngle = System.Math.Atan2(Ship.Target.y - Ship.msngY, Ship.Target.x - Ship.msngX) + PI / 2
Ship.turning = True

And in the physics code I have the following:

Code:
If Ship.turning = True Then

c = System.Math.Sin(Ship.msngFacing)
d = System.Math.Cos(Ship.msngFacing)

det = a * d - b * c

If det > 0 Then
Ship.msngFacing = Ship.msngFacing + Ship.ROTATION_RATE * PI / 180
If Ship.msngFacing > Ship.DestAngle Then
Ship.msngFacing = Ship.DestAngle
Ship.turning = False
End If
Else
Ship.msngFacing = Ship.msngFacing - Ship.ROTATION_RATE * PI / 180
If Ship.msngFacing < Ship.DestAngle Then
Ship.msngFacing = Ship.DestAngle
Ship.turning = False
End If
End If
End If

Sometimes the ship turns nicely, other times it 'jumps' to the destination angle. I think this has something to do with the sign changing. Can I have some advice please on how to fix this?

thanks

Canning

Top

 Post subject: Re: shpshootPosted: Tue Oct 04, 2011 11:06 am
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
The problem is this:

Ship.msngFacing > Ship.DestAngle

You shouldn't use inequalities with bearings because their algebra uses modular arithmetic. It's like asking if 3 o'clock is before 10 o'clock... well it depends which way round the clock we go.

The correct way to do this is to measure the angle to turn through both clockwise and anticlockwise, and +/- 2π so each is bound to the range (0, 2π) .

Choose whichever has smaller magnitude and rotate in that direction. If the above is calculated correctly, then crossing the 0 or 2π mark should cause no jumping.

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Re: shpshootPosted: Tue Oct 04, 2011 11:22 am
 Level 1 Cleric

Joined: Wed Sep 21, 2011 3:14 am
Posts: 10
Quote:
The correct way to do this is to measure the angle to turn through both clockwise and anticlockwise

Is there a simple formula to do this? If so, can you please enlighten me?

Canning

Top

 Post subject: Re: shpshootPosted: Tue Oct 04, 2011 11:31 am
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
It's very simple...

Anticlockwise angle = target bearing - current bearing
clockwise angle = current bearing - target bearing

As I said above, +/- 2π so each is bound to the range (0, 2π).

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Re: shpshootPosted: Fri Oct 14, 2011 10:16 pm
 Grand Optimizer

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 356

I see you have a missile tracking thing going on, but none of your missiles hit home

I actually had some success a long time ago with an old (partial)game I made. It was a spaceship game. I could spawn enemies, who would acurately follow and shoot at you. I think the same follow logic could apply to your missiles.

I can only provide you with logic, not code.. since your in C++ and im in C#. Anyway, it went something like this:

1) Determine the vector between enemy ship and player ship
2) Determine the quickest way to face the target (turning left, or right? which is the smaller angle?)
3) Once I'm facing the target, throttle up and chase him down!
4) Oh crap, im getting too close.. back off a bit. (naturally a missile wouldn't do this.. but you get the idea)

I hope this helps.

In all likelyhood you've already thought 50 steps beyond this, but if not, hopefully this logic is helpfull. Worked for me.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 20 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News