Finished changes on animation system

* Added collision callback
* Set sprite density to 1 by default
* Added default frame value of 0 on frame resize
* Changed defaults for sprite fixture
* Made changes to some of the animation functions parameters
This commit is contained in:
n00b
2024-10-18 00:10:12 -04:00
parent 9b517acae1
commit 8f372bdf19
7 changed files with 142 additions and 26 deletions

View File

@@ -1577,8 +1577,6 @@ bool rc_free_type(rc_usrId* parent)
rc_usrId* p_obj; rc_usrId* p_obj;
uint64_t field_size = 0;
for(uint64_t i = 0; i < dim_size; i++) for(uint64_t i = 0; i < dim_size; i++)
{ {
p_obj = &parent->uid_value[i]; p_obj = &parent->uid_value[i];
@@ -1968,7 +1966,7 @@ void for_117(uint64_t nid, int n1, int n2, int n3)
//These 3 lines reads the line value passed by the compiler //These 3 lines reads the line value passed by the compiler
//This line value is the address of the end of the loop //This line value is the address of the end of the loop
unsigned rcbasic_cmd = segment[current_segment][current_address]; //unsigned rcbasic_cmd = segment[current_segment][current_address];
current_address++; current_address++;
uint64_t for_end_addr = readInt(); uint64_t for_end_addr = readInt();
@@ -2273,8 +2271,6 @@ void rc_type_array_copy(rc_usrId* src, rc_usrId* dst)
rc_usrId* p_obj; rc_usrId* p_obj;
uint64_t field_size = 0;
rc_usrId* s_obj; rc_usrId* s_obj;
//cout << "starting field: " << endl; //cout << "starting field: " << endl;
@@ -3137,7 +3133,7 @@ void obj_usr_init1_185(int u1, int n1)
void obj_usr_init2_186(int u1, int n1, int n2) void obj_usr_init2_186(int u1, int n1, int n2)
{ {
uint64_t d[3]; //uint64_t d[3];
//cout << "obj_usr_init2: u" << u1 << " --dim=[" << d[0] << ", " << d[1] << ", " << d[2] << "]" << endl; //cout << "obj_usr_init2: u" << u1 << " --dim=[" << d[0] << ", " << d[1] << ", " << d[2] << "]" << endl;
usr_object.index = (uint64_t)vm_n[n1].value * vm_u[u1].dim[1] + (uint64_t)vm_n[n2].value; usr_object.index = (uint64_t)vm_n[n1].value * vm_u[u1].dim[1] + (uint64_t)vm_n[n2].value;
usr_object.obj_ref = &vm_u[u1].var_ref->uid_value[usr_object.index]; usr_object.obj_ref = &vm_u[u1].var_ref->uid_value[usr_object.index];
@@ -3161,7 +3157,6 @@ bool rc_preset_type(rc_usrId* parent)
rc_usrId* p_obj; rc_usrId* p_obj;
uint64_t field_size = 0;
for(uint64_t i = 0; i < dim_size; i++) for(uint64_t i = 0; i < dim_size; i++)
{ {

View File

@@ -914,12 +914,14 @@ int rc_canvasOpen(int w, int h, int vx, int vy, int vw, int vh, int mode, int ca
canvas.physics2D.world = NULL; canvas.physics2D.world = NULL;
if(canvas_type == RC_CANVAS_TYPE_SPRITE) if(canvas_type == RC_CANVAS_TYPE_SPRITE)
{ {
b2Vec2 gravity(0, -9.8); b2Vec2 gravity(0, 0);
canvas.physics2D.world = new b2World(gravity); canvas.physics2D.world = new b2World(gravity);
canvas.physics2D.timeStep = 1/20.0; //the length of time passed to simulate (seconds) canvas.physics2D.timeStep = 1/60.0; //the length of time passed to simulate (seconds)
canvas.physics2D.velocityIterations = 8; //how strongly to correct velocity canvas.physics2D.velocityIterations = 8; //how strongly to correct velocity
canvas.physics2D.positionIterations = 3; //how strongly to correct position canvas.physics2D.positionIterations = 3; //how strongly to correct position
canvas.physics2D.enabled = true; canvas.physics2D.enabled = true;
canvas.physics2D.contact_listener = new rc_contactListener_obj();
canvas.physics2D.world->SetContactListener(canvas.physics2D.contact_listener);
} }
switch(mode) switch(mode)
@@ -1365,7 +1367,7 @@ Uint32 rc_getPixel(int x, int y)
irr::video::SColor * texel = (SColor *)(texels + ((y * pitch) + (x * sizeof(SColor)))); irr::video::SColor * texel = (SColor *)(texels + ((y * pitch) + (x * sizeof(SColor))));
irr::video::SColor c = texel[0]; //irr::video::SColor c = texel[0];
texture->unlock(); texture->unlock();

View File

@@ -2861,6 +2861,8 @@ double rc_computeActorImpulseDenominator(int actor, double pos_x, double pos_y,
{ {
return rc_actor[actor].physics.rigid_body->computeImpulseDenominator(irr::core::vector3df(pos_x, pos_y, pos_z), irr::core::vector3df(normal_x, normal_y, normal_z)); return rc_actor[actor].physics.rigid_body->computeImpulseDenominator(irr::core::vector3df(pos_x, pos_y, pos_z), irr::core::vector3df(normal_x, normal_y, normal_z));
} }
return 0;
} }
double rc_computeActorAngularImpulseDenominator(int actor, double x, double y, double z) double rc_computeActorAngularImpulseDenominator(int actor, double x, double y, double z)
@@ -2872,6 +2874,8 @@ double rc_computeActorAngularImpulseDenominator(int actor, double x, double y, d
{ {
return rc_actor[actor].physics.rigid_body->computeAngularImpulseDenominator(irr::core::vector3df(x, y, z)); return rc_actor[actor].physics.rigid_body->computeAngularImpulseDenominator(irr::core::vector3df(x, y, z));
} }
return 0;
} }
void rc_setActorAngularFactor(int actor, double x, double y, double z) void rc_setActorAngularFactor(int actor, double x, double y, double z)
@@ -3016,6 +3020,8 @@ int rc_createPointConstraintEx(int actorA, int actorB, double pxA, double pyA, d
rc_physics3D.constraints[constraint_id] = p2p; rc_physics3D.constraints[constraint_id] = p2p;
return constraint_id; return constraint_id;
} }
return -1;
} }
void rc_setPointPivotA(int constraint_id, double x, double y, double z) void rc_setPointPivotA(int constraint_id, double x, double y, double z)
@@ -3062,6 +3068,8 @@ int rc_createHingeConstraint(int actorA, int frameInA_matrix, bool useReferenceF
rc_physics3D.constraints[constraint_id] = hinge; rc_physics3D.constraints[constraint_id] = hinge;
return constraint_id; return constraint_id;
} }
return -1;
} }
@@ -3088,6 +3096,8 @@ int rc_createHingeConstraintEx(int actorA, int actorB, int frameInA_matrix, int
rc_physics3D.constraints[constraint_id] = hinge; rc_physics3D.constraints[constraint_id] = hinge;
return constraint_id; return constraint_id;
} }
return -1;
} }
int rc_createSlideConstraint(int actorA, int frameInB_matrix, bool useLinearReferenceFrameA) int rc_createSlideConstraint(int actorA, int frameInB_matrix, bool useLinearReferenceFrameA)
@@ -3110,6 +3120,8 @@ int rc_createSlideConstraint(int actorA, int frameInB_matrix, bool useLinearRefe
rc_physics3D.constraints[constraint_id] = slide; rc_physics3D.constraints[constraint_id] = slide;
return constraint_id; return constraint_id;
} }
return -1;
} }
int rc_createSlideConstraintEx(int actorA, int actorB, int frameInA_matrix, int frameInB_matrix, bool useLinearReferenceFrameA) int rc_createSlideConstraintEx(int actorA, int actorB, int frameInA_matrix, int frameInB_matrix, bool useLinearReferenceFrameA)
@@ -3135,6 +3147,8 @@ int rc_createSlideConstraintEx(int actorA, int actorB, int frameInA_matrix, int
rc_physics3D.constraints[constraint_id] = slide; rc_physics3D.constraints[constraint_id] = slide;
return constraint_id; return constraint_id;
} }
return -1;
} }
@@ -3159,6 +3173,8 @@ int rc_createConeConstraint(int actorA, int rbAFrame_matrix)
rc_physics3D.constraints[constraint_id] = cone; rc_physics3D.constraints[constraint_id] = cone;
return constraint_id; return constraint_id;
} }
return -1;
} }
int rc_createConeConstraintEx(int actorA, int actorB, int rbAFrame_matrix, int rbBFrame_matrix) int rc_createConeConstraintEx(int actorA, int actorB, int rbAFrame_matrix, int rbBFrame_matrix)
@@ -3184,6 +3200,8 @@ int rc_createConeConstraintEx(int actorA, int actorB, int rbAFrame_matrix, int r
rc_physics3D.constraints[constraint_id] = cone; rc_physics3D.constraints[constraint_id] = cone;
return constraint_id; return constraint_id;
} }
return -1;
} }
void rc_deleteConstraint(int constraint_id) void rc_deleteConstraint(int constraint_id)
@@ -5069,7 +5087,7 @@ void rc_setActorFrame(int actor, int frame)
void rc_setActorAutoCulling(int actor, int cull_type) void rc_setActorAutoCulling(int actor, int cull_type)
{ {
if(actor < 0 || actor >= rc_actor.size()) if(actor < 0 || actor >= rc_actor.size())
return; return;
rc_actor[actor].mesh_node->setAutomaticCulling((irr::scene::E_CULLING_TYPE) cull_type); rc_actor[actor].mesh_node->setAutomaticCulling((irr::scene::E_CULLING_TYPE) cull_type);
} }
@@ -5089,7 +5107,7 @@ int rc_getActorAutoCulling(int actor)
void rc_addActorShadow(int actor) void rc_addActorShadow(int actor)
{ {
if(actor < 0 || actor >= rc_actor.size()) if(actor < 0 || actor >= rc_actor.size())
return; return;
if(rc_actor[actor].shadow) if(rc_actor[actor].shadow)
return; return;

View File

@@ -283,10 +283,30 @@ struct rc_physicsWorld3D_obj
rc_physicsWorld3D_obj rc_physics3D; rc_physicsWorld3D_obj rc_physics3D;
//Canvases //Canvases
class rc_contactListener_obj : public b2ContactListener
{
void BeginContact(b2Contact* contact)
{
rc_sprite2D_obj* spriteA = (rc_sprite2D_obj*) contact->GetFixtureA()->GetBody()->GetUserData().pointer;
rc_sprite2D_obj* spriteB = (rc_sprite2D_obj*) contact->GetFixtureB()->GetBody()->GetUserData().pointer;
//std::cout << "sprite[" << spriteA->id << "] collide with sprite[" << spriteB->id << "]" << std::endl;
}
void EndContact(b2Contact* contact)
{
}
};
struct rc_physicsWorld2D_obj struct rc_physicsWorld2D_obj
{ {
bool enabled = false; bool enabled = false;
b2World* world; b2World* world;
rc_contactListener_obj* contact_listener;
float timeStep = 1/20.0; //the length of time passed to simulate (seconds) float timeStep = 1/20.0; //the length of time passed to simulate (seconds)
int velocityIterations = 8; //how strongly to correct velocity int velocityIterations = 8; //how strongly to correct velocity
int positionIterations = 3; //how strongly to correct position int positionIterations = 3; //how strongly to correct position

View File

@@ -901,7 +901,6 @@ bool rc_scalarMatrixColumns(uint32_t mA, uint32_t mB, uint32_t c, uint32_t num_c
rc_copyMatrix(mA, mB); rc_copyMatrix(mA, mB);
int row_offset = 0; int row_offset = 0;
int b_offset = 0;
for(int row = 0; row < rc_matrix[mB].r; row++) for(int row = 0; row < rc_matrix[mB].r; row++)
{ {
row_offset = row * rc_matrix[mB].c; row_offset = row * rc_matrix[mB].c;

View File

@@ -29,6 +29,7 @@ struct rc_sprite2D_animation_obj
struct rc_sprite2D_obj struct rc_sprite2D_obj
{ {
int id; //This is needed to reference this sprite in the contact listener
bool active = false; bool active = false;
int image_id; int image_id;
int sheet_numFrames; int sheet_numFrames;

View File

@@ -66,7 +66,7 @@ int rc_getSpriteFrame(int spr_id)
return rc_sprite[spr_id].animation[current_animation].frames[current_frame]; return rc_sprite[spr_id].animation[current_animation].frames[current_frame];
} }
void rc_setSpriteAnimationFrame(int spr_id, int anim_frame, int frame) void rc_setSpriteAnimationFrame(int spr_id, int spr_animation, int anim_frame, int frame)
{ {
if(spr_id < 0 || spr_id >= rc_sprite.size()) if(spr_id < 0 || spr_id >= rc_sprite.size())
return; return;
@@ -74,19 +74,16 @@ void rc_setSpriteAnimationFrame(int spr_id, int anim_frame, int frame)
if(!rc_sprite[spr_id].active) if(!rc_sprite[spr_id].active)
return; return;
int current_animation = rc_sprite[spr_id].current_animation; if(anim_frame < 0 || anim_frame >= rc_sprite[spr_id].animation[spr_animation].num_frames)
if(anim_frame < 0 || anim_frame >= rc_sprite[spr_id].animation[current_animation].num_frames)
return; return;
if(frame < 0 || frame >= rc_sprite[spr_id].sheet_numFrames) if(frame < 0 || frame >= rc_sprite[spr_id].sheet_numFrames)
return; return;
rc_sprite[spr_id].animation[current_animation].frames[anim_frame] = frame; rc_sprite[spr_id].animation[spr_animation].frames[anim_frame] = frame;
rc_sprite[spr_id].animation[current_animation].frame_start_time = SDL_GetTicks();
} }
int rc_getSpriteAnimationFrame(int spr_id) int rc_getSpriteAnimationFrame(int spr_id, int spr_animation, int anim_frame)
{ {
if(spr_id < 0 || spr_id >= rc_sprite.size()) if(spr_id < 0 || spr_id >= rc_sprite.size())
return -1; return -1;
@@ -94,9 +91,10 @@ int rc_getSpriteAnimationFrame(int spr_id)
if(!rc_sprite[spr_id].active) if(!rc_sprite[spr_id].active)
return -1; return -1;
int current_animation = rc_sprite[spr_id].current_animation; if(anim_frame < 0 || anim_frame >= rc_sprite[spr_id].animation[spr_animation].num_frames)
return -1;
return rc_sprite[spr_id].animation[current_animation].current_frame; return rc_sprite[spr_id].animation[spr_animation].frames[anim_frame];
} }
@@ -115,12 +113,24 @@ void rc_setSpriteAnimationLength(int spr_id, int animation, int num_frames)
if(num_frames <= 0) if(num_frames <= 0)
num_frames = 1; num_frames = 1;
if(num_frames > rc_sprite[spr_id].animation[animation].num_frames)
{
for(int i = rc_sprite[spr_id].animation[animation].num_frames; i < rc_sprite[spr_id].animation[animation].frames.size(); i++)
rc_sprite[spr_id].animation[animation].frames[i] = 0;
}
rc_sprite[spr_id].animation[animation].num_frames = num_frames; rc_sprite[spr_id].animation[animation].num_frames = num_frames;
if(num_frames > rc_sprite[spr_id].animation[animation].frames.size()) if(num_frames > rc_sprite[spr_id].animation[animation].frames.size())
{ {
while(num_frames > rc_sprite[spr_id].animation[animation].frames.size()) while(num_frames > rc_sprite[spr_id].animation[animation].frames.size())
rc_sprite[spr_id].animation[animation].frames.push_back(0); rc_sprite[spr_id].animation[animation].frames.push_back(0);
} }
if(num_frames < rc_sprite[spr_id].animation[animation].frames.size())
{
if(rc_sprite[spr_id].animation[animation].current_frame >= num_frames)
rc_sprite[spr_id].animation[animation].current_frame = num_frames - 1;
}
} }
int rc_getSpriteAnimationLength(int spr_id, int animation) int rc_getSpriteAnimationLength(int spr_id, int animation)
@@ -194,6 +204,18 @@ int rc_getSpriteAnimation(int spr_id)
return rc_sprite[spr_id].current_animation; return rc_sprite[spr_id].current_animation;
} }
int rc_getSpriteActiveAnimationFrame(int spr_id)
{
if(spr_id < 0 || spr_id >= rc_sprite.size())
return -1;
if(!rc_sprite[spr_id].active)
return -1;
int current_animation = rc_sprite[spr_id].current_animation;
return rc_sprite[spr_id].animation[current_animation].current_frame;
}
void rc_loopSpriteAnimation(int spr_id, int num_loops) void rc_loopSpriteAnimation(int spr_id, int num_loops)
{ {
if(spr_id < 0 || spr_id >= rc_sprite.size()) if(spr_id < 0 || spr_id >= rc_sprite.size())
@@ -273,13 +295,31 @@ int rc_createSprite(int img_id, double w, double h)
} }
rc_sprite[spr_id].active = true; rc_sprite[spr_id].active = true;
rc_sprite[spr_id].id = spr_id;
rc_sprite[spr_id].image_id = img_id; rc_sprite[spr_id].image_id = img_id;
rc_sprite[spr_id].frame_size.set(w, h); rc_sprite[spr_id].frame_size.set(w, h);
if(img_id >= 0 && img_id < rc_image.size())
{
if(rc_image[img_id].image)
{
int img_w = rc_image[img_id].image->getSize().Width;
int img_h = rc_image[img_id].image->getSize().Height;
rc_sprite[spr_id].frames_per_row = (int)(img_w / w);
rc_sprite[spr_id].sheet_numFrames = ((int)(img_h / h)) * rc_sprite[spr_id].frames_per_row;
}
else
rc_sprite[spr_id].image_id = -1;
}
else
rc_sprite[spr_id].image_id = -1;
b2BodyDef sprBodyDef; b2BodyDef sprBodyDef;
sprBodyDef.type = b2_staticBody; sprBodyDef.type = b2_dynamicBody;
sprBodyDef.position.Set(0, 0); sprBodyDef.position.Set(0, 0);
sprBodyDef.angle = 0; sprBodyDef.angle = 0;
sprBodyDef.userData.pointer = (uintptr_t)&rc_sprite[spr_id];
rc_sprite[spr_id].physics.body = rc_canvas[rc_active_canvas].physics2D.world->CreateBody(&sprBodyDef); rc_sprite[spr_id].physics.body = rc_canvas[rc_active_canvas].physics2D.world->CreateBody(&sprBodyDef);
b2FixtureDef sprFixtureDef; b2FixtureDef sprFixtureDef;
@@ -288,12 +328,18 @@ int rc_createSprite(int img_id, double w, double h)
fix_shape->SetAsBox(w/2, h/2); fix_shape->SetAsBox(w/2, h/2);
sprFixtureDef.shape = rc_sprite[spr_id].physics.shape; sprFixtureDef.shape = rc_sprite[spr_id].physics.shape;
sprFixtureDef.isSensor = true; sprFixtureDef.isSensor = true;
sprFixtureDef.density = 1;
rc_sprite[spr_id].physics.fixture = rc_sprite[spr_id].physics.body->CreateFixture(&sprFixtureDef); rc_sprite[spr_id].physics.fixture = rc_sprite[spr_id].physics.body->CreateFixture(&sprFixtureDef);
rc_sprite[spr_id].physics.offset_x = 0; rc_sprite[spr_id].physics.offset_x = 0;
rc_sprite[spr_id].physics.offset_y = 0; rc_sprite[spr_id].physics.offset_y = 0;
rc_sprite[spr_id].isSolid = false; rc_sprite[spr_id].isSolid = false;
rc_sprite[spr_id].visible = true;
if(rc_sprite[spr_id].image_id < 0)
rc_sprite[spr_id].visible =false;
else
rc_sprite[spr_id].visible = true;
rc_sprite[spr_id].scale.set(1.0, 1.0); rc_sprite[spr_id].scale.set(1.0, 1.0);
rc_sprite[spr_id].alpha = 255; rc_sprite[spr_id].alpha = 255;
rc_sprite[spr_id].z = 0; rc_sprite[spr_id].z = 0;
@@ -618,6 +664,37 @@ double rc_spriteZ(int spr_id)
return rc_sprite[spr_id].z; return rc_sprite[spr_id].z;
} }
void rc_setSpriteVisible(int spr_id, bool flag)
{
if(spr_id < 0 || spr_id >= rc_sprite.size())
return;
if(!rc_sprite[spr_id].active)
return;
if(rc_sprite[spr_id].image_id)
rc_sprite[spr_id].visible = flag;
else
rc_sprite[spr_id].visible = false;
}
bool rc_spriteIsVisible(int spr_id)
{
if(spr_id < 0 || spr_id >= rc_sprite.size())
return false;
if(!rc_sprite[spr_id].active)
return false;
return rc_sprite[spr_id].visible;
}
//-----------------------------------PHYSICS----------------------------------------------------------------------------------
//This function is called on each canvas on update //This function is called on each canvas on update
void drawSprites(int canvas_id) void drawSprites(int canvas_id)
{ {
@@ -629,7 +706,7 @@ void drawSprites(int canvas_id)
rc_canvas[canvas_id].physics2D.world->Step(step, velocityIterations, positionIterations); rc_canvas[canvas_id].physics2D.world->Step(step, velocityIterations, positionIterations);
//Setting the render target to the current canvas. NOTE: I might change this target to a separate sprite layer later. //Setting the render target to the current canvas. NOTE: I might change this target to a separate sprite layer later.
VideoDriver->setRenderTarget(rc_canvas[canvas_id].texture, false, false); VideoDriver->setRenderTarget(rc_canvas[canvas_id].texture, true, false);
irr::core::dimension2d<irr::u32> src_size; irr::core::dimension2d<irr::u32> src_size;
@@ -680,7 +757,10 @@ void drawSprites(int canvas_id)
if(sprite->current_animation_loop >= sprite->num_animation_loops) if(sprite->current_animation_loop >= sprite->num_animation_loops)
{ {
sprite->isPlaying = false; if(sprite->num_animation_loops < 0)
sprite->isPlaying = true;
else
sprite->isPlaying = false;
sprite->current_animation_loop = 0; sprite->current_animation_loop = 0;
} }
} }
@@ -716,6 +796,7 @@ void drawSprites(int canvas_id)
sprite->color_mod.getGreen(), sprite->color_mod.getGreen(),
sprite->color_mod.getBlue()); sprite->color_mod.getBlue());
//I don't want to draw an image that doesn't exists. Thats just crazy.
draw2DImage(VideoDriver, rc_image[img_id].image, sourceRect, position, rotationPoint, rotation, scale, useAlphaChannel, color, screenSize); draw2DImage(VideoDriver, rc_image[img_id].image, sourceRect, position, rotationPoint, rotation, scale, useAlphaChannel, color, screenSize);
} }
//Must set back to canvas 0 (the backbuffer) before returning //Must set back to canvas 0 (the backbuffer) before returning