gravity
parent
b86f332fc3
commit
8a0f249cc1
@ -0,0 +1,25 @@
|
||||
#ifndef GRAVITY_H
|
||||
#define GRAVITY_H
|
||||
|
||||
//gravitator gravitates things
|
||||
|
||||
#import <raylib.h>
|
||||
#import <yeslib.h>
|
||||
#import "Activator.h"
|
||||
#import "PhysObject.h"
|
||||
|
||||
//F = M*A
|
||||
//objects should have mass and a velocity vector
|
||||
|
||||
//maybe change this to subclass of YSArray
|
||||
|
||||
@interface Gravitator : YSArray {
|
||||
@public
|
||||
Vector3 acceleration;
|
||||
}
|
||||
|
||||
-(void) applyGravity;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,30 @@
|
||||
#import "Gravitator.h"
|
||||
|
||||
static inline Vector3 multFloatVector3(float f, Vector3 v) {
|
||||
return (Vector3){f * v.x, f * v.y, f * v.z};
|
||||
}
|
||||
|
||||
@implementation Gravitator
|
||||
|
||||
//apply gravity to all objects in array
|
||||
-(void) applyGravity {
|
||||
int i = 0;
|
||||
|
||||
//receivers must have velocity vector
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
|
||||
//cursed
|
||||
|
||||
Vector3 force = multFloatVector3( [(PhysObject *)array[i] mass], acceleration);
|
||||
printf("%f\n", [(PhysObject *)array[i] mass]);
|
||||
|
||||
[(PhysObject *)array[i] setVelocity: Vector3Add(force, [(PhysObject *)array[i] velocity])];
|
||||
[(PhysObject *)array[i] setPos: Vector3Add( [(PhysObject *)array[i] velocity], [(PhysObject *)array[i] pos] )];
|
||||
//printf("%f %f %f\n", force.x, force.y, force.z );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
@ -0,0 +1,20 @@
|
||||
#ifndef PHYSOBJECT_H
|
||||
#define PHYSOBJECT_H
|
||||
|
||||
#import "Model.h"
|
||||
|
||||
|
||||
@interface PhysObject : YSModel {
|
||||
@public
|
||||
float mass;
|
||||
Vector3 velocity;
|
||||
}
|
||||
|
||||
-(id) init;
|
||||
-(Vector3) velocity;
|
||||
-(void) setVelocity: (Vector3) v;
|
||||
-(float) mass;
|
||||
-(void) setMass: (float) m;
|
||||
|
||||
@end
|
||||
#endif
|
||||
@ -0,0 +1,31 @@
|
||||
#import "PhysObject.h"
|
||||
|
||||
@implementation PhysObject
|
||||
|
||||
-(id) init {
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
mass = 1.0f;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(Vector3) velocity {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
-(void) setVelocity: (Vector3) v {
|
||||
velocity = v;
|
||||
}
|
||||
|
||||
-(float) mass {
|
||||
return mass;
|
||||
}
|
||||
|
||||
-(void) setMass: (float) m {
|
||||
mass = m;
|
||||
}
|
||||
|
||||
@end
|
||||
@ -0,0 +1,20 @@
|
||||
#ifndef RENDERER_H
|
||||
#define RENDERER_H
|
||||
|
||||
#import <yeslib.h>
|
||||
|
||||
/*
|
||||
the main loop may have two or more renderers,
|
||||
one for the 3D section, and another for the
|
||||
2D section.
|
||||
*/
|
||||
|
||||
@interface Renderer : YSArray {
|
||||
|
||||
}
|
||||
|
||||
-(void) render;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,13 @@
|
||||
#import "Renderer.h"
|
||||
|
||||
@implementation Renderer
|
||||
|
||||
-(void) render {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
[array[i] draw];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Loading…
Reference in New Issue