Jump to content

Planet Gravity


Sammy6200
 Share

Recommended Posts

How would it be possible to make gravity attract towords the center of a giant sphere to simulate a planet with LE?

And also, would it be possible to create some sort of event horizon around the sphere that makes the gravity attract when close enough, so it would be possible to create multiple "planets" who all have gravity?

c++, 3Ds Max, LE.

Link to comment
Share on other sites

It's quite easy, you just need to loop through all physics bodies and add a force to them in direction of the nearest/biggest gravity center.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

I did this:

 

#include "engine.h"

#include <iostream>

 

int main( int argn, char* argv[] )

{

Initialize() ;

RegisterAbstractPath("C:/Users/Sammy/Desktop/Leadwerks SDK");

SetAppTitle( "New" ) ;

Graphics( 800, 500 ) ;

AFilter() ;

TFilter() ;

 

TWorld world;

TBuffer gbuffer;

TCamera camera;

TMesh mesh;

TBody body;

TMesh mesh2;

TBody body2;

TLight light;

TMaterial material;

 

world = CreateWorld() ;

if (!world) {

MessageBoxA(0,"Error","Failed to create world.",0);

return Terminate();

}

 

gbuffer=CreateBuffer(GraphicsWidth(),GraphicsHeight(),BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);

 

camera=CreateCamera();

PositionEntity(camera,Vec3(0,0,-2));

 

material=LoadMaterial("abstract::cobblestones.mat");

 

mesh=CreateSphere(16);

body=CreateBodySphere();

EntityParent(mesh,body);

PaintEntity(mesh,material);

SetBodyMass(body,1);

EntityType(body,1);

PositionEntity(body,Vec3(0.7,0.5,0.3));

 

mesh2=CreateSphere(16);

body2=CreateBodySphere();

EntityParent(mesh2,body2);

PaintEntity(mesh2,material);

EntityType(body2,1);

PositionEntity(body2,Vec3(-0.7,0,0));

 

DebugPhysics(1);

 

 

light=CreateDirectionalLight();

RotateEntity(light,Vec3(45,45,45));

 

Collisions(1,1,1);

SetWorldGravity(Vec3(0,0,0));

 

TVec3 vector = Vec3(0,0,0);

std::string vinfo;

 

// Game loop

while( !KeyHit() && !AppTerminate() )

{

vector = TFormVector(Vec3(-2,0,0),body,body2);

 

// Update timing and world

UpdateAppTime();

UpdateWorld(AppSpeed()) ;

 

// Render

SetBuffer(gbuffer);

RenderWorld();

SetBuffer(BackBuffer());

RenderLights(gbuffer);

 

AddBodyForce(body,vector,1);

 

// Send to screen

Flip(0) ;

}

 

// Done

return Terminate() ;

}

 

 

It kinda seems to create an gravitation towords the one sphere.

c++, 3Ds Max, LE.

Link to comment
Share on other sites

The only use I know for TFormVector is to rotate a vector so that it has the same rotation as some entity.

So to use it for gravity force, you point a pivot from an object to the gravity center, and then use TFormVector to get that rotation, so you can use it as force vector:

PointEntity( pivot, gravitycenter );    // pivot is parented to the object
TFormVector( &force, pivot, NULL);
AddBodyForce( object, force );

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Remember to divide the Force by the square of the distance from the centre of gravity if you want to make it realistic.

 

Fortunately the distance squared is simply the the sum of the squares of the difference in x, y and z. i.e (x1 - x2)2 + (y1 - y2)2 + (z1 - z2)2

AMD Phenom 9850 (X4 2.5GHz) | 1066MHz CL5 GEIL Black Dragon (800MHz) | Powercolor ATI 4870 1GB | DFI 790FXB-M2RSH | 24" 1920x1200 | Windows Vista 64-bit Ultimate

Link to comment
Share on other sites

Yes, the EntityPosition(). You can also do this:

 


TVec3 planetpos = EntityPosition(planet);
TVec3 objpos = EntityPostion(obj);

//x
while(objpos.x<planetpos.x){
  MoveEntity(obj,Vec3(-1,0,0);
}
while(objpos.x>planetpos.x){
  MoveEntity(obj,Vec3(1,0,0);
}
//y
while(objpos.y<planetpos.y){
  MoveEntity(obj,Vec3(0,-1,0);
}
while(objpos.y>planetpos.y){
  MoveEntity(obj,Vec3(0,1,0);
}
//z
while(objpos.z<planetpos.z){
  MoveEntity(obj,Vec3(0,0,-1);
}
while(objpos.z>planetpos.z){
  MoveEntity(obj,Vec3(0,0,1);
}

 

not a good solution, but it can work. :)

Link to comment
Share on other sites

My solution was this:

#include "engine.h"

#include <iostream>

#include <cmath>

 

int main( int argn, char* argv[] )

{

Initialize() ;

RegisterAbstractPath("C:/Users/Sammy/Desktop/Leadwerks SDK");

SetAppTitle( "New" ) ;

Graphics( 800, 500 ) ;

AFilter() ;

TFilter() ;

 

TWorld world;

TBuffer gbuffer;

TCamera camera;

TMesh mesh;

TBody body;

TMesh mesh2;

TBody body2;

TLight light;

TMaterial material;

 

world = CreateWorld() ;

if (!world) {

MessageBoxA(0,"Error","Failed to create world.",0);

return Terminate();

}

 

gbuffer=CreateBuffer(GraphicsWidth(),GraphicsHeight(),BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);

 

camera=CreateCamera();

PositionEntity(camera,Vec3(0,0,-2));

 

mesh=CreateSphere(16);

body=CreateBodySphere();

EntityParent(mesh,body);

SetBodyMass(body,1);

EntityType(body,1);

PositionEntity(body,Vec3(-0.8,0,0));

 

mesh2=CreateSphere(16);

body2=CreateBodySphere();

EntityParent(mesh2,body2);

SetBodyMass(body2,1);

EntityType(body2,1);

PositionEntity(body2,Vec3(0.2,0,0));

 

DebugPhysics(1);

 

light=CreateDirectionalLight();

RotateEntity(light,Vec3(45,45,45));

 

Collisions(1,1,1);

SetWorldGravity(Vec3(0,0,0));

 

TVec3 b1Vec = Vec3(0,0,0);

TVec3 b2Vec = Vec3(0,0,0);

TVec3 fVec = Vec3(0,0,0);

 

// Game loop

while( !KeyHit() && !AppTerminate() )

{

 

b1Vec = EntityPosition(body);

b2Vec = EntityPosition(body2);

 

fVec = Vec3((b1Vec.X - b2Vec.X)*(-1), (b1Vec.Y - b2Vec.Y)*(-1), (b1Vec.Z - b2Vec.Z)*(-1));

 

AddBodyForce(body,fVec);

 

 

if(KeyDown(KEY_UP))

{

AddBodyForce(body,Vec3(0,10,0));

}

 

if(KeyDown(KEY_DOWN))

{

AddBodyForce(body,Vec3(0,-10,0));

}

 

// Update timing and world

UpdateAppTime();

UpdateWorld(AppSpeed()) ;

 

// Render

SetBuffer(gbuffer);

RenderWorld();

SetBuffer(BackBuffer());

RenderLights(gbuffer);

 

// Send to screen

Flip(0) ;

}

 

// Done

return Terminate() ;

}

 

 

And it works perfectly!

c++, 3Ds Max, LE.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...