Movement around a sphere.

Blitz3D Forums/Blitz3D Beginners Area/Movement around a sphere.

Aussie(Posted 2012) [#1]
Hey there all, been a while since I have posted here.
I have a sphere as a planet & have a player that orbits the planet.
I have used a pivot & parented the player to the pivot that is used to move the player around the planet.
I am trying to setup a system for an enemy to chase the player & am having problems regarding the best way to go about the enemy chasing system & aligning to the player correctly.

If someone could offer some advice or an example it would be greatly appreciated.

Here is what I have. Use the arrow keys to move the player.


Last edited 2012

Last edited 2012


RemiD(Posted 2012) [#2]

I am trying to setup a system for an enemy to chase the player & am having problems regarding the best way to go about the enemy chasing system & aligning to the player correctly.


If you want the enemy to move around the planet, as the player does, and chase the player at this height, here what i would do :

1)Turn the enemy towards the player
2)Move the enemy forward (+Z)
3)Make your planet pickable
4)LinePick from above the enemy to below the enemy (from above because the enemy ship may be inside the planet at first)
5)Use the picked point coordinates to position the enemy on the planet at the desired height (if you want to add the ability to go up or down.)
6)Align the enemy to the ground of the planet with AlignToVector

I suggest you also use the same method for the movement of the player ship, so that you will be able to adjust the height you want.

This is probably not the best technique, your planet being a sphere it may be easier to do it with maths. But i can't help with this.

Last edited 2012


Aussie(Posted 2012) [#3]
Thanks RemiD,

I probably should have said the reason i have been using the pivots & positioning the enemy & player offset from the pivots is because i'm not to keen on going down the path of using linepicks.

This is just a test to get a system of the enemy being able to chase the player correctly & eventually i will be having a fair few enemies/objects around the planet & if i use a heap of linepicks it will probably cause to much of a slowdown.

I had thought maybe there was a way to do it with maths but i am not that great at using maths for this kind of a system. I am tipping it would use Sin, Cos, Tan but i could never really get my head around it.

Thanks for the help anyway.

Actually,
dose anyone know how much of a slowdown linepicks cause? I remember reading that to many will cause a slowdown & eventually there could be up to 100 or so objects around the planet.


RemiD(Posted 2012) [#4]
Linepick and Collisions are calculated very fast if used properly.

The best way i have found to make it fast :
At the begining of each loop, delete all colliders and disable all pickmode.

Before you do a check, set only the collider or the pickmode of the colliders you need.

If you use meshes for colliders and pickable entities, make sure these meshes have the fewest tris possible, just to approximate the shape of the collider / pickable entity.

This way you check if a collision or a pick happens on only a few colliders / pickable entities and they are low tris so it is very fast !

You can use linepick for many things and it won't slow down your game if you follow the advices mentioned above.



Of course for a planet being made of a perfect sphere, i think will be better with maths...
Edit : corrections
Maybe you can add vectors :
The vector of movement of the ship (+Z locally)
+
The vector of the difference of the distance from the ship to the center of the sphere minus the radius of the sphere, this will give you a length that is the distance from the ship to the ground of the planet after the movement of the ship.

And with this you will be able to position the ship.

Try it it should work.

Last edited 2012


Zethrax(Posted 2012) [#5]
Not sure what you're after exactly, but here's some smart missile code I wrote at one point that sound like what you're requesting.

http://www.blitzbasic.com/codearcs/codearcs.php?code=2258


Aussie(Posted 2012) [#6]
Thanks guys

@ RemiD
I never thought of doing that with linepicks & collisions i will have to try it out.

With the vector
The vector of movement of the ship (+Z locally)

Are you talking velocity or Z coordinates?
Like i was saying, i'm not that great with maths when it comes to dealing with circles or spheres. :)

@ Zethrax
What you have there is the kind of thing i am trying to achieve, the difference is that the missile would be orbiting a planet & chasing other objects in the same orbit. Hope that explains what i am trying to achieve.

I will have a play around & see what i can come up with.

Last edited 2012


Aussie(Posted 2012) [#7]
Ok i am struggling with this.
I decided to try & use the linepick method instead of using pivots & i can't get the player to align with the sphere using the aligntovector. Here is what i have, if some one could let me know what i am doing wrong it would be greatly appreciated.



I have also been trying to figure out how to use the vectors for doing this kind of a control system with no luck. I did do some looking around for a spherical coordinate system & information on how they work & it is way over my head.
If i can get the linepick & aligntovector to work i think i should be able to get the enemy to chase the player properly.


Stevie G(Posted 2012) [#8]
The PickedNX, NY and NZ commands are functions which return values so should always be used with open/close brackets,i.e:

[bbcode]
AlignToVector player,PickedNX(),PickedNY(),PickedNZ(),2,1
[/bbcode]

This seems to be your only issue.

Stevie


Aussie(Posted 2012) [#9]
Thanks heaps Stevie G

It has been driving me nuts not being able to figure out what was going wrong. I looked at other examples & didn't even notice the ().
Works great now.


RemiD(Posted 2012) [#10]
Aussie>>This works well and Linepick seems to be fast enough for now.
Here are some suggestions :
->First turn your ship and move it, and then do the linepick calculation in order to correct the position and orientation.
->You should use a collider with less tris for the sphere, or if it is faster, use a combination of EntityPickMode sphere with EntityRadius()

Edit: i have added the EntityPickMode(1) with EntityRadius(), it is really fast !

Here is your code with some suggestions :
Also i have added the routine with vectors in words, maybe somebody can help you find the math formula, i can't help sorry.


Last edited 2012


Aussie(Posted 2012) [#11]
Thanks for the help RemiD.
Very much appreciated. I would be interested in trying to use vectors but way over my head with the formulas.


Kryzon(Posted 2012) [#12]
The best way i have found to make it fast :
At the begining of each loop, delete all colliders and disable all pickmode.

Before you do a check, set only the collider or the pickmode of the colliders you need.

Cool idea; I hadn't thought of that before.
Changing pickmodes is merely setting values in memory internally, so it should be much faster than having every redundant but potentially pickable entity be tested.

Anyway, I played with the code a bit, changed [a lot of] things... and filled the vector math routine. Took a while.
Seems to work ok; it's definitely much faster. Just copy and paste:



Last edited 2012


Aussie(Posted 2012) [#13]
Cool.
Thanks heaps Kryzon.
Love the effect of the light on the surface of the planet.


Kryzon(Posted 2012) [#14]
• Added a single enemy following the player.

When you want to add more features to your program you notice how important modularity and structure are. Generic setup functions, generic types\classes, sensible frameworking.
It made it all that much easier knowing where to add the new code, or change some parts (like the behind-the-planet 2D indicators).




Aussie(Posted 2012) [#15]
Thanks again Kryzon.

I'm not at my computer at the moment so i am unable to see what you have done & probably won’t get much of a chance until the weekend.

When you want to add more features to your program you notice how important modularity and structure are. Generic setup functions, generic types\classes, sensible frameworking.


I agree completely. Whenever i get the chance to have a go at coding & play with ideas it always starts off a bit mix & match until i get things starting to work. Once i get things tested & working i then go through & start organising things into functions & types. One thing i do need to do is start commenting my code more as i go, i have some bits & pieces of code from when i first started coding & if i haven’t been able to code for a while it takes a fair bit time to catch up on what i was doing.

Thanks again.