Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 225261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:23:45+00:00 2026-05-11T19:23:45+00:00

I am new to c++ and I have been practicing collision in a small

  • 0

I am new to c++ and I have been practicing collision in a small game program that does nothing and I just can’t get the collision right

So I use images loaded into variables

background = oslLoadImageFile("background.png", OSL_IN_RAM, OSL_PF_5551);
sprite = oslLoadImageFile("sprite.png", OSL_IN_RAM, OSL_PF_5551);
bush = oslLoadImageFile("bush.png", OSL_IN_RAM, OSL_PF_5551);

While there are variables stored like

sprite->x = 3;

if ( (sprite->x + spritewidth > bush->x) && (sprite->x < bush->x + bushwidth) && (sprite->y + spriteheight > bush->y) && (sprite->y < bush->y + bushheight) ) 
{
         bushcol = 1;               
}
else
{
        bushcol = 0;      
}

So when i press a button

if (osl_keys->held.down)
{
if (bushcol == 1) 
{
sprite->y = bush->y + 38;
}
else
{
sprite->y += 3;
}
}
if (osl_keys->held.up)
{
if (bushcol == 1) 
{
sprite->y = bush->y - 23;
}
else
{ 
sprite->y -= 3;
}
}
if (osl_keys->held.right)
{
if (bushcol == 1) 
{
sprite->x = bush->x - 28;
}
else
{ 
sprite->x += 3;
}
}
if (osl_keys->held.left)
{
if (bushcol == 1) 
{
sprite->x = bush->x + 28;
}
else
{ 
sprite->x -= 3;
}
}

i was thinking of things like

sprite->y = bushheight - 24;

but it doesnt work

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:23:46+00:00Added an answer on May 11, 2026 at 7:23 pm

    I think you have the basic idea. Just check your work. Here is a simple version which compiles:

    #import <stdlib.h>
    
    typedef struct {
        // I'm going to say x, y, is in the center
        int x;
        int y;
        int width;
        int height;
    } Rect;
    
    Rect newRect(int x, int y, int w, int h) {
        Rect r = {x, y, w, h};
        return r;
    }
    
    int rectsCollide(Rect *r1, Rect *r2) {
        if (r1->x + r1->width/2 < r2->x - r2->width/2) return 0;
        if (r1->x - r1->width/2 > r2->x + r2->width/2) return 0;
        if (r1->y + r1->height/2 < r2->y - r2->height/2) return 0;
        if (r1->y - r1->height/2 > r2->y + r2->height/2) return 0;
        return 1;
    }
    
    int main() {
        Rect r1 = newRect(100,200,40,40);
        Rect r2 = newRect(110,210,40,40);
        Rect r3 = newRect(150,250,40,40);
    
        if (rectsCollide(&r1, &r2))
            printf("r1 collides with r2\n");
        else
            printf("r1 doesnt collide with r2\n");
    
        if (rectsCollide(&r1, &r3))
            printf("r1 collides with r3\n");
        else
            printf("r1 doesnt collide with r3\n");
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been creating a peer to peer connection for a new game, that
I'm using NSMediaQueries to detect new screenshots that have been taken. But to push
I have a new Joomla (2.5) site that has been developed on a different
I am new to TCL and have been practicing a bit on my own.
I came to a new project which have been developed for a quite long
I have been working on a new style for my list boxes but I
I have been building a new application using my current understanding of domain driven
I have been asked to post a new question about how to correctly sort
I have been trying to create a new google custom search engine, but when
I have been using the new feature of Spring 3.1.1 the java based configuration.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.